Lucene Query Parser 语法

2023-05-16

lucene的组合条件语法,看了网上很多文章,真的都太差了。还是官网清晰明了一点。 

SKIP NAVIGATION LINKS

  • OVERVIEW
  • PACKAGE
  • CLASS
  • USE
  • TREE
  • DEPRECATED
  • HELP
  • PREV PACKAGE
  • NEXT PACKAGE
  • FRAMES
  • NO FRAMES
  • ALL CLASSES

Package org.apache.lucene.queryparser.classic

A simple query parser implemented with JavaCC.

See: Description

  • Interface Summary
    InterfaceDescription
    CharStream

    This interface describes a character stream that maintains line and column number positions of the characters.

    QueryParserConstants

    Token literal values and constants.

  • Class Summary
    ClassDescription
    FastCharStream

    An efficient implementation of JavaCC's CharStream interface.

    MultiFieldQueryParser

    A QueryParser which constructs queries to search multiple fields.

    QueryParser

    This class is generated by JavaCC.

    QueryParserBase

    This class is overridden by QueryParser in QueryParser.jj and acts to separate the majority of the Java code from the .jj grammar file.

    QueryParserTokenManager

    Token Manager.

    Token

    Describes the input token stream.

  • Enum Summary
    EnumDescription
    QueryParser.Operator

    The default operator for parsing queries.

  • Exception Summary
    ExceptionDescription
    ParseException

    This exception is thrown when parse errors are encountered.

  • Error Summary
    ErrorDescription
    TokenMgrError

    Token Manager Error.

Package org.apache.lucene.queryparser.classic Description

A simple query parser implemented with JavaCC.

Note that JavaCC defines lots of public classes, methods and fields that do not need to be public.  These clutter the documentation.  Sorry.

Note that because JavaCC defines a class named Token, org.apache.lucene.analysis.Token must always be fully qualified in source code in this package.

NOTE: org.apache.lucene.queryparser.flexible.standard has an alternative queryparser that matches the syntax of this one, but is more modular, enabling substantial customization to how a query is created.

Query Parser Syntax

  • Overview
  • Terms
  • Fields
  • Term Modifiers
    • Wildcard Searches
    • Regular expression Searches
    • Fuzzy Searches
    • Proximity Searches
    • Range Searches
    • Boosting a Term
  • Boolean Operators
    • OR
    • AND
    • +
    • NOT
    • -
  • Grouping
  • Field Grouping
  • Escaping Special Characters

Overview

Although Lucene provides the ability to create your own queries through its API, it also provides a rich query language through the Query Parser, a lexer which interprets a string into a Lucene Query using JavaCC.

Generally, the query parser syntax may change from release to release. This page describes the syntax as of the current release. If you are using a different version of Lucene, please consult the copy of docs/queryparsersyntax.html that was distributed with the version you are using.

Before choosing to use the provided Query Parser, please consider the following:

  1. If you are programmatically generating a query string and then parsing it with the query parser then you should seriously consider building your queries directly with the query API. In other words, the query parser is designed for human-entered text, not for program-generated text.
  2. Untokenized fields are best added directly to queries, and not through the query parser. If a field's values are generated programmatically by the application, then so should query clauses for this field. An analyzer, which the query parser uses, is designed to convert human-entered text to terms. Program-generated values, like dates, keywords, etc., should be consistently program-generated.
  3. In a query form, fields which are general text should use the query parser. All others, such as date ranges, keywords, etc. are better added directly through the query API. A field with a limit set of values, that can be specified with a pull-down menu should not be added to a query string which is subsequently parsed, but rather added as a TermQuery clause.

Terms

A query is broken up into terms and operators. There are two types of terms: Single Terms and Phrases.

A Single Term is a single word such as "test" or "hello".

A Phrase is a group of words surrounded by double quotes such as "hello dolly".

Multiple terms can be combined together with Boolean operators to form a more complex query (see below).

Note: The analyzer used to create the index will be used on the terms and phrases in the query string. So it is important to choose an analyzer that will not interfere with the terms used in the query string.

Fields

Lucene supports fielded data. When performing a search you can either specify a field, or use the default field. The field names and default field is implementation specific.

You can search any field by typing the field name followed by a colon ":" and then the term you are looking for.

As an example, let's assume a Lucene index contains two fields, title and text and text is the default field. If you want to find the document entitled "The Right Way" which contains the text "don't go this way", you can enter:


title:"The Right Way" AND text:go  

or


title:"The Right Way" AND go  

Since text is the default field, the field indicator is not required.

Note: The field is only valid for the term that it directly precedes, so the query


title:The Right Way  

Will only find "The" in the title field. It will find "Right" and "Way" in the default field (in this case the text field).

Term Modifiers

Lucene supports modifying query terms to provide a wide range of searching options.

Wildcard Searches

Lucene supports single and multiple character wildcard searches within single terms (not within phrase queries).

To perform a single character wildcard search use the "?" symbol.

To perform a multiple character wildcard search use the "*" symbol.

The single character wildcard search looks for terms that match that with the single character replaced. For example, to search for "text" or "test" you can use the search:


te?t  

Multiple character wildcard searches looks for 0 or more characters. For example, to search for test, tests or tester, you can use the search:


test*  

You can also use the wildcard searches in the middle of a term.


te*t  

Note: You cannot use a * or ? symbol as the first character of a search.

Regular Expression Searches

Lucene supports regular expression searches matching a pattern between forward slashes "/". The syntax may change across releases, but the current supported syntax is documented in the RegExp class. For example to find documents containing "moat" or "boat":


/[mb]oat/  

Fuzzy Searches

Lucene supports fuzzy searches based on Damerau-Levenshtein Distance. To do a fuzzy search use the tilde, "~", symbol at the end of a Single word Term. For example to search for a term similar in spelling to "roam" use the fuzzy search:


roam~  

This search will find terms like foam and roams.

An additional (optional) parameter can specify the maximum number of edits allowed. The value is between 0 and 2, For example:


roam~1  

The default that is used if the parameter is not given is 2 edit distances.

Previously, a floating point value was allowed here. This syntax is considered deprecated and will be removed in Lucene 5.0

Proximity Searches

Lucene supports finding words are a within a specific distance away. To do a proximity search use the tilde, "~", symbol at the end of a Phrase. For example to search for a "apache" and "jakarta" within 10 words of each other in a document use the search:


"jakarta apache"~10  

Range Searches

Range Queries allow one to match documents whose field(s) values are between the lower and upper bound specified by the Range Query. Range Queries can be inclusive or exclusive of the upper and lower bounds. Sorting is done lexicographically.


mod_date:[20020101 TO 20030101]  

This will find documents whose mod_date fields have values between 20020101 and 20030101, inclusive. Note that Range Queries are not reserved for date fields. You could also use range queries with non-date fields:


title:{Aida TO Carmen}  

This will find all documents whose titles are between Aida and Carmen, but not including Aida and Carmen.

Inclusive range queries are denoted by square brackets. Exclusive range queries are denoted by curly brackets.

Boosting a Term

Lucene provides the relevance level of matching documents based on the terms found. To boost a term use the caret, "^", symbol with a boost factor (a number) at the end of the term you are searching. The higher the boost factor, the more relevant the term will be.

Boosting allows you to control the relevance of a document by boosting its term. For example, if you are searching for


jakarta apache  

and you want the term "jakarta" to be more relevant boost it using the ^ symbol along with the boost factor next to the term. You would type:


jakarta^4 apache  

This will make documents with the term jakarta appear more relevant. You can also boost Phrase Terms as in the example:


"jakarta apache"^4 "Apache Lucene"  

By default, the boost factor is 1. Although the boost factor must be positive, it can be less than 1 (e.g. 0.2)

Boolean Operators

Boolean operators allow terms to be combined through logic operators. Lucene supports AND, "+", OR, NOT and "-" as Boolean operators(Note: Boolean operators must be ALL CAPS).

OR

The OR operator is the default conjunction operator. This means that if there is no Boolean operator between two terms, the OR operator is used. The OR operator links two terms and finds a matching document if either of the terms exist in a document. This is equivalent to a union using sets. The symbol || can be used in place of the word OR.

To search for documents that contain either "jakarta apache" or just "jakarta" use the query:


"jakarta apache" jakarta  

or


"jakarta apache" OR jakarta  

AND

The AND operator matches documents where both terms exist anywhere in the text of a single document. This is equivalent to an intersection using sets. The symbol && can be used in place of the word AND.

To search for documents that contain "jakarta apache" and "Apache Lucene" use the query:


"jakarta apache" AND "Apache Lucene"  

+

The "+" or required operator requires that the term after the "+" symbol exist somewhere in a the field of a single document.

To search for documents that must contain "jakarta" and may contain "lucene" use the query:


+jakarta lucene  

NOT

The NOT operator excludes documents that contain the term after NOT. This is equivalent to a difference using sets. The symbol ! can be used in place of the word NOT.

To search for documents that contain "jakarta apache" but not "Apache Lucene" use the query:


"jakarta apache" NOT "Apache Lucene"  

Note: The NOT operator cannot be used with just one term. For example, the following search will return no results:


NOT "jakarta apache"  

-

The "-" or prohibit operator excludes documents that contain the term after the "-" symbol.

To search for documents that contain "jakarta apache" but not "Apache Lucene" use the query:


"jakarta apache" -"Apache Lucene"  

Grouping

Lucene supports using parentheses to group clauses to form sub queries. This can be very useful if you want to control the boolean logic for a query.

To search for either "jakarta" or "apache" and "website" use the query:


(jakarta OR apache) AND website  

This eliminates any confusion and makes sure you that website must exist and either term jakarta or apache may exist.

Field Grouping

Lucene supports using parentheses to group multiple clauses to a single field.

To search for a title that contains both the word "return" and the phrase "pink panther" use the query:


title:(+return +"pink panther")  

Escaping Special Characters

Lucene supports escaping special characters that are part of the query syntax. The current list special characters are

+ - && || ! ( ) { } [ ] ^ " ~ * ? : \ /

To escape these character use the \ before the character. For example to search for (1+1):2 use the query:


\(1\+1\)\:2  

SKIP NAVIGATION LINKS

  • OVERVIEW
  • PACKAGE
  • CLASS
  • USE
  • TREE
  • DEPRECATED
  • HELP
  • PREV PACKAGE
  • NEXT PACKAGE
  • FRAMES
  • NO FRAMES
  • ALL CLASSES

Copyright © 2000-2021 Apache Software Foundation. All Rights Reserved.

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Lucene Query Parser 语法 的相关文章

  • 如何处理 Elasticsearch 中的重复数据?

    我已经使用父子映射来规范化数据 但据我所知 无法从 parent 文档获取任何字段 这是我的索引的映射 mappings building properties name type string flat parent type build
  • 如何创建更复杂的 Lucene 查询字符串?

    这个问题是从this https stackoverflow com questions 532365 how to get more out of lucene net问题 我的询问有两个方面 但因为两者都是相关的 所以我认为将它们放在一
  • Lucene 与 PHP

    我可以将 Lucene 与 PHP 一起使用吗 我不想使用 Zend 我可以在本机 PHP 非框架 中使用吗 我建议使用阿帕奇SOLR http lucene apache org solr 作为您的 Lucene 后端 并通过 PHP 代
  • Lucene TermQuery 和 QueryParser

    我有 2 个 lucene 查询 1 Term term new Term Properties LUCENE APPARTMENT ADDRESS address Query termQuery new TermQuery term To
  • 如何检查 Lucene IndexWriter 实例是否有效/打开?

    抱歉这个简单的问题 但似乎没有任何明显的方法 根据文档 建议保留单个实例IndexWriter存储在内存中 可以一次又一次地用于更新 而不是为每次更改打开和关闭一个内存 这要昂贵得多 然而 文档还指出IndexWriter如果发生异常 例如
  • “你的意思?” Lucene.net 中的功能

    有人可以告诉我如何在 Lucene net 中实现 您是说 功能吗 Thanks 你应该调查一下拼写检查器 https svn apache org repos asf lucene lucene net trunk C 23 contri
  • 如何为 PhraseQuery 搜索设置 Lucene 标准分析器?

    Lucene 上的各种教程给我的印象是 如果我这样做 IndexWriter writer new IndexWriter indexPath new StandardAnalyzer Version LUCENE CURRENT true
  • Lucene 中的关键字(OR、AND)搜索

    我在我的门户 基于 J2EE 中使用 Lucene 来提供索引和搜索服务 问题出在Lucene的关键字上 当您在搜索查询中使用其中之一时 您会收到错误消息 例如 searchTerms ik OR jij 这工作正常 因为它会搜索 ik o
  • Solrcloud 多核配置

    我有一个独立的Solr具有 4 个不同内核的实例使用嵌入式 Jetty 服务器运行良好 我为 v4 10 3 配置了核心 但自从我迁移到 v5 1 后 一切似乎都工作正常 无需任何更改 在投入生产之前 我需要将其设置为Solrcloud 安
  • Elasticsearch - 如果术语出现频率越高,得分越高

    我有 2 个文档 正在搜索关键字 Twitter 假设两个文档都是带有 标签 字段的博客文章 文档 A 在 标签 字段中只有 1 个术语 它是 Twitter 文档 B 在 标签 字段中有 100 个术语 但其中 3 个是 Twitter
  • 如何指示Lucene中的StandardAnalyzer不删除停用词?

    简单问题 如何制作Lucene的StandardAnalyzer在分析我的句子时不要删除停用词 答案取决于版本 为了Lucene 3 0 3 当前 http lucene apache org java 3 0 3 api core org
  • 重用 Runnable 的最佳方式

    我有一个实现的类Runnable目前我正在使用 Executor 作为线程池来运行任务 将文档索引到 Lucene executor execute new LuceneDocIndexer doc writer 我的问题是我的 Runna
  • Apache Solr 6.6 替换文档而不是更新

    我已配置 solr 6 6 1 进行测试设置 在索引了一些文档后 我必须更新一些字段 我正在使用 python 客户端solr https pythonhosted org solrpy reference html 要更新 以下是我的代码
  • 索引的最大大小/文档数量是多少 - 32 位操作系统上的 java lucene 3.0.2

    我正在使用 lucene 和 40GB 的数据 500M 的元组 2 个字段的行为类似于键值 我创建了一个 35 GB 的索引 但它不起作用 因此 我想创建一组较小的索引 但为此 我需要有关最大尺寸的信息 你使用什么文件系统 您绝对确定已创
  • 如何查找相似文档

    如何在 Lucene 中找到给定文档的相似文档 我不知道文本是什么 我只知道文档是什么 有没有办法在lucene中找到类似的文档 我是新手 所以我可能需要一些指导 你可能想检查 lucene 的 MoreLikeThis 功能 MoreLi
  • 更改 SOLR 默认连接

    我正在使用嵌入 SOLR 的应用程序 SOLR 在 Tomcat 的 webapp 区域中像一场战争一样运行 是否有 SOLR 配置允许我切换搜索的默认 SOLR 行为以假定 AND 而不是 OR 作为连接运算符 在您的模式文件中添加 或修
  • Solr 中的多值字段排序

    我有一个 Solr 索引 将每个产品的价格存储在多值字段中 我需要按价格对结果集进行排序 其中价格从低到高 从高到低 我尝试对价格进行排序 它显示错误您无法对 multivalued True 字段进行排序 下面是我的 solr XML
  • 了解elasticsearch如何在内部存储日期

    我想了解 ES 如何在其索引内部存储日期值 它会转换为 UTC 吗 我有一个日期类型的字段 t 这是映射 t type date 现在 当我向 ES 插入 添加文档时 它如何存储在索引中 t 1427700477165 从 Date now
  • 如何判断lucene索引版本?

    我正在编写一个 shell 脚本 csh 它必须确定 lucene 索引版本 然后根据该版本将索引升级到下一个版本 所以 如果 lucene 索引是 2 x 我必须将索引升级到 3 x 最后索引需要升级到6 x 由于升级索引是一个顺序过程
  • 随着索引和文档数量恒定,elasticsearch 批量索引会随着时间的推移而变慢

    我遇到了使用 NET NEST 客户端和 ElasticSearch 进行批量索引的性能随着时间的推移 索引数量和文档数量恒定而降低的情况 我们正在奔跑ElasticSearch Version 0 19 11 JVM 23 5 b02在具

随机推荐