【问题标题】:Elasticsearch multi-word, multi-field search with analyzers使用分析器进行 Elasticsearch 多词、多字段搜索
【发布时间】:2014-10-02 08:49:32
【问题描述】:

我想使用 elasticsearch 进行多词搜索,其中所有字段都使用分配的分析器在文档中进行检查。

所以如果我有一个映射:

{
"settings": {
    "analysis": {
      "analyzer": {
        "folding": {
          "tokenizer": "standard",
          "filter":  [ "lowercase", "asciifolding" ]
        }
      }
    }
  },
  "mappings" : {
    "typeName" :{
      "date_detection": false,
      "properties" : {
        "stringfield" : {
          "type" : "string",
          "index" : "folding"
        },
        "numberfield" : {
          "type" : "multi_field",
          "fields" : {
            "numberfield" : {"type" : "double"},
            "untouched" : {"type" : "string", "index" : "not_analyzed"}
          }
        },
        "datefield" : {
          "type" : "multi_field",
          "fields" : {
            "datefield" : {"type" : "date", "format": "dd/MM/yyyy||yyyy-MM-dd"},
            "untouched" : {"type" : "string", "index" : "not_analyzed"}
          }
        }
      }
    }
  }
}

如您所见,我有不同类型的字段,但我确实知道结构。 我想要做的是用一个字符串开始搜索,以使用分析器检查所有字段。

例如,如果查询字符串是:

John Smith 2014-10-02 300.00

我想在所有字段中搜索“John”、“Smith”、“2014-10-02”和“300.00”,同时计算相关性得分。更好的解决方案是在单个文档中包含更多字段匹配项。

到目前为止,我能够使用 multi_field 搜索所有字段,但在这种情况下,我无法解析 300.00,因为 300 存储在 multi_field 的字符串部分中。 如果我在“_all”字段中搜索,则没有使用分析器。

我应该如何修改我的映射或查询以进行多词搜索,其中日期和数字在多词查询字符串中被识别? 现在,当我进行搜索时,会发生错误,因为无法将整个字符串解析为数字或日期。如果我使用 multi_search 的字符串表示,那么 300.00 将不会是结果,因为字符串表示是 300。

(我想要的是类似于 google 搜索,在多词查询中可以识别日期、数字和字符串)

有什么想法吗?

谢谢!

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    使用whitespace 作为analyzer 中的过滤器,然后将此analyzer 作为search_analyzer 应用到mapping 中的字段,将分割查询部分,每个部分都将应用于索引以找到最佳匹配。并且将ngram 用于index_analyzer 会大大改善结果。 我正在使用以下设置进行查询:

    "query": {
                "multi_match": {
                    "query": "sample query",
                    "fuzziness": "AUTO",
                    "fields": [
                        "title",
                        "subtitle",
                    ]
                }
            }
    

    对于映射和设置:

    {
    "settings" : {
        "analysis": {
            "analyzer": {
                "autocomplete": {
                    "type": "custom",
                    "tokenizer": "whitespace",
                    "filter": [
                        "standard",
                        "lowercase",
                        "ngram"
                    ]
                }
            },
            "filter": {
                "ngram": {
                    "type": "ngram",
                    "min_gram": 2,
                    "max_gram": 15
                }
            }
        },
    "mappings": {
            "title": {
                "type": "string",
                "search_analyzer": "whitespace",
                "index_analyzer": "autocomplete"
            },
            "subtitle": {
                "type": "string"
            }
        }
    }
    

    请参阅以下answerarticle 了解更多详情。

    【讨论】:

      猜你喜欢
      • 2016-10-14
      • 2013-09-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-06
      • 1970-01-01
      • 1970-01-01
      • 2018-12-22
      相关资源
      最近更新 更多