【问题标题】:elasticsearch, find exact phrase in a fieldelasticsearch,在字段中找到确切的短语
【发布时间】:2018-11-28 11:17:09
【问题描述】:

我想在单个字段中搜索确切的短语.. 这是我的方法:

"query": {
  "match_phrase": {                                             
    "word": "developer"
  }
}

但关键是这个查询会找到任何有这个关键字developer的文档:

点赞"word": "developer""word": "php developer"

如何创建一个查询,当我搜索 developer 时只返回 "word": "developer" doc,

当我搜索php developer 时,返回"word": "php developer" doc

谢谢

【问题讨论】:

  • 您是否可以接受比其他匹配更好地对精确匹配进行排名的解决方案?即搜索“developer”将首先返回“developer”,然后才返回“php developer”,而搜索“php developer”将首先返回“php developer”,然后才返回其他“developer”
  • 顺便说一句,如果你用match_phrase找到php developer,你应该不会得到developer

标签: elasticsearch exact-match


【解决方案1】:

简单来说,如果您的字段wordkeyword 类型,那么您可以使用Term Query,如下所示:

POST <your_index_name>/_search
{
  "query": {
    "term" : { "word" : "developer" } 
  }
}

如果您只有word 类型为text,我建议您将其keyword 字段添加为multi-fields,这样您就可以使用word 进行文本匹配和@987654330 @ 用于精确匹配。

PUT <your_index_name>
{
  "mappings": {
    "_doc": {
      "properties": {
        "word": {
          "type": "text",
          "fields": {
            "keyword": { 
              "type":  "keyword"
            }
          }
        }
      }
    }
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-10
    • 1970-01-01
    相关资源
    最近更新 更多