Elasticsearch Query String Query

【硬刚ES】ES基础(九)Query & Simple Query String Query 本文是对《【硬刚大数据之学习路线篇】从零到大数据专家的学习指南(全面升级版)》的ES部分补充。 课程 Demo 需导入Movie测试数据,具体参考“2.4-Logstash安装与导入数据” PUT /users/_doc/1 { "name":"Ruan Yiming", "about":"java, golang, node, swift, elasticsearch" } PUT /users/_doc/2 { "name":"Li Yiming", .. 阅读详情

Query String Queryedit

On this page

A query that uses a query parser in order to parse its content. Here is an example:

{
    "query_string" : {
        "default_field" : "content",
        "query" : "this AND that OR thus"
    }
}

The query_string top level parameters include:

Parameter Description

query

The actual query to be parsed. See Query string syntax.

default_field

The default field for query terms if no prefix field is specified. Defaults to the index.query.default_field index settings, which in turn defaults to_all.

default_operator

The default operator used if no explicit operator is specified. For example, with a default operator of OR, the query capital of Hungary is translated tocapital OR of OR Hungary, and with default operator of AND, the same query is translated to capital AND of AND Hungary. The default value is OR.

analyzer

The analyzer name used to analyze the query string.

allow_leading_wildcard

When set, * or ? are allowed as the first character. Defaults to true.

lowercase_expanded_terms

Whether terms of wildcard, prefix, fuzzy, and range queries are to be automatically lower-cased or not (since they are not analyzed). Default ittrue.

enable_position_increments

Set to true to enable position increments in result queries. Defaults totrue.

fuzzy_max_expansions

Controls the number of terms fuzzy queries will expand to. Defaults to 50

fuzziness

Set the fuzziness for fuzzy queries. Defaults to AUTO. See the section called “Fuzzinessedit” for allowed settings.

fuzzy_prefix_length

Set the prefix length for fuzzy queries. Default is 0.

phrase_slop

Sets the default slop for phrases. If zero, then exact phrase matches are required. Default value is 0.

boost

Sets the boost value of the query. Defaults to 1.0.

analyze_wildcard

By default, wildcards terms in a query string are not analyzed. By setting this value to true, a best effort will be made to analyze those as well.

auto_generate_phrase_queries

Defaults to false.

max_determinized_states

Limit on how many automaton states regexp queries are allowed to create. This protects against too-difficult (e.g. exponentially hard) regexps. Defaults to 10000.

minimum_should_match

A value controlling how many "should" clauses in the resulting boolean query should match. It can be an absolute value (2), a percentage (30%) or acombination of both.

lenient

If set to true will cause format based failures (like providing text to a numeric field) to be ignored.

locale

Locale that should be used for string conversions. Defaults to ROOT.

time_zone

Time Zone to be applied to any range query related to dates. See also JODA timezone.

When a multi term query is being generated, one can control how it gets rewritten using the rewriteparameter.

Default Fieldedit

When not explicitly specifying the field to search on in the query string syntax, theindex.query.default_field will be used to derive which field to search on. It defaults to _all field.

So, if _all field is disabled, it might make sense to change it to set a different default field.

Multi Fieldedit

The query_string query can also run against multiple fields. Fields can be provided via the "fields"parameter (example below).

The idea of running the query_string query against multiple fields is to expand each query term to an OR clause like this:

field1:query_term OR field2:query_term | ...

For example, the following query

{
    "query_string" : {
        "fields" : ["content", "name"],
        "query" : "this AND that"
    }
}

matches the same words as

{
    "query_string": {
      "query": "(content:this OR name:this) AND (content:that OR name:that)"
    }
}

Since several queries are generated from the individual search terms, combining them can be automatically done using either a dis_max query or a simple bool query. For example (the name is boosted by 5 using ^5notation):

{
    "query_string" : {
        "fields" : ["content", "name^5"],
        "query" : "this AND that OR thus",
        "use_dis_max" : true
    }
}

Simple wildcard can also be used to search "within" specific inner elements of the document. For example, if we have a city object with several fields (or inner object with fields) in it, we can automatically search on all "city" fields:

{
    "query_string" : {
        "fields" : ["city.*"],
        "query" : "this AND that OR thus",
        "use_dis_max" : true
    }
}

Another option is to provide the wildcard fields search in the query string itself (properly escaping the *sign), for example: city.\*:something.

When running the query_string query against multiple fields, the following additional parameters are allowed:

Parameter Description

use_dis_max

Should the queries be combined using dis_max (set it to true), or a bool query (set it tofalse). Defaults to true.

tie_breaker

When using dis_max, the disjunction max tie breaker. Defaults to 0.

The fields parameter can also include pattern based field names, allowing to automatically expand to the relevant fields (dynamically introduced fields included). For example:

{
    "query_string" : {
        "fields" : ["content", "name.*^5"],
        "query" : "this AND that OR thus",
        "use_dis_max" : true
    }
}

Query string syntaxedit

The query string “mini-language” is used by the Query String Query and by the q query string parameter in the search API.

The query string is parsed into a series of terms and operators. A term can be a single word — quick orbrown — or a phrase, surrounded by double quotes — "quick brown" — which searches for all the words in the phrase, in the same order.

Operators allow you to customize the search — the available options are explained below.

Field namesedit

As mentioned in Query String Query, the default_field is searched for the search terms, but it is possible to specify other fields in the query syntax:

  • where the status field contains active

    status:active
  • where the title field contains quick or brown. If you omit the OR operator the default operator will be used

    title:(quick OR brown)
    title:(quick brown)
  • where the author field contains the exact phrase "john smith"

    author:"John Smith"
  • where any of the fields book.title, book.content or book.date contains quick or brown (note how we need to escape the * with a backslash):

    book.\*:(quick brown)
  • where the field title has no value (or is missing):

    _missing_:title
  • where the field title has any non-null value:

    _exists_:title
Wildcardsedit

Wildcard searches can be run on individual terms, using ? to replace a single character, and * to replace zero or more characters:

qu?ck bro*

Be aware that wildcard queries can use an enormous amount of memory and perform very badly — just think how many terms need to be queried to match the query string "a* b* c*".

Warning

Allowing a wildcard at the beginning of a word (eg "*ing") is particularly heavy, because all terms in the index need to be examined, just in case they match. Leading wildcards can be disabled by setting allow_leading_wildcard to false.

Wildcarded terms are not analyzed by default — they are lowercased (lowercase_expanded_terms defaults totrue) but no further analysis is done, mainly because it is impossible to accurately analyze a word that is missing some of its letters. However, by setting analyze_wildcard to true, an attempt will be made to analyze wildcarded words before searching the term list for matching terms.

Regular expressionsedit

Regular expression patterns can be embedded in the query string by wrapping them in forward-slashes ("/"):

name:/joh?n(ath[oa]n)/

The supported regular expression syntax is explained in Regular expression syntax.

Warning

The allow_leading_wildcard parameter does not have any control over regular expressions. A query string such as the following would force Elasticsearch to visit every term in the index:

/.*n/

Use with caution!

Fuzzinessedit

We can search for terms that are similar to, but not exactly like our search terms, using the “fuzzy” operator:

quikc~ brwn~ foks~

This uses the Damerau-Levenshtein distance to find all terms with a maximum of two changes, where a change is the insertion, deletion or substitution of a single character, or transposition of two adjacent characters.

The default edit distance is 2, but an edit distance of 1 should be sufficient to catch 80% of all human misspellings. It can be specified as:

quikc~1
Proximity searchesedit

While a phrase query (eg "john smith") expects all of the terms in exactly the same order, a proximity query allows the specified words to be further apart or in a different order. In the same way that fuzzy queries can specify a maximum edit distance for characters in a word, a proximity search allows us to specify a maximum edit distance of words in a phrase:

"fox quick"~5

The closer the text in a field is to the original order specified in the query string, the more relevant that document is considered to be. When compared to the above example query, the phrase "quick fox" would be considered more relevant than "quick brown fox".

Rangesedit

Ranges can be specified for date, numeric or string fields. Inclusive ranges are specified with square brackets [min TO max] and exclusive ranges with curly brackets {min TO max}.

  • All days in 2012:

    date:[2012-01-01 TO 2012-12-31]
  • Numbers 1..5

    count:[1 TO 5]
  • Tags between alpha and omega, excluding alpha and omega:

    tag:{alpha TO omega}
  • Numbers from 10 upwards

    count:[10 TO *]
  • Dates before 2012

    date:{* TO 2012-01-01}

Curly and square brackets can be combined:

  • Numbers from 1 up to but not including 5

    count:[1 TO 5}

Ranges with one side unbounded can use the following syntax:

age:>10
age:>=10
age:<10
age:<=10
Note

To combine an upper and lower bound with the simplified syntax, you would need to join two clauses with an AND operator:

age:(>=10 AND <20)
age:(+>=10 +<20)

The parsing of ranges in query strings can be complex and error prone. It is much more reliable to use an explicit range query.

Boostingedit

Use the boost operator ^ to make one term more relevant than another. For instance, if we want to find all documents about foxes, but we are especially interested in quick foxes:

quick^2 fox

The default boost value is 1, but can be any positive floating point number. Boosts between 0 and 1 reduce relevance.

Boosts can also be applied to phrases or to groups:

"john smith"^2   (foo bar)^4
Boolean operatorsedit

By default, all terms are optional, as long as one term matches. A search for foo bar baz will find any document that contains one or more of foo or bar or baz. We have already discussed thedefault_operator above which allows you to force all terms to be required, but there are also boolean operators which can be used in the query string itself to provide more control.

The preferred operators are + (this term must be present) and - (this term must not be present). All other terms are optional. For example, this query:

quick brown +fox -news

states that:

  • fox must be present
  • news must not be present
  • quick and brown are optional — their presence increases the relevance

The familiar operators AND, OR and NOT (also written &&, || and !) are also supported. However, the effects of these operators can be more complicated than is obvious at first glance. NOT takes precedence over AND, which takes precedence over OR. While the + and - only affect the term to the right of the operator, AND and OR can affect the terms to the left and right.

Groupingedit

Multiple terms or clauses can be grouped together with parentheses, to form sub-queries:

(quick OR brown) AND fox

Groups can be used to target a particular field, or to boost the result of a sub-query:

status:(active OR pending) title:(full text search)^2
Reserved charactersedit

If you need to use any of the characters which function as operators in your query itself (and not as operators), then you should escape them with a leading backslash. For instance, to search for (1+1)=2, you would need to write your query as \(1\+1\)\=2.

The reserved characters are: + - = && || > < ! ( ) { } [ ] ^ " ~ * ? : \ /

Failing to escape these special characters correctly could lead to a syntax error which prevents your query from running.

Empty Queryedit

If the query string is empty or only contains whitespaces the query will yield an empty result set.

【Unity 实用工具篇】✨| I2 Localization 实现本地化及多种语言切换,快速上手 Unity的多语言本地化是一个很实用的功能,它可以帮助游戏支持多种语言,让不同语言的玩家都能够更好地体验游戏。 而实现本地化的方案也有很多种,各个方案之间也各有优劣,后面也会对多个方案进行介绍学习。 本文就来介绍一个专门作用于多语言本地化的插件:I2 Localization 。 阅读详情

相关推荐

对族实例进行任意的矩阵变换,需要什么样的族?

问:一个小问题,想向您请教一下。 能不能实现一个这样的操作:创建了一个常规模型的FamilyInstance,然后知道旋转和平移的矩阵,能否根据变换矩阵更新FamilyInstance的位置,绕任意轴旋转任意角度。 请问什么样的实体才能在revit项目文件中任意旋转?貌似一般的实例只能在XY平面内旋转,并且LocationPoint中只有一个Rotation参数。答:您好,因为Revit里面的构件

我爱Revit —— BIM应用及BIM应用软件解决方案专栏 3636

Elasticsearch: query_string 查询

DSL被广泛使用于Elasticsearch的搜索中。query_string是其中的比较特别的一种。它使用一套称之为“mini-language”的句法。它也被使用于URI Search之中。query_string 使用具有严格语法的解析器,根据提供的查询字符串返回文档。 该查询使用语法基于OR, AND或NOT等运算符来解析和拆分提供的查询字符串。 然后查询在返回匹配文档之前独立分析每个拆...

Elastic 中国社区官方博客 1万+

Tesseract in Java - Intellij IDEA对Tess4J的配置和使用

最近要弄OCR,搜到这个,官网全英文,很烦,看看有没有中文的博客介绍,擦,一个比一个奇葩,还是自己按照官方的来吧,外网慢的很,传到csdn给大家下载。中文训练库下载地址: http://download.csdn.net/detail/d_dmelon/9916532Tess4J3.4下载地址: http://download.csdn.net/detail/d_dmelon/9916519安装

Lewis的博客 1万+

Elasticsearch 基本使用(五)查询条件匹配方式(query & query_string)

ES中常用的查询类型往大了分可以分为简单查询,复合查询,聚合查询等;而复合查询及聚合查询都是基于简单查询的;简单查询里面对条件的匹配方式又分为不同类型。term[s],match,match_all,match_phrase 等等。

冰糖的博客 1万+

OpenCvSharp之利用海康相机采集图像并在pictureBox中显示

OpenCvSharp之利用海康相机采集图像并在pictureBox中显示 说明:OpenCvSharp版本为4.0 海康相机为黑白相机 实现功能:将海康相机采集的图像转成Mat,并在pictureBox中显示。 示例代码: Mat srcImage; private Mat CaptureImage() { int nRet; //MyCamera.MVCC_I...

Kevin_Sun777的博客 5511

【TensorRT】TensorRT C# API 项目介绍:基于C#与TensorRT部署深度学习模型(上篇)

开发了TensorRT C# API 2.0版本,该版本在开发时充分考虑了上一版本应用时出现的问题,并进行了改进。同时在本版本中,我们对接口进行了优化,使用起来更加简单,并同时提供了相关的应用案例,方便开发者进行使用。

grape_yan的博客 3053

ElasticSearch7.2之query_string查询

query_string 查询,如果熟悉lucene的查询语法,我们可以直接⽤lucene查询语法写⼀个查询串进⾏查询,ES中接到请求后,通过查询解析器,解析查询串⽣成对应的查询。 指定单个字段查询 POST /nba/_search { "query": { "query_string": { "default_field": "dis...

qq_22049773的博客 1517

HI3516CV610海思SOC:超高清智慧视觉开发全攻略

本文全面解析海思HI3516CV610 SOC在超高清智慧视觉开发中的应用。该芯片集成双核A7处理器、1Tops NPU、AI ISP及4K视频编码器,专为智能摄像头设计。文章详细介绍了开发环境搭建、核心硬件架构、媒体处理平台(MPP)实战、AI算法集成与系统调优等全流程攻略,帮助开发者高效构建稳定可靠的视觉AI产品。

weixin_29269441的博客 1269

elasticsearch 基础 —— Query String

使用查询解析器来解析其内容的查询。下面是一个例子: GET /_search { "query": { "query_string" : { "default_field" : "content", "query" : "this AND that OR thus" } } } query

gmHappy 3万+

嵌入式系统存储体系

主要分为三种:高速缓存(cache)、主存和外存。

weixin_43586667的博客 2273

VL53L0X加盖玻片后精度暴跌?手把手教你完成串扰校准(CrossTalk Calibration)

本文详细解析了VL53L0X激光测距传感器加盖玻片后精度下降的原因,并提供了串扰校准(CrossTalk Calibration)的完整解决方案。通过分步指导和实用代码示例,帮助工程师恢复毫米级测距精度,适用于智能门锁、扫地机器人等应用场景。

weixin_33708432的博客 553

Elasticsearch(四):query_string查询介绍

介绍了查询字符串(query_string)语法,并结合一些高级查询展示了查询字符串语法的使用。如果你觉得“查询字符串”这种叫法有些奇怪,大可不必,因为这完全是安装query_string译过来的。

欧阳方超的专栏 2375

YOLO系列算法改进 | C3k2改进篇 | 融合DEConv细节增强卷积模块 | 细节增强+多分支差分卷积融合,标准卷积的完美涨点替代! | TIP 2024

YOLO系列算法改进 | C3k2改进篇 | 融合DEConv细节增强卷积模块 | 细节增强+多分支差分卷积融合,标准卷积的完美涨点替代! | TIP 2024 (以YOLO26最新目标检测模型为基础,融合C3k2_DEConv创新模块进行改进)

2502_91881744的博客 326

Elasticsearch 基本使用(五)查询条件匹配方式(query &amp; query_string)_es query_string

ES中常用的查询类型往大了分可以分为简单查询,复合查询,聚合查询等;而复合查询及聚合查询都是基于简单查询的;简单查询里面对条件的匹配方式又分为不同类型。term[s],match,match_all,match_phrase 等等。

2501_90193504的博客 1090

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

vb 6.0并口开发操作实例

vb6并口开发,简体中文,试例内有inpout32.dll动态链接库

Elasticsearch Query String

1)、数据准备 https://blog.csdn.net/qq_36918149/article/details/104226994 2)、演示数据添加 PUT /users/_doc/1 { "name":"Ruan Yiming", "about":"java, golang, node, swift, elasticsearch" } PUT /users/_doc/2 { ...

希望我的博客,能解决你工作中的问题 2605

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

es elasticsearch 中的 query string query 用法

_exists 号代表不存在,

m0_67402096的博客 1285

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

ElasticSearch(7.2.2)-es之query_string查询

简介:⼿把⼿教你es之query_string查询 介绍 query_string 查询,如果熟悉lucene的查询语法,我们可以直接⽤lucene查询语法写⼀个查询串进⾏查询,ES中接到请求后,通过查询解析器,解析查询串⽣成对应的查询。 指定单个字段查询 POST /nba/_search { "query": { "query_string": { "default_fie...

逍遥云恋 4820

Elasticsearch: Query string与Simple query string

Elasticsearch Query String

qq_39198749的博客 1632

es - elasticsearch search - DSL - simple_query_string

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。 问:simple_query_string有什么特点? 答: 问:simple_query_string如何使用? 答: # simple_query_string PUT /simple_query_string_test { "mappings": { "properties": { "name": {"type": "text"} } } } # 索引 POST /sim.

我的博客 409

【Elasticsearch】simple_query_string

与`query_string`查询相比,`simple_query_string`查询的一个重要特点是其容错性。可以使用通配符(如`*`)表示多个字段,并可以通过`^`符号为特定字段提升权重(例如`title^5`)。• 默认操作符:默认操作符的选择(`OR`或`AND`)会对查询结果产生显著影响。例如,`+foo +bar`表示文档必须同时包含`foo`和`bar`。例如,`foo -bar`表示文档包含`foo`但不包含`bar`。例如,`foo | bar`表示文档包含`foo`或`bar`。

risc123456的博客 788

Elasticsearch:理解 query_string 和 simple_query_string 查询

针对很多的开发者来说,如果你不是很熟悉 DSL 查询,那么在有些情况下,query_string 及 simple_query_string 变得非常灵活及方便。在今天的文章中,我来比较一下这两种查询的方法。

Elastic 中国社区官方博客 3839

es - elasticsearch search - DSL - query_string - 初识

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。 问:query_string有什么特点? 答: 问:query_string 如何使用? 答: # query_string PUT /query_string_test { "mappings": { "properties": { "name": {"type": "text"} } } } # 索引 POST /query_string_test/_doc/1 { ".

我的博客 741

es query string

1、query string基础语法 GET /test_index/test_type/_search?q=test_field:test GET /test_index/test_type/_search?q=+test_field:test GET /test_index/test_type/_search?q=-test_field:test 2、_all metadata的原理和作...

keyuquan的博客 2578

ES教程深度剖析:使用query_string进行动态搜索

深入讲解如何在es教程中利用query_string进行灵活的动态搜索,掌握核心语法与实际应用场景,提升检索效率与查询表达能力。

weixin_29301059的博客 1073
上一篇: Elasticsearch _all字段
下一篇: 用logstash发邮件告警
笑天居士
博客等级 码龄24年 291粉丝 136原创
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值
⌂ 换网址