es 的 match_phrase 如何排除不需要的结果,发现用 must_not 无法实现结果

2019-10-22 11:08:53 +08:00
 herozzm

es 中有两条数据, 字段 item

  1. 编制服务
  2. 工厂制服

想通过“match_phrase”搜索“制服”得到第 2 条数据,排除第 1 条数据,以下语句却能得到 2 条数据,请问怎么写才能达到目的?

{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "should": [
            {
              "match_phrase": {
                "item": {
                  "query": "制服"
                }
              }
            }
          ]
        }
      },
      "must_not": [
        {
          "match_phrase": {
            "item": {
              "query": "制服务"
            }
          }
        }
      ]
    }
  }
4811 次点击
所在节点    Elasticsearch
8 条回复
Memento
2019-10-22 11:14:06 +08:00
应该可以添加中文分词字段, 然后查询该字段;
yuikns
2019-10-22 11:16:50 +08:00
should 和 must_not 同层
herozzm
2019-10-22 11:19:06 +08:00
@yuikns 也不行
```
{
"query": {
"bool": {
"should": [
{
"match_phrase": {
"item": {
"query": "制服"
}
}
}
],
"must_not": [
{
"match_phrase": {
"item": {
"query": "制服务"
}
}
}
]
}
}
}
herozzm
2019-10-22 11:19:49 +08:00
@Memento 分过词了,这里要精准匹配,所以用了 match_phrase
airfling
2019-10-22 11:21:51 +08:00
should 换成 must,should 不是强制匹配
herozzm
2019-10-22 11:24:11 +08:00
@airfling 这里是要用多个词语 or 查询的,所以用 should,不过换成 must 也是一样的效果
airfling
2019-10-22 11:36:09 +08:00
那你有没有试过这个 missing 的查询
herozzm
2019-10-22 11:40:25 +08:00
@airfling 是 item 字段分词颗粒度导致的,弄成 ik 的最粗分词就没问题了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/611677

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX