com.bitranger.parknshop.common.fulltext.SearchOrder.java Source code

Java tutorial

Introduction

Here is the source code for com.bitranger.parknshop.common.fulltext.SearchOrder.java

Source

/*******************************************************************************
 * Copyright (c) 2014 BitRangers (Team C1).
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *      BitRangers (Team C1) - initial API and implementation
 ******************************************************************************/
/**
 * 
 */
package com.bitranger.parknshop.common.fulltext;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.ParseException;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;

import com.bitranger.parknshop.buyer.model.PsCustomer;
import com.bitranger.parknshop.seller.model.PsOrder;

/**
 * @author GaoJin
 * 
 */
public class SearchOrder {

    // you can search the item using the following keywords
    private String[] queryString = { "id", "psCustomer", "idShop" };
    // state a analyzer
    private Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_45);
    // we want to get the attributes, so here we state the indexfield
    static String[] indexField = { "id", "psCustomer", "idShop", "status", "trackingNumber", "address",
            "postalCode", "nameRecipient", "phoneRecipient", "priceTotal", "timeCreated" };

    // the method search
    public List<PsOrder> search(String value) throws IOException {
        // get the index
        IndexReader reader = DirectoryReader.open(FSDirectory.open(BuildIndexForItem.getIndexFile()));
        // use this to search
        IndexSearcher indexSearcher = new IndexSearcher(reader);
        // use the queryParser to wrap your request
        QueryParser queryParser = new MultiFieldQueryParser(Version.LUCENE_45, queryString, analyzer);

        List<PsOrder> order = new ArrayList<PsOrder>();

        Query query = null;
        try {
            query = queryParser.parse("title:" + value + "~");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        // we get what we want in the topdocs
        TopDocs topDocs = indexSearcher.search(query, 25);
        System.out.println(":" + topDocs.totalHits + "");
        // ScoreDoc[] scoreDoc = topDocs.scoreDocs;
        for (int i = 0; i < topDocs.scoreDocs.length; i++) {
            Document resultDocument = indexSearcher.doc(topDocs.scoreDocs[i].doc);
            PsOrder myorder = new PsOrder();
            myorder.setId(Integer.valueOf(resultDocument.get((indexField[0]))));
            myorder.setStatus(Short.valueOf(resultDocument.get((indexField[3]))));
            myorder.setTrackingNumber(resultDocument.get((indexField[4])));
            myorder.setPriceTotal(Double.valueOf(resultDocument.get((indexField[9]))));
            order.add(myorder);

        }
        return order;
    }

}