Note that there are some explanatory texts on larger screens.

plurals
  1. POLucene SpanNearQuery
    text
    copied!<p>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. Any thoughts on what might I be doing wrong will be appreciated.</p> <p>Here is the code:</p> <p>//indexing </p> <pre><code>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); } </code></pre> <p>//searching<br> public void doSpanSearching(String text) throws CorruptIndexException, IOException, ParseException {</p> <pre><code> 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&lt;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")); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload