美文网首页ElasticSearch实战笔记
20、ElasticSearch 7.x 的 bool搜索,te

20、ElasticSearch 7.x 的 bool搜索,te

作者: 众神开挂 | 来源:发表于2020-04-05 08:00 被阅读0次

    主要内容:

    1、bool搜索实战

    bool相当于SQL中的多个and条件,bool可以嵌套,与must,must_not,should,组合多个过滤条件

    1.1、bool搜索实战01

    搜索发帖1日期为2017-01-01,或者帖子ID为XHDK-A-1293-#fJ3的帖子,同时要求帖子的发帖日期绝对不为2017-01-02

    GET /forum/_search
    {
      "query": {
        "constant_score": {
          "filter": {
            "bool": {
              "should": [
                {"term": { "postDate": "2017-01-01" }},
                {"term": {"articleID": "XHDK-A-1293-#fJ3"}}
              ],
              "must_not": {
                "term": {
                  "postDate": "2017-01-02"
                }
              }
            }
          }
        }
      }
    }
    

    must:必须匹配,

    should:可以匹配其中任意一个即可,

    must_not :必须不匹配

    1.2、bool搜索实战02

    搜索帖子ID为XHDK-A-1293-#fJ3,或者是帖子ID为JODL-X-1937-#pV7而且发帖日期为2017-01-01的帖子

    GET /forum/_search
    {
      "query": {
        "constant_score": {
          "filter": {
            "bool": {
              "should": [
                {
                  "term": {
                    "articleID": "XHDK-A-1293-#fJ3"
                  }
                },
                {
                  "bool": {
                    "must": [
                      {
                        "term": {
                          "articleID": "JODL-X-1937-#pV7"
                        }
                      },
                      {
                        "term": {
                          "postDate": "2017-01-01"
                        }
                      }
                    ]
                  }
                }
              ]
            }
          }
        }
      }
    }
    

    2、使用terms搜索多个值以及多值搜索结果优化

    terms使用类似sql中的where ... in()方法

    select * from tbl where col in ("value1", "value2")

    2.1、为帖子数据增加tag字段
    POST /forum/_bulk
    { "update": { "_id": "1"} }
    { "doc" : {"tag" : ["java", "hadoop"]} }
    { "update": { "_id": "2"} }
    { "doc" : {"tag" : ["java"]} }
    { "update": { "_id": "3"} }
    { "doc" : {"tag" : ["hadoop"]} }
    { "update": { "_id": "4"} }
    { "doc" : {"tag" : ["java", "elasticsearch"]} }
    
    2.2、实战演练

    案例01:搜索articleID为KDKE-B-9947-#kL5或QQPX-R-3956-#aD8的帖子

    GET /forum/_search 
    {
      "query": {
        "constant_score": {
          "filter": {
            "terms": {
              "articleID": [
                "KDKE-B-9947-#kL5",
                "QQPX-R-3956-#aD8"
              ]
            }
          }
        }
      }
    }
    

    案例02: 搜索tag中包含java的帖子

    GET /forum/_search
    {
      "query": {
        "constant_score": {
          "filter": {
            "terms": {
              "tag": [
                "java"
              ]
            }
          }
        }
      }
    }
    
    2.3、优化搜索结果,仅仅搜索tag只包含java的帖子

    添加新的字段

    POST /forum/_bulk
    { "update": { "_id": "1"} }
    { "doc" : {"tag_cnt" : 2} }
    { "update": { "_id": "2"} }
    { "doc" : {"tag_cnt" : 1} }
    { "update": { "_id": "3"} }
    { "doc" : {"tag_cnt" : 1} }
    { "update": { "_id": "4"} }
    { "doc" : {"tag_cnt" : 2} }
    

    使用_cnt限制返回的tag匹配的情况,tag_cnt表示索引原字段包含值的个数

    GET /forum/_search
    {
      "query": {
        "constant_score": {
          "filter": {
            "bool": {
              "must": [
                {
                  "term": {"tag_cnt": 1}
                },
                {
                  "terms": {"tag": ["java"]}
                }
              ]
            }
          }
        }
      }
    }
    

    3、基于range filter来进行范围过滤

    使用range做范围过滤:

    3.1、为帖子数据增加浏览量的字段
    POST /forum/_bulk
    { "update": { "_id": "1"} }
    { "doc" : {"view_cnt" : 30} }
    { "update": { "_id": "2"} }
    { "doc" : {"view_cnt" : 50} }
    { "update": { "_id": "3"} }
    { "doc" : {"view_cnt" : 100} }
    { "update": { "_id": "4"} }
    { "doc" : {"view_cnt" : 80} }
    
    3.2、搜索浏览量在30~60之间的帖子
    GET /forum/_search
    {
      "query": {
        "constant_score": {
          "filter": {
            "range": {
              "view_cnt": {
                "gt": 30,
                "lt": 60
              }
            }
          }
        }
      }
    }
    
    3.3、搜索发帖日期在最近1个月的帖子

    插入一条数据,日期写到最近几天

    POST /forum/_bulk
    { "index": { "_id": 5 }}
    { "articleID" : "DHJK-B-1395-#Ky5", "userID" : 3, "hidden": false, "postDate": "2020-03-27", "tag": ["elasticsearch"], "tag_cnt": 1, "view_cnt": 10 }
    

    查询方法:

    两种查询方法

    GET /forum/article/_search 
    {
      "query": {
        "constant_score": {
          "filter": {
            "range": {
              "postDate": {
                "gt": "now-30d"   ## 大于现在时间减30天
              }
            }
          }
        }
      }
    }
    

    第二种查询方法:(假设今天是2020年3月10号)

    GET /forum/_search 
    {
      "query": {
        "constant_score": {
          "filter": {
            "range": {
              "postDate": {
                "gt": "2020-03-10||-30d"
              }
            }
          }
        }
      }
    }
    

    相关文章

      网友评论

        本文标题:20、ElasticSearch 7.x 的 bool搜索,te

        本文链接:https://www.haomeiwen.com/subject/ueokuhtx.html