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)


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM