带有过滤器的 Elasticsearch bool 查询不适用于带有“-”的字段

时间:2021-02-11 10:19:20

标签: elasticsearch

我的每个客户的索引中有多个文档:

{"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"

我没有得到结果。

当我从 customer 字段中删除 '-' 字符时,我得到了包括 m-test-service 文档在内的结果。

为什么这个字符有问题?

2 个答案:

答案 0 :(得分:2)

术语查询返回在提供的字段中包含确切术语的文档。

如果您想搜索 "customer":"m-test-service",请尝试使用 customer.keyword 字段。这使用关键字分析器而不是标准分析器

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

答案 1 :(得分:1)

看起来您映射中的 customer 字段是 text,它在中断文本时使用 standard 分析器,如果它是动态生成的,则应使用 .keyword 字段,否则使用 multi-field 你应该添加它并查询它。

相关问题