V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
herozzm
V2EX  ›  Elasticsearch

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

  •  
  •   herozzm · 2019-10-22 11:08:53 +08:00 · 4766 次点击
    这是一个创建于 1619 天前的主题,其中的信息可能已经有所发展或是发生改变。

    es 中有两条数据, 字段 item

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

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

    {
      "query": {
        "bool": {
          "filter": {
            "bool": {
              "should": [
                {
                  "match_phrase": {
                    "item": {
                      "query": "制服"
                    }
                  }
                }
              ]
            }
          },
          "must_not": [
            {
              "match_phrase": {
                "item": {
                  "query": "制服务"
                }
              }
            }
          ]
        }
      }
    
    第 1 条附言  ·  2019-10-22 11:40:02 +08:00
    是 item 字段弄成了最细分词导致的,换成最粗的 ik 分词就好了
    8 条回复    2019-10-22 11:40:25 +08:00
    Memento
        1
    Memento  
       2019-10-22 11:14:06 +08:00
    应该可以添加中文分词字段, 然后查询该字段;
    yuikns
        2
    yuikns  
       2019-10-22 11:16:50 +08:00
    should 和 must_not 同层
    herozzm
        3
    herozzm  
    OP
       2019-10-22 11:19:06 +08:00
    @yuikns 也不行
    ```
    {
    "query": {
    "bool": {
    "should": [
    {
    "match_phrase": {
    "item": {
    "query": "制服"
    }
    }
    }
    ],
    "must_not": [
    {
    "match_phrase": {
    "item": {
    "query": "制服务"
    }
    }
    }
    ]
    }
    }
    }
    herozzm
        4
    herozzm  
    OP
       2019-10-22 11:19:49 +08:00
    @Memento 分过词了,这里要精准匹配,所以用了 match_phrase
    airfling
        5
    airfling  
       2019-10-22 11:21:51 +08:00
    should 换成 must,should 不是强制匹配
    herozzm
        6
    herozzm  
    OP
       2019-10-22 11:24:11 +08:00
    @airfling 这里是要用多个词语 or 查询的,所以用 should,不过换成 must 也是一样的效果
    airfling
        7
    airfling  
       2019-10-22 11:36:09 +08:00
    那你有没有试过这个 missing 的查询
    herozzm
        8
    herozzm  
    OP
       2019-10-22 11:40:25 +08:00
    @airfling 是 item 字段分词颗粒度导致的,弄成 ik 的最粗分词就没问题了
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   986 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 20:15 · PVG 04:15 · LAX 13:15 · JFK 16:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.