lucene 3.0.1 api http://lucene.apache.org/core/old_versioned_docs/versions/3_0_1/api/core/overview-summary.html package com.tianditu.searchDemo;import java.io.File;import org.apache.lucene.analysis.KeywordAnalyzer;import org.apache.lucene.document.Document;import org.apache.lucene.queryParser.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;public class SearchDemo { public static void main(String[] args){ try { Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30); Directory directory = FSDirectory.open(new File("D:/indexes/part-00000")); IndexSearcher isearcher = new IndexSearcher(directory, true); QueryParser parser = new QueryParser(Version.LUCENE_30, "content", analyzer); Query query = parser.parse("建管机构"); ScoreDoc[] hits = isearcher.search(query, null, 1000).scoreDocs; for (int i = 0; i < hits.length; i++) { Document hitDoc = isearcher.doc(hits[i].doc); System.out.println(hitDoc.get("title")); System.out.println(hitDoc.get("url")); } System.out.println(hits.length); isearcher.close(); directory.close(); } catch (Exception e) { e.printStackTrace(); } }}
带编辑距离的查询 // ===========建立按字分词的Span为0的顺序Query============char[] arrChars = strQuery.toCharArray();int iCount = arrChars.length;char[] arrTerm = new char[1];ArrayListlistGroupQuery = new ArrayList ();for( int i = 0; i < iCount; i++){// 去掉空格if(arrChars[i] == ' ')continue;arrTerm[0] = arrChars[i];SpanTermQuery termQuery = new SpanTermQuery(new Term(strField, new String(arrTerm)));listGroupQuery.add(termQuery);}SpanNearQuery suggestQuery = new SpanNearQuery(listGroupQuery.toArray(new SpanQuery[0]), 0, true);//====================================================return suggestQuery;