Lucene 6.0 中的 TermQuery 和 QueryParser 有什么区别? [英] what is the difference between TermQuery and QueryParser in Lucene 6.0?

查看:22
本文介绍了Lucene 6.0 中的 TermQuery 和 QueryParser 有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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?

解决方案

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.

这篇关于Lucene 6.0 中的 TermQuery 和 QueryParser 有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆