Lucene SpanNearQuery
I am trying to understand Lucene SpanNearQuery and wrote up a dummy example. I am looking for "not" followed by "fox" within 5 of each other. I would expect document 3 to be returned as the only hit. However, I end up getting no hits. An开发者_如何学运维y thoughts on what might I be doing wrong will be appreciated.
Here is the code:
//indexing
public void doSpanIndexing() throws IOException {
IndexWriter writer=new IndexWriter(directory, AnalyzerUtil.getPorterStemmerAnalyzer(new StandardAnalyzer(Version.LUCENE_30)),IndexWriter.MaxFieldLength.LIMITED);
Document doc1=new Document();
doc1.add(new Field("content", " brown fox jumped ", Field.Store.YES, Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
writer.addDocument(doc1);
Document doc2=new Document();
doc2.add(new Field("content", "foxes not jumped over the huge fence", Field.Store.YES, Index.ANALYZED,Field.TermVector.WITH_POSITIONS_OFFSETS));
writer.addDocument(doc2);
Document doc3=new Document();
doc3.add(new Field("content", " brown not fox", Field.Store.YES, Index.ANALYZED, Field.TermVector.WITH_POSITIONS_OFFSETS));
writer.addDocument(doc3);
}
//searching
public void doSpanSearching(String text) throws CorruptIndexException, IOException, ParseException { IndexSearcher searcher=new IndexSearcher(directory);
SpanTermQuery term1 = new SpanTermQuery(new Term("content", "not"));
SpanTermQuery term2 = new SpanTermQuery(new Term("content", text));
SpanNearQuery query = new SpanNearQuery(new SpanQuery[] {term1, term2}, 5, true);
TopDocs topDocs=searcher.search(query,5);
for(int i=0; i<topDocs.totalHits; i++) {
System.out.println("Hit Document number: "+topDocs.scoreDocs[i].doc);
System.out.println("Hit Document score: "+topDocs.scoreDocs[i].score);
Document result=searcher.doc(topDocs.scoreDocs[i].doc);
System.out.println("Search result "+(i+1)+ " is "+result.get("content"));
}
}
"Not" is a stop word in the standard analyzer (i.e. it is removed from your text). Can you try it with another word which is not a stop word?
精彩评论