首页 > 解决方案 > 弹性 match_phrase_prefix 设置单词顺序

问题描述

无法找到一种干净的方式来执行这种类型的查询——有没有办法指定这些前缀只返回带有单词前缀的结果?这样一个有效的回报将是 Alpha Beta 并且 Beta Alpha 不会返回?

"query": {
"bool": {
  "must": [
    {
      "match_phrase_prefix": {
        "search_name": "Al"
      }
    },
    {
      "match_phrase_prefix": {
        "search_name": "Be"
      }
    }
  ]
}

},

标签: elasticsearchnosqlmatch-phrase

解决方案


如果您已索引keyword该字段的 -type 版本,则可以执行以下操作:

"query": {
   "bool": {
     "must": [
       {
         "match_phrase_prefix": {
           "search_name.keyword": "Al"
         }
       },
       {
         "match_phrase_prefix": {
           "search_name.keyword": "Be"
         }
       }
     ]
   }
}

推荐阅读