Elasticsearch 结构化搜索

时间:2019-12-11
本文章向大家介绍Elasticsearch 结构化搜索,主要包括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"
        }
      }
    }
  }
}



参考资料:
-自定义字段映射
-no"query" registered for "filtered"
-no"query" registered for "missing"
-Exists Query文档

原文地址:https://www.cnblogs.com/linkworld/p/12024518.html