簡體   English   中英

Elasticsearch 帶有過濾器的布爾查詢不適用於帶有“-”的字段

[英]Elasticsearch bool query with filter doesn't work on field with “-”

每個客戶的索引中有多個文檔:

{"customer":"m-test-service", "customer_id":"x55sg" "book":"f","date"....}
{"customer":"m-test-service", "customer_id":"x55sg" "book":"g","date"....}
{"customer":"x12", "customer_id":"dhb5" "book":"e","date"....}
{"customer":"x12", "customer_id":"dhb5" "book":"d","date"....}

我想檢索所有將 customer 和 customer_id 設置為某些兩個特定值的文檔,例如以下 SQL 查詢: SELECT * FROM TABLE WHERE CUSTOMER='XXX' AND CUSTOMER_ID="YYYY"

我一直在使用以下查詢:

GET my_index/_search
{
  "query":{
    "bool":{
      "filter" : [
          {"term" : {"customer" : "x12"} },
          {"term" : {"customer_id" : "dhb5"}}
        ]
    
    }
  }
}

但是,當我嘗試查詢字符串中包含"-"的客戶時,我沒有得到任何文檔。 我嘗試使用以下值運行前面提到的查詢:

"customer":"m-test-service", "customer_id":"x55sg"

我沒有得到結果。

當我從客戶字段中刪除'-'字符時,我得到的結果包括 m-test-service 的文檔。

為什么這個字符有問題?

術語查詢返回在提供的字段中包含確切術語的文檔。

如果要搜索"customer":"m-test-service" ,請嘗試使用customer.keyword字段。 這使用關鍵字分析器而不是標准分析器

{
  "query":{
    "bool":{
      "filter" : [
          {"term" : {"customer.keyword" : "m-test-service"} },
          {"term" : {"customer_id" : "x55sg"}}
        ]
    
    }
  }
}

看起來您的映射中的customer字段是使用standard分析器同時破壞text的文本,如果它是動態生成的,則應使用.keyword字段,否則應使用多字段添加並查詢它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM