Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lucene 5.2.1 PhraseQuery was indexed without position data cannot run PhraseQuery

Tags:

lucene

I receive an exception when I make a research with a PhraseQuery on the field named 'content'.
To index this one, I use the org.apache.lucene.document.TextField class because this field contains many words.
I use the FrenchAnalyzer and RAMDirectory class to create the index.

Exception in thread "main" java.lang.IllegalStateException: field "comment" was indexed without position data; cannot run PhraseQuery (phrase=comment:"the skype") at org.apache.lucene.search.PhraseQuery$PhraseWeight.scorer(PhraseQuery.java:301) at org.apache.lucene.search.Weight.bulkScorer(Weight.java:137) at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:768) at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:485) at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:694) at org.apache.lucene.search.IndexSearcher.searchAfter(IndexSearcher.java:410) at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:439) at org.memory.LuceneIndexer.wildcardSearchIndex(LuceneIndexer.java:189) at org.memory.Mem_04.main(Mem_04.java:60)

idxfld = new TextField(field.getFieldname(),(String) field.getValue(),Field.Store.YES);

PhraseQuery query = new PhraseQuery();
query.setSlop(0);
query.add(new Term("comment","the"));
query.add(new Term("comment","skype"));
System.out.println(query.toString());
int numResults = 1000;
ScoreDoc[] hits = searcher.search(query, numResults).scoreDocs;

Can you help me ? Serge

like image 416
Serge Moers Avatar asked Sep 25 '15 11:09

Serge Moers


1 Answers

Check if you have indexed the same field using different field types.

document.Add(new TextField("comment", "Lucene rocks, do you agree?", FieldStore.YES);
.
.
document.Add(new StringField("comment", "Sure", FieldStore.YES);

The above code will produce an "IllegalStateException field - was indexed without position data" error if you run this phrase query

comment: "Lucene rocks"

So make sure you use TextField consistently. Alternatively, you can also use a custom Field with IndexOption set to DOCS_AND_FREQS_AND_POSITIONS.

like image 135
cris almodovar Avatar answered Oct 15 '22 02:10

cris almodovar