当前位置: 首页 > article >正文

使用ElasticSearch查询

从一个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.

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.kler.cn/a/472321.html

相关文章:

  • 【Arm】Arm 处理器的半主机(semihosting)机制
  • Linux服务器网络不通问题排查及常用命令使用
  • 成为LabVIEW自由开发者
  • Numpy数组的属性
  • WebRTC 在视频联网平台中的应用:开启实时通信新篇章
  • Android NDK开发实战之环境搭建篇(so库,Gemini ai)
  • Redis的内存预分配策略
  • Ungoogled Chromium127 编译指南 MacOS篇(六)- 获取源代码
  • 用Rust构建高性能WebAssembly模块:性能调优与实际案例
  • 【网络安全技术与应用】(选修)实验4 网络扫描
  • android 启动页倒计时页面编写
  • 【Qt】QtConcurrent
  • 【UE5 C++课程系列笔记】21——弱指针的简单使用
  • 回归预测 | MATLAB实ELM-Adaboost多输入单输出回归预测
  • Mono里运行C#脚本23—mono_jit_exec
  • Python 批量生成Word 合同
  • xss-labs(level11-20)【通关技巧】
  • el-table 使用el-form 表单验证
  • STM32学习(十)
  • 嵌入式入门Day38
  • Android Process 问题:NoSuchMethodError,No static method myProcessName()
  • HTML5实现好看的博客网站、通用大作业网页模板源码
  • 第19章 数据库备份与恢复
  • 基于单片机的观赏类水草养殖智能控制系统的设计(论文+源码)
  • 采用标准化的方式开展设计-研发中运用设计模式
  • 中国科技统计年鉴EXCEL版(2021-2023年)-社科数据