Hi,

Why don't you create your query with API?

Term term = new Term("B", "1 2");
Query query = new TermQuery(term);

Ahmet



On Friday, June 19, 2015 9:31 AM, Gimantha Bandara <giman...@wso2.com> wrote:
Correction..

second time I used the following code to test. Then I got the above
IllegalStateException issue.

w = new QueryParser(null, new WhitespaceAnalyzer()).parse("*B:\"1 2\"*");

not the below one.

w = new QueryParser(null, new WhitespaceAnalyzer()).parse("*\**"B:1 2\"*");

Can someone point out the correct way to query for StringFields?

Thanks,

On Thu, Jun 18, 2015 at 2:12 PM, Gimantha Bandara <giman...@wso2.com> wrote:

> 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

>



-- 
Gimantha Bandara
Software Engineer
WSO2. Inc : http://wso2.com
Mobile : +94714961919

---------------------------------------------------------------------
To unsubscribe, e-mail: java-user-unsubscr...@lucene.apache.org
For additional commands, e-mail: java-user-h...@lucene.apache.org

Reply via email to