Hi all,
I have created lucene documents like below.
Document doc = new Document();
doc.add(new TextField("A", "1", Field.Store.YES));
doc.add(new StringField("B", "1 2 3", Field.Store.NO));
doc.add(new TextField("Publish Date", "2010", Field.Store.NO));
indexWriter.addDocument(doc);
doc = new Document();
doc.add(new TextField("A", "2", Field.Store.YES));
doc.add(new StringField("B", "1 2", Field.Store.NO));
doc.add(new TextField("Publish Date", "2010", Field.Store.NO));
indexWriter.addDocument(doc);
doc = new Document();
doc.add(new TextField("A", "3", Field.Store.YES));
doc.add(new StringField("B", "1", Field.Store.NO));
doc.add(new TextField("Publish Date", "2012", Field.Store.NO));
indexWriter.addDocument(doc);
Now I am using the following code to test the StringField behavior.
Query w = null;
try {
w = new QueryParser(null, new WhitespaceAnalyzer()).parse("B:1
2");
} catch (ParseException e) {
e.printStackTrace();
}
TopScoreDocCollector collector = TopScoreDocCollector.create(100,
true);
searcher.search(w, collector);
ScoreDoc[] hits = collector.topDocs(0).scoreDocs;
Document indexDoc;
for (ScoreDoc doc : hits) {
indexDoc = searcher.doc(doc.doc);
System.out.println(indexDoc.get("A"));
}
Above code should print only the second document's 'A' value as it is the
only one where 'B' has value '1 2'. But it returns the 3rd document. So I
tried using double quotation marks for 'B' value as below.
w = new QueryParser(null, new WhitespaceAnalyzer()).parse("\"B:1 2\"");
It gives the following error.
Exception in thread "main" java.lang.IllegalStateException: field "B" was
indexed without position data; cannot run PhraseQuery (term=1)
at
org.apache.lucene.search.PhraseQuery$PhraseWeight.scorer(PhraseQuery.java:277)
at org.apache.lucene.search.Weight.bulkScorer(Weight.java:131)
at org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:618)
at
org.apache.lucene.search.IndexSearcher.search(IndexSearcher.java:309) Is
my searching query wrong? (Note: I am using whitespace analyzer everywhere)
--
Gimantha Bandara
Software Engineer
WSO2. Inc : http://wso2.com
Mobile : +94714961919