zoukankan      html  css  js  c++  java
  • Elasticsearch 结构化搜索

    1,精确值查找

    1.1 term查询

    • 用于查询数字(numbers),布尔值(Booleans),日期(dates),文本(text)
    // 1, 自定义字段映射
    
    PUT /my_store
    {
        "mappings": {
            "products": {
                "properties":{
                    "productID": {
                        "type":"keyword"
                    }
                }
            }
        }
    }
    
    // 2,初始化数据
    POST /my_store/products/_bulk
    { "index": { "_id": 1 }}
    { "price" : 10, "productID" : "XHDK-A-1293-#fJ3" }
    { "index": { "_id": 2 }}
    { "price" : 20, "productID" : "KDKE-B-9947-#kL5" }
    { "index": { "_id": 3 }}
    { "price" : 30, "productID" : "JODL-X-1937-#pV7" }
    { "index": { "_id": 4 }}
    { "price" : 30, "productID" : "QQPX-R-3956-#aD8" }
    
    
    // 2,以非评分模式(constant_score)执行 term 查询
    GET /my_store/products/_search
    {
      "query": {
        "constant_score": {
          "filter": {
            "term": {
              "price": 20
            }
          }
        }
      }
    }
    
    // SQL 形式表达
    SELECT document
    FROM products
    WHERE price = 20
    
    // 3, 查询 productID
    // 备注:如果不设置“自定义字段映射”,“productID”会被拆分成多个token,
    // analyze 分析索引
    GET /my_store/_analyze
    {
        "field":"productID",
        "text":"XHDK-A-1293-#fJ3"
    }
    

    2, 组合过滤器(compound filter)

    // 形式:
    {
        "bool":{
            "must":[],    // 与AND等价
            "should":[],   // 与NOT等价
            "must_not":[]  // 与OR等价
        }
    }
     
    
    // SQL 表达形式
    SELECT product
    FROM   products
    WHERE  (price = 20 OR productID = "XHDK-A-1293-#fJ3")
      AND  (price != 30)
    
    
    // 组合过滤器
    GET /my_store/products/_search
    {
      "query":{
            "bool":{
              "should":[
                {"term":{"price":20}},
                {"term":{"productID":"XHDK-A-1293-#fJ3"}}
                ],
                "must_not":{
                  "term":{"price":30}
                }
            }
          }
    }
    
    
    // 嵌套布尔过滤器
    SELECT document
    FROM   products
    WHERE  productID      = "KDKE-B-9947-#kL5"
      OR (     productID = "JODL-X-1937-#pV7"
           AND price     = 30 )
    
    GET /my_store/products/_search
    {
      "query":{
            "bool":{
              "should":[
                {"term":{"productID":"KDKE-B-9947-#kL5"}},
                {
                  "bool":{
                    "must":[
                      {"term":{"productID":"JODL-X-1937-#pV7"}},
                      {"term":{"price":30}}]
                  }
                }
                ]
            }
          }
    }
    

    3, 查找多个精确值

    // 查找价格字段值为 $20 或 $30
    GET /my_store/products/_search
    {
      "query":{
            "constant_score": {
              "filter": {
                "terms":{
                  "price":[20,30]
                }}
            }
      }
    }
    

    3.1 范围

    // 查询价格大于$20且小于$40美元的产品
    
    GET /my_store/products/_search
    {
      "query":{
            "constant_score": {
              "filter": {
                "range":{
                  "price":{
                    "gte":20,
                    "lt":40
                  }
                }}
            }
      }
    }
    

    4. 处理NULL值

    // 初始化数据
    POST /my_index/posts/_bulk
    { "index": { "_id": "1"              }}
    { "tags" : ["search"]                }  
    { "index": { "_id": "2"              }}
    { "tags" : ["search", "open_source"] }  
    { "index": { "_id": "3"              }}
    { "other_field" : "some data"        }  
    { "index": { "_id": "4"              }}
    { "tags" : null                      }  
    { "index": { "_id": "5"              }}
    { "tags" : ["search", null]          }  
    
    

    4.1 存在查询(exists)

    // SQL 形式
    SELECT tags
    FROM   posts
    WHERE  tags IS NOT NULL
    
    // exists 查询
    GET /my_index/posts/_search
    {
      "query": {
        "constant_score": {
          "filter": {
            "exists": {"field": "tags"}
          }
        }
      }
    }
    

    4.2 缺失查询(missing)

    // SQL 形式
    SELECT tags
    FROM   posts
    WHERE  tags IS NULL
    
    
    // missing 查询
    GET /my_index/posts/_search
    {
      "query": {
        "bool":{
          "must_not":{
            "exists":{
              "field":"tags"
            }
          }
        }
      }
    }
    

    **参考资料:** -[自定义字段映射](https://www.elastic.co/guide/cn/elasticsearch/guide/current/mapping-intro.html#custom-field-mappings) -[no"query" registered for "filtered"](https://stackoverflow.com/questions/40519806/no-query-registered-for-filtered) -[no"query" registered for "missing"](https://stackoverflow.com/questions/37217663/no-query-registered-for) -[Exists Query文档](https://www.elastic.co/guide/en/elasticsearch/reference/5.5/query-dsl-exists-query.html#_literal_missing_literal_query)
  • 相关阅读:
    南邮NOJ Counter Attack
    南邮NOJ 计算机基础知识大赛4
    南邮NOJ 计算机基础知识大赛4
    南邮NOJ 计算机基础知识大赛4
    南邮NOJ 计算机基础知识大赛4
    南邮NOJ DDKFC
    南邮NOJ DDKFC
    南邮NOJ DDKFC
    南邮NOJ没有被接待的童鞋
    【POJ】2828 Buy Tickets
  • 原文地址:https://www.cnblogs.com/linkworld/p/12024518.html
Copyright © 2011-2022 走看看