Question

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??

Was it helpful?

Solution

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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top