2

Not able to use lucene's keyword analyzer properly,

    String term = "new york";
    // id and location are the fields in which i want to search the "term"
    MultiFieldQueryParser queryParser = new MultiFieldQueryParser(
                                       Version.LUCENE_30,
                                       {"id", "location"},
                                       new KeywordAnalyzer());
    Query query = queryParser.parse(term);
    System.out.println(query.toString());

OUTCOME: (id:new location:new) (id:york location:york)

EXPECTED OUTCOME: (id:new york location:new york) (id:new york location:new york)

Please help me identify what i am doing wrong here??

divibisan
  • 11,659
  • 11
  • 40
  • 58
Elvis
  • 125
  • 9
  • First thing, I'm curious this got compiled, because Eclipse tells me there's an error - I had to replace `{"id", "location"}` with `new String[]{"id", "location"}` (otherwise it is treated as two strings instead of an array). You compiled exactly this code, or replaced the parameter? – jakub.g Sep 20 '11 at 17:23

1 Answers1

5

You are doing nothing wrong. This is the way QueryParser works. Since you are indexing your text as a single token with KeywordAnalyzer, you should use TermQuery. Since you have two fields to search, you can combine two TermQueries like:

BooleanQuery bq = new BooleanQuery();
bq.Add(new TermQuery(new Term("id", term)), BooleanClause.Occur.SHOULD );
bq.Add(new TermQuery(new Term("location", term)), BooleanClause.Occur.SHOULD );
string txtQuery = bq.ToString();
jakub.g
  • 38,512
  • 12
  • 92
  • 130
L.B
  • 114,136
  • 19
  • 178
  • 224