使用ElasticSearch查询

embedded/2025/1/8 23:18:27/

从一个query body开始

{"query": {"bool": {"disable_coord": true,"must": [{"match": {"enabled": "1"}},{"range": {"effectTime": {"lt": "2017-06-13 13:33:54"}}},{"range": {"expireTime": {"gt": "2017-06-13 13:33:54"}}}],"should": [{"match": {"location": {"query": "北京","boost": 2.7617}}}]}},"size": 50
}
  • bool

A query that matches documents matching boolean combinations of other queries.

  • must

The clause must appear in matching documents and will contribute to the score.

  • should

The clause should appear in the matching document.

If the bool query is in a query context and has a must or filter clause then a document will match the bool query even if none of the should queries match. In this case these clauses are only used to influence the score.

If the bool query is a filter context or has neither must or filter then at least one of the should queries must match a document for it to match the bool query. This behavior may be explicitly controlled by settings the minimum_should_match parameter.

  • disable_coord

Suppose you have a should clause in which you have three queries now suppose one document matches first bool query then it will get some score on that but suppose this query do not exactly match second query but partially matches, now this document will be given some little bit score extra that means (first query match score + second query partial match score).

Now if u do not want this partial score to be given in your query then you should write “disable_coord”: true what it will do it will only give score to the document according to the exactly match query not on the partial match.

must, should

对于must和should,存储数据的形式是一样的。如果有多个查询条件,就放在数组中,如果只有一个查询条件,则可以直接赋值。

  • match
"must|should": [{"match": { "foo": "bar" }},
...

如果包含多个条件,不同条件之间可以使用 operator。Default or.

"must|should": [{"match": { "foo": {"query": "bar", "operator": "and"} }},
...
  • range
"must|should": [{"range": { "someTime": {"lt": "2017-06-13 13:33:54"} }},
...
  • term

The term query finds documents that contain the exact term specified in the inverted index.

  • terms

Filters documents that have fields that match any of the provided terms (not analyzed).

  • boost

A boost parameter can be specified to give this term query a higher relevance score than another query.

elasticsearch/guide/current/term-vs-full-text.html" rel="nofollow">Term v.s. Match

  • term

Queries like the term or fuzzy queries are low-level queries that have no analysis phase. They operate on a single term. A term query for the term Foo looks for that exact term in the inverted index and calculates the TF/IDF relevance _score for each document that contains the term.

It is important to remember that the term query looks in the inverted index for the exact term only; it won’t match any variants like foo or FOO. It doesn’t matter how the term came to be in the index, just that it is. If you were to index [“Foo”,“Bar”] into an exact value not_analyzed field, or Foo Bar into an analyzed field with the whitespace analyzer, both would result in having the two terms Foo and Bar in the inverted index.

  • match

Queries like the match or query_string queries are high-level queries that understand the mapping of a field:

If you use them to query a date or integer field, they will treat the query string as a date or integer, respectively.
If you query an exact value (not_analyzed) string field, they will treat the whole query string as a single term.
But if you query a full-text (analyzed) field, they will first pass the query string through the appropriate analyzer to produce the list of terms to be queried.
Once the query has assembled a list of terms, it executes the appropriate low-level query for each of these terms, and then combines their results to produce the final relevance score for each document.

match 全文匹配, term 严格匹配。

其他

  • filter, must_not

这两个关键字和must, should在同一级别。

The filter clause must appear in matching documents. However unlike must the score of the query will be ignored. Filter clauses are executed in filter context, meaning that scoring is ignored and clauses are considered for caching.

The must_not clause must not appear in the matching documents. Clauses are executed in filter context meaning that scoring is ignored and clauses are considered for caching. Because scoring is ignored, a score of 0 for all documents is returned.

参考

Elasticsearch 权威指南(中文版)


http://www.ppmy.cn/embedded/152409.html

相关文章

明源地产ERP VisitorWeb_XMLHTTP.aspx SQL注入漏洞复现

0x01 产品简介 明源地产ERP是一款专门为房地产行业设计的企业资源规划(ERP)系统,旨在帮助房地产企业实现全面的信息化管理,提高运营效率和管理水平。系统涵盖了项目管理、财务管理、供应链管理、客户关系管理(CRM)、人力资源管理等多个核心功能模块,通过整合企业的各个…

docker镜像制作的命令,docker自定义镜像

一.Dockerfile制作镜像的命令1.Dockerfile文件的编写格式FROM //指定基础镜像(唯一)FROM 镜像名:标签USER //启动容器使用的用户,切换用户执行命令(唯一)相当与sudo -u nobody,切换用户执行命令RUN //在容器内执行命令,可以写多条…

【办公类-47-02】20250103 课题资料快速打印(单个docx转PDF,多个pdf合并一个PDF 打印)

背景需求: 2023区级大课题《运用Python优化3-6岁幼儿学习活动材料的实践研究》需要做阶段资料 本来应该2024年6月就提交电子稿和打印稿。可是python学具的教学实验实在太多了,不断生成,我忙着做教学,都没有精力去整理。 2025年…

小程序学习07—— uniapp组件通信props和$emit和插槽语法

目录 一 父组件向子组件传递消息 1.1 props (a)传递静态或动态的 Prop (b)单向数据流 二 子组件通知父组件 2.1 $emit (a)定义自定义事件 (b)绑定自定义事件 三 插槽语法…

MySQL数据结构选择

系列文章目录 一、MySQL数据结构选择 二、MySQL性能优化explain关键字详解 三、MySQL索引优化 文章目录 系列文章目录前言一、索引1.1、什么是索引1.2、构建索引的过程1.3、索引的更新和维护1.4、索引的查询和管理1.5、InnoDB 和 MyISAM 的索引实现1.6、联合索引和最左前缀法则…

【2025软考高级架构师】案例题重点知识——第三部分

33.需求分析总结 需求分析主要是用来分析系统主要做什么,提炼、分析、认真审查获取到的需求,确保所有项目干系人明白其中的含义,同时找出错误、遗漏或者不足的地方。 需求分析的7个方面包括: 1.建立系统边界 2.创建用户界面原型 3.创建数据流图 4.创建数据字典 5.确定…

深入Android架构(从线程到AIDL)_15 应用Android的UI框架02

3、 使用UI线程的MQ(Message Queue) // myView.java // ……… public class myView extends View {// ………Override protected void onDraw(Canvas canvas) {super.onDraw(canvas);// ………// canvas.drawRect(….);invalidate();} } 我们可以透过Message方式来触发UI线程…

PingCAP 连续两年入选 Gartner 云数据库管理系统魔力象限“荣誉提及”

近日,全球 IT 市场研究和咨询公司 Gartner 发布最新报告《Magic Quadrant™ for Cloud Database Management Systems》(云数据库管理系统魔力象限),PingCAP 因其企业级开源分布式数据库 TiDB 在全球市场的表现,连续两年…