Lucene 6.0中的TermQuery和QueryParser有什么区别?

Yao*_*Pan 2 lucene

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

QueryParser parser = new QueryParser(field, analyzer);
Query query1 = parser.parse("Lucene");
Run Code Online (Sandbox Code Playgroud)

另一个是术语查询:

Query query2=new TermQuery(new Term("title", "Lucene"));
Run Code Online (Sandbox Code Playgroud)

query1和query2有什么区别?

man*_*nuj 8

这是来自lucene docs的Term的定义.

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.
Run Code Online (Sandbox Code Playgroud)

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

为了解释两者之间的区别让我采取不同的例子,

考虑以下

Query query2 = new TermQuery(new Term("title", "Apache Lucene"));
Run Code Online (Sandbox Code Playgroud)

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

在另一种情况下作为一个例子,让我们假设一个Lucene索引包含两个字段,"title"和"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\"");
Run Code Online (Sandbox Code Playgroud)

几件事.

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

    因此解析器将在body字段中搜索"Apache",在title字段中搜索"Lucene".由于该字段仅对其直接前面的术语有效,(http://lucene.apache.org/core/2_9_4/queryparsersyntax.html)

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

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

因此,总结术语查询不会分析术语并按原样搜索.而Query解析器根据上述某些条件解析输入.


inj*_*eer 6

解析QueryParser字符串并构造一个由和沿途的术语BooleanQuery组成的(据我所知) 。BooleanClausesanalyzes

TermQuery进行分析,并按原样使用该术语。这是主要的区别。

因此,如果 相同,并且 QueryParser没有更改该术语,则query1query2可能是等效的(从某种意义上来说,它们提供相同的搜索结果) 。fieldanalyzer