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

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

问题描述

有两个查询,一个是由QueryParser创建的:

There are two queries,one is created by QueryParser:

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

另一个是术语查询:

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

query1和query2有什么区别?

what is the difference between query1 and query2?

推荐答案

这是Lucene文档中术语的定义.

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.

因此,在您的情况下,将创建查询以在标题"字段中搜索单词"Lucene".

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,

考虑以下

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

在这种情况下,查询将在字段标题中搜索确切的单词"Apache Lucene".

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

在另一种情况下 例如,假设Lucene索引包含两个字段"title"和"body".

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\"");

几件事情.

  1. 标题"是QueryParser将在没有前缀字段的情况下搜索的字段.(如构造函数中所述).
  2. parser.parse("title:Apache body:Lucene");->在这种情况下,最终查询将如下所示. query2 =标题:Apache正文:Lucene.
  3. parser.parse("body:Apache Lucene");->在这种情况下,最终查询也将如下所示. query2 =正文:Apache标题:Lucene.但出于不同的原因.

  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.

因此,解析器将在主体字段中搜索"Apache",在标题字段中搜索"Lucene". 因为该字段仅对其直接在其前面的术语有效,(

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)

因此,由于我们没有为lucene指定任何字段,因此将使用默认字段"title".

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

query2 = parser.parse("title:\"Apache Lucene\"");明确告诉我们要在字段"title"中搜索"Apache Lucene".这是词组查询,如果经过正确分析,则类似于术语查询.

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天全站免登陆