what is the difference between TermQuery and QueryParser in Lucene 6.0?

后端 未结 2 867
广开言路
广开言路 2021-01-07 03:44

There are two queries,one is created by QueryParser:

QueryParser parser = new QueryParser(field, analyzer);
Query query1 = parser.parse(\"Lucene\");
<         


        
相关标签:
2条回答
  • 2021-01-07 04:19

    The QueryParser parses the string and constructs a BooleanQuery (afaik) consisting of BooleanClauses and analyzes the terms along the way.

    The TermQuery does NOT do analysis, and takes the term as-is. This is the main difference.

    So the query1 and query2 might me equivalent (in a sense, that they provide the same search results) if the field is the same, and the QueryParser's analyzer is not changing the term.

    0 讨论(0)
  • 2021-01-07 04:29

    This is the definition of Term from lucene docs.

    A Term represents a word from text. This is the unit of search. It is composed of two elements, the text of the word, as a string, and the name of the field that the text occurred in.
    

    So in your case the query will be created to search the word "Lucene" in the field "title".

    To explain the difference between the two let me take a difference example,

    consider the following

    Query query2 = new TermQuery(new Term("title", "Apache Lucene"));
    

    In this case the query will search for the exact word "Apache Lucene" in the field title.

    In the other case As an example, let's assume a Lucene index contains two fields, "title" and "body".

    QueryParser parser = new QueryParser("title", "StandardAnalyzer");
    Query query1 = parser.parse("title:Apache body:Lucene");
    Query query2 = parser.parse("title:Apache Lucene");
    Query query3 = parser.parse("title:\"Apache Lucene\"");
    

    couple of things.

    1. "title" is the field that QueryParser will search if you don't prefix it with a field.(as given in the constructor).
    2. parser.parse("title:Apache body:Lucene"); -> in this case the final query will look like this. query2 = title:Apache body:Lucene.
    3. parser.parse("body:Apache Lucene"); -> in this case the final query will also look like this. query2 = body:Apache title:Lucene. but for a different reason.

      So the parser will search "Apache" in body field and "Lucene" in title field. Since The field is only valid for the term that it directly precedes,(http://lucene.apache.org/core/2_9_4/queryparsersyntax.html)

      So since we do not specify any field for lucene , the default field which is "title" will be used.

    4. query2 = parser.parse("title:\"Apache Lucene\""); in this case we are explicitly telling that we want to search for "Apache Lucene" in field "title". This is phrase query and is similar to Term query if analyzed correctly.

    So to summarize the term query will not analyze the term and search as it is. while Query parser parses the input based on some conditions described above.

    0 讨论(0)
提交回复
热议问题