I am using SpanTerm Query for searching exact phrase in lucene. But it doesnt seem to work. Here is my code.
Indexing
IndexWriter writer = new IndexW
For version 4.6.0 Indexing:
IndexWriterConfig config=new IndexWriterConfig(Version.LUCENE_46,analyzer);
try {
IndexWriter iwriter=new IndexWriter(mDir,config);
iwriter.deleteAll();
iwriter.commit();
Document doc = new Document();
doc.add(new Field(myfieldname,text,TextField.TYPE_STORED));
iwriter.addDocument(doc);
iwriter.close();
}
Searching for exact phrase (given in variable keyword):
DirectoryReader ireader=DirectoryReader.open(mDir);
IndexSearcher isearcher=new IndexSearcher(ireader);
QueryParser parser = new QueryParser(Version.LUCENE_46,myfieldname,analyzer);
parser.setDefaultOperator(QueryParser.Operator.AND);
parser.setPhraseSlop(0);
Query query=parser.createPhraseQuery(myfieldname,keyword);
ScoreDoc[] hits=isearcher.search(query, null, 1000).scoreDocs;
nret=hits.length;
ireader.close();
Note for the use of "setPhraseSlop(0) and createPhraseQuery()