Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

lucene

There are two queries,one is created by QueryParser:

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

the other is term query:

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

what is the difference between query1 and query2?

like image 627
Yao Pan Avatar asked Nov 07 '16 14:11

Yao Pan


2 Answers

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.

like image 81
root Avatar answered Oct 17 '22 05:10

root


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 be 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.

like image 6
injecteer Avatar answered Oct 17 '22 06:10

injecteer