简体   繁体   English

组合字段的弹性搜索自动完成/部分匹配

[英]Elastic Search autocomplete/ partial matching for combines fields

I have my data in vehicle index:我在车辆索引中有我的数据:

{
  "mappings": {
    "properties": {
      "@timestamp": {
        "type": "date"
      },
      "@version": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "engine": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "make": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "model": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "year": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      }
    }
  }
}

I'm working on search functionality where users can search with auto-suggestions sourced from ElasticSearch.我正在开发搜索功能,用户可以使用来自 ElasticSearch 的自动建议进行搜索。 Presently I'm using the following query:目前我正在使用以下查询:

{
  "query": {
    "multi_match": {
      "query" : "2014 Iko",
      "fields": [
        "make",
        "model",
        "year",
        "engine"
        ],
      "operator": "and"
    }
  }
}

With the above query I'm able to search results if I type the whole word.通过上面的查询,如果我输入整个单词,我就可以搜索结果。 For eg If a user searches for Ford Mustang, it returns all the possible results (year and engine variations) for a combination of make as Ford and Mustang as model.例如,如果用户搜索福特野马,它会返回所有可能的结果(年份和发动机变化),以福特和野马作为模型的组合。 Problem: The problem is when a user searches for "Ford Musta", elastic search results with 0 results.问题:问题是当用户搜索“Ford Musta”时,弹性搜索结果为 0 个结果。 In this case I want to have all the options with Ford as make and Musta as phrase of model, year or engine.在这种情况下,我希望将福特作为品牌和 Musta 作为型号、年份或引擎的短语的所有选项。

The search can be in any order, so we want to support user searching for Mustang Ford and other possible orders.搜索可以按任何顺序进行,因此我们希望支持用户搜索 Mustang Ford 和其他可能的顺序。

Thanks a lot!!非常感谢!!

If you want to use autocomplete, you cant use keyword.如果你想使用自动完成,你不能使用关键字。 You need to use different mappings (=> es need to index data in a different way).您需要使用不同的映射(=> es 需要以不同的方式索引数据)。

I joins mappings if you want to use autocomplete on "model" and "engine".如果您想在“模型”和“引擎”上使用自动完成功能,我会加入映射。 Add others fields if needed, or keep them as keyword.如果需要,添加其他字段,或将它们保留为关键字。

Note: This will obviously take more ressource on your es cluster.注意:这显然会在您的 es 集群上占用更多资源。 Note2: you will have to reindex your data, take a look at _reindex feature, please ask us if you have any question.注意2:您将不得不重新索引您的数据,看看 _reindex 功能,如果您有任何问题,请咨询我们。

{
            'settings': {
                "analysis":{
                      "filter":{
                         "name_ngrams":{
                            "max_gram":"20",
                            "type":"edge_ngram",
                            "min_gram":"1",
                            "side":"front"
                         }
                      },
                      "analyzer":{
                         "partial_name":{
                            "type":"custom",
                            "filter":[
                               "lowercase",
                               "name_ngrams",
                               "asciifolding"
                            ],
                            "tokenizer":"standard"
                         },
                         "full_name":{
                            "type":"custom",
                            "filter":[
                               "lowercase",
                               "asciifolding"
                            ],
                            "tokenizer":"standard"
                         }
                      }
                  }
               },
               "mappings":{
                     "properties":{
                        "model":{
                             "type":"text",
                             "analyzer":"partial_name",
                             "search_analyzer":"full_name"
                         },
                        "engine":{
                           "type":"text",
                           "analyzer":"partial_name",
                           "search_analyzer":"full_name"
                        }
                     }
               }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM