Java tutorial
/******************************************************************************* * 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; /** * @author GaoJin * */ public class SearchCustomer { // you can search the item using the following keywords private String[] queryString = { "name", "email" }; // 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 = { "nickname", "email", "password", "gender", "name" }; // the method search public List<PsCustomer> 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<PsCustomer> customer = new ArrayList<PsCustomer>(); 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); PsCustomer mycustomer = new PsCustomer(); mycustomer.setNickname(resultDocument.get((indexField[0]))); mycustomer.setEmail(resultDocument.get((indexField[1]))); mycustomer.setPassword(resultDocument.get((indexField[2]))); mycustomer.setGender(Short.valueOf(resultDocument.get((indexField[3])))); mycustomer.setName(resultDocument.get((indexField[4]))); customer.add(mycustomer); } return customer; } }