• es操作入门到高级


    1.索引

    1. 创建索引:

      put /索引名称?pretty
      
      • 1
    2. 查询索引:

      get _cat/indices?v
      
      • 1
    3. 删除索引

      delete /index_name?pretty
      
      • 1

    2.数据

    2.1 插入数据

    PUT /index/_doc/id
    {
        Json数据
    }
    示例数据:
    PUT /product/_doc/1
    {
        "name" : "xiaomi phone",
        "desc" :  "shouji zhong de zhandouji",
        "price" :  3999,
        "tags": [ "xingjiabi", "fashao", "buka" ]
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2.2. 更新数据

    • 全量更新:使用put关键字,和插入数据格式相同,会完全替换掉原始数据(某些字段不传,就更新成空了)

    • 更新字段:

      post /索引名称/_doc/id/_uodate
      POST /product/_doc/1/_update
      {
          "doc": {
              "price": 2999
          }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7

    2.3. 查询数据

    2.3.1 入门查询
    1. 查询所有

      方式一:
      get /product/_doc/_search
      
      方式二:
      get /product/_search
      {
        "query": {
          "match_all": {}
        }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
    2. 根据id查询

      GET /product/_doc/1
      
      • 1
    2.3.2 基本查询
    2.3.2.1 Query_String

    分页:

    get /product/_search from=0&size=2
    
    • 1
    2.3.2.2 Query DSL
    1. match_all:匹配所有

      GET /product/_search
      {
        "query":{
          "match_all": {}
        }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
    2. match:name 中包含nfc

      GET /product/_search
      {
        "query": {
          "match": {
            "name": "nfc"
          }
        }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
    3. sort 排序

      多字段查询。
      query:要查询的字符串
      fields:要查询的字段
      
      GET /product/_search
      {
        "query": {
          "multi_match": {
            "query": "nfc",
            "fields": ["name","desc"]
          }
        },
        "sort": [
          {
            "price": "desc"
          }
        ]
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
    4. _source 元数据:想要查询的多个字段,例子中只查询“name”

      GET /product/_search
      {
        "query":{
          "match": {
            "name": "nfc"
          }
        },
        "_source": ["name"]
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
    5. 分页

      GET /product/_search
      {
        "query":{
          "match_all": {}
        },
        "sort": [
          {
            "price": "asc"
          }
        ], 
        "from": 0,
        "size": 2
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
    2.3.2.3 全文查询
    1. phrase search:短语搜索(match_phrase的分词结果必须在被检索字段的分词中都包含,而且顺序必须相同,默认必须都是连续的

      GET /product/_search
      {
        "query": {
          "match_phrase": {
            "name": "nfc phone"
          }
      
      }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
    2. query-term : 不会被分词

      GET /product/_search
      {
        "query": {
          "term": {
           这里nfc phone 是作为一个整体的,不会进行分词的查找
            "name": "nfc phone" 
          }
        }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
    3. match 和terms 区别

      term:代表的完全匹配,也就是精确查询,搜索前不会再对搜索词进行分词拆解。只能查单个单词。

      terms:匹配多个词,[]中的多个词是或者的关系,只要满足一个次就可以。

      match:match搜索的时候,会先进行分词的拆分,拆完完,再来匹配,属于或的关系,只要有一个词能匹配到,就行。

      //match 匹配一个分词就行;
      //terms是书写的整体作为查找条件。
      库里是按照单个分词存储的
      GET /product/_search
      {
        "query": {
          "bool": {
          //要满足两个词
            "must": [
              {"term":{"name":"nfc"}},
              {"term":{"name":"phone"}}
            ]
          }
        }
      }
      GET /product/_search
      {
        "query": {
          "terms": {
            "name":["nfc","phone"]
          }
        }
      }
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
    2.3.2.4 查询过滤条件

    bool可以组合多个查询条件bool查询也采用more_matches_is_better的机制,因此满足must和should的子句的文档将会合起来并计算分值

    • must,子句必须出现在匹配的文档中,并将有助于得分。包含多个,用[]包裹,
    • filter,过滤器不计算相关度分数,只有“是,否”,用于精确匹配和范围检索。速度快。match的话,确定文档是否是应该成为结果的一部分
    • should,(可能满足or)
    • must_not ,必须不满足
    案例:
    GET /product/_search
    {
      "query": {
    "bool":{
    #name中必须不能包含“erji”
          "must": [
            {"match": { "name": "xiaomi"}}
          ],
    #name中必须包含“xiaomi”
          "must_not": [
            {"match": { "name": "erji"}}
          ],
    #可能满足 desc字段包含nfc
          "should": [
            {"match": {
              "desc": "nfc"
            }}
          ], 
    #筛选价格大于4999的doc
          "filter": [
          #这种可以理解为模板二级位置要放的东西,和上面的match一个意思
            {"range": {
              "price": {
                "gt": 4999   
              }
            }}
          ]
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    2.3.2.5 组合查询

    搜索一台xiaomi nfc phone或者一台满足 是一台手机 并且 价格小于等于2999。

    constant_score 不再计算得分排序了,所有的得分都是一样的。

    GET /product/_search
    {
      "query": {
        "constant_score": {
          "filter": { 
            "bool":{
              "should":[
                {"match_phrase":{"name":"xiaomi nfc phone"}},
                {
                  "bool":{
                    "must":[
                      {"term":{"name":"phone"}},
                      {"range":{"price":{"lte":"2999"}}}
                      ]
                  }
                }
              ]
            }
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    3. mapping

    3.1 index

    是否对创建对当前字段创建索引,默认true,如果不创建索引,该字段不会通过索引被搜索到,但是仍然会在source元数据中展示

    3.2 analyzer

    指定分析器

    3.3 boost

    对当前字段相关度评分权重,默认1

    3.4 coerce(强迫)

    是否运行强制类型转换。true 。

    3.5 copy_to

    我们把 city, country 及 province 三个项合并成为一个项 region,但是这个 region 并不存在于我们文档的 source 里。当我们这么定义我们的 mapping 的话,在文档被索引之后,有一个新的 region 项可以供我们进行搜索

        "city": {
          "type": "keyword",
          "copy_to": "region"
        },
        "country": {
          "type": "keyword",
          "copy_to": "region"      
        },
        "province": {
          "type": "keyword",
          "copy_to": "region"
        },
        "region": {
          "type": "text"
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    假如我们想搜索 country:中国,province:北京 这样的记录的话,我们可以只写如下的一条语句就可以了

    GET twitter/_search 
    {
      "query": {
        "match": {
          "region": {
            "query": "中国 北京",
            "minimum_should_match": 4
          }
        }
      }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    3.6 doc_values

    为了提升排序和聚合效率,默认true,如果确定不需要对字段进行排序和聚合,也不需要通过脚本访问值,可以禁用doc值以节省磁盘空间

    3.7 dynamic(动态的)

    控制是否可以动态添加新字段(在插入消息的时候)。

    1. true,新检测到的字段将添加到映射中(默认)
    2. false,新检测到的字段被忽略。这些字段将不会被索引,因此无法查询,但是仍然回出现再_source返回的匹配项中。这些字段不会添加到映射中,必须显式添加新字段。
    3. strict,如果检测到新字段,回引发异常并拒绝文档。必须将新字段显式添加到映射中
    3.8 eager_global_ordinals

    不应该用于冻结的索引,对于一个冻结索引,全局序数在每次搜索时重建后被丢弃,当请求它们时重新构建,这导致在每次搜索时重新加载全局序数。用于聚合的字段上,优化聚合性能(冻结索引)

    3.9 enable

    是否创建倒排索引,可以对字段操作,也可以对索引操作,

    3.10 fielddate

    4. 高级查询

    4.1 _mget
  • 相关阅读:
    Rust 使用egui创建一个简单的下载器demo
    CV每日论文--2024.6.4
    Java多线程篇(2)——mesi与内存屏障与volatile
    ORM 单表记录与字段操作
    利用Nacos作为配置中心动态修改线程池
    【CI/CD】详解自动化开发之CI/CD(持续集成、持续交付、持续部署)
    k8s笔记20--基于 K8S 的 cicd 概述
    【正点原子STM32连载】第五章 STM32基础知识入门 摘自【正点原子】MiniPro STM32H750 开发指南_V1.1
    关于 passing ‘const xx’ as ‘this’ argument of 的错误
    Android 事件分发介绍
  • 原文地址:https://blog.csdn.net/sbl19940819/article/details/127793456