• ElasticSearch之结构化搜索


    写在前面

    本文看下es的结构化搜索,结构化搜索顾名思义就是对结构化数据的搜索,那么什么是结构话数据呢?我的理解是具有一定规则的数据就是结构化,在es中包括但不限于如下:

    1:Boolean
        只有true和false
    2:整数
        就是整数,不会是其他的
    3:日期
        日期的格式虽然可能有多种,但也是有规则的
    4:枚举类的text
        普通的text肯定不是结构化数据,比如一条日志,但是如性别,风险等级,彩笔的颜色等,都是有限的枚举值,所以也是结构化的数据
    5:文档的key
        是有限的枚举值,所以也是有规则的,所以也是结构化数据。
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    针对上述数据的搜索,我们可以采用term搜索 以及前缀搜索词项查询

    结构化查询在一定程度上可以认为是精准匹配,所以可以根据具体情况考虑不算分

    1:例子

    1.1:布尔,数字

    # 1:删除老的,避免影响
    DELETE /products
    POST /products/_bulk
    {"index": {"_id": 1}}
    {"price":10,"available":true,"date":"2018-01-01","productID":"XHDK-A-1293-#fJ3"}
    {"index": {"_id": 2}}
    {"price":20,"available":true,"date":"2019-01-01","productID":"KDKE-B-9947-#kL5"}
    {"index": {"_id": 3}}
    {"price":30,"available":true,"productID":"JODL-X-1937-#pV7"}
    {"index": {"_id": 4}}
    {"price":30,"available":false,"productID":"QQPX-R-3956-#aD8"}
    
    # 2:对布尔查询,并且算分,可以查询到3条
    POST products/_search
    {
      "profile": "true",
      "explain": true,
      "query": {
        "term": {
          "available": true
        }
      }
    }
    # 3:对布尔查询,不算分,可以查询到3条
    POST products/_search
    {
        "profile": "true",
        "explain": true,
        "query": {
            "constant_score": {
                "filter": {
                    "term": {
                        "available": true
                    }
                }
            }
        }
    }
    
    # 4:数字range
    POST products/_search
    {
        "query": {
            "constant_score": {
                "filter": {
                    "range": {
                        "price": {
                            "gte": 20,
                            "lte": 30
                        }
                    }
                }
            }
        }
    }
    
    • 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
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55

    1.2:日期

    # 1:删除老的,避免影响
    DELETE /products
    POST /products/_bulk
    {"index": {"_id": 1}}
    {"price":10,"available":true,"date":"2018-01-01","productID":"XHDK-A-1293-#fJ3"}
    {"index": {"_id": 2}}
    {"price":20,"available":true,"date":"2019-01-01","productID":"KDKE-B-9947-#kL5"}
    {"index": {"_id": 3}}
    {"price":30,"available":true,"productID":"JODL-X-1937-#pV7"}
    {"index": {"_id": 4}}
    {"price":30,"available":false,"productID":"QQPX-R-3956-#aD8"}
    
    # 2:日期range
    POST products/_search
    {
        "query": {
            "constant_score": {
                "filter": {
                    "range": {
                        "date": {
                            "gte": "now-7y"
                        }
                    }
                }
            }
        }
    }
    
    • 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

    日期的语法糖
    在这里插入图片描述

    1.3:Exist

    # 1:删除老的,避免影响
    DELETE /products
    POST /products/_bulk
    {"index": {"_id": 1}}
    {"price":10,"available":true,"date":"2018-01-01","productID":"XHDK-A-1293-#fJ3"}
    {"index": {"_id": 2}}
    {"price":20,"available":true,"date":"2019-01-01","productID":"KDKE-B-9947-#kL5"}
    {"index": {"_id": 3}}
    {"price":30,"available":true,"productID":"JODL-X-1937-#pV7"}
    {"index": {"_id": 4}}
    {"price":30,"available":false,"productID":"QQPX-R-3956-#aD8"}
    
    # 2:exists,必须包含date key并且有值才行(null和空字符串都不可以)
    POST products/_search
    {
        "query": {
            "constant_score": {
                "filter": {
                    "exists": {
                        "field": "date"
                    }
                }
            }
        }
    }
    
    • 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

    1.4:多值字段

    # 1:删除老的,避免影响
    DELETE movies
    POST /movies/_bulk
    {"index": {"_id": 1}}
    {"title":"Father of the Bridge Part II","year":1995,"genre":"Comedy"}
    {"index": {"_id": 2}}
    {"title":"Dave","year":1993,"genre":["Comedy","Romance"]}
    
    # 2:会查询出{"_id": 2}的数据,它是个数组,其实不应该查出来
    POST movies/_search
    {
      "query": {
        "constant_score": {
          "filter": {
            "term": {
              "genre.keyword": "Comedy"
            }
          }
        }
      }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    在这里插入图片描述
    上述的查询,将"_id": 2}的数据的数据也查询出来了,其实这是不对的,是es对数组的实现机制导致了这个问题,该怎么办呢?

    写在后面

    其实结构化搜索的内容是包含在我们前面学习过的这篇文章 中的,只不过这里为了更加明确结构化的概念又进行了专门的的分析。

    参考文章列表

    ElasticSearch之search API

  • 相关阅读:
    计算机毕业设计Python+django的家政管理系统设计(源码+系统+mysql数据库+Lw文档)
    NI USRP RIO软件无线电
    常用meta整理
    2024最新华为OD算法题目
    谈谈互联网免费思维
    数据采集代码示例
    使用 Kubeadm 部署 Kubernetes(K8S) 安装
    “一个有趣的C语言代码”分析
    【计算机网络 - 第六章】链路层
    SimpleITK使用——4. 奇怪的问题
  • 原文地址:https://blog.csdn.net/wang0907/article/details/136227554