【问题标题】:Elastic Search Rails find partial record with idElastic Search Rails 找到带有 id 的部分记录
【发布时间】:2015-06-29 20:16:24
【问题描述】:

我正在尝试使用 Rails 和 Elastic Search 使用 elasticsearch-rails gem 实现自动完成。

假设我有以下记录:

[{id: 1, name: "John White"}, 
 {id:2, name: "Betty Johnson"}]

在搜索“John”时,我可以使用哪种弹性搜索方法来返回两条记录。

自动完成只会返回“John White”,并且不会返回 id:1。

【问题讨论】:

    标签: elasticsearch elasticsearch-rails


    【解决方案1】:

    一种方法是使用edgeNgram filter:

    PUT office
    {
      "settings": {
        "analysis": {
          "analyzer": {
            "default_index":{
              "type":"custom",
              "tokenizer":"standard",
              "filter":["lowercase","edgeNgram_"]
            }
          },
          "filter": {
            "edgeNgram_":{
              "type":"edgeNgram",
              "min_gram":"2",
              "max_gram":"10"
            }
          }
        }
      },
      "mappings": {
        "employee":{
          "properties": {
            "name":{
              "type": "string"
            }
          }
        }
      }
    }
    
    PUT office/employee/1
    {
      "name": "John White"
    }
    PUT office/employee/2
    {
      "name": "Betty Johnson"
    }
    GET office/employee/_search
    {
      "query": {
        "match": {
          "name": "John"
        }
      }
    }
    

    结果是:

    {
       "took": 5,
       "timed_out": false,
       "_shards": {
          "total": 5,
          "successful": 5,
          "failed": 0
       },
       "hits": {
          "total": 2,
          "max_score": 0.19178301,
          "hits": [
             {
                "_index": "office",
                "_type": "employee",
                "_id": "1",
                "_score": 0.19178301,
                "_source": {
                   "name": "John White"
                }
             },
             {
                "_index": "office",
                "_type": "employee",
                "_id": "2",
                "_score": 0.19178301,
                "_source": {
                   "name": "Betty Johnson"
                }
             }
          ]
       }
    }
    

    【讨论】:

    • 非常感谢!我认为我最终得到了这个工作。唯一的问题是如果您搜索“John Black”会怎样。为什么会返回结果?有什么办法可以排除这样的不匹配?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-28
    • 2020-12-20
    • 1970-01-01
    • 2018-04-09
    • 1970-01-01
    • 1970-01-01
    • 2020-06-26
    相关资源
    最近更新 更多