Elasticsearch使用分析器进行多字,多字段搜索

时间:2014-10-02 08:49:33

标签: elasticsearch

我想将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。

(我想要的是谷歌搜索,其中日期,数字和字符串在多字查询中被识别)

有什么想法吗?

谢谢!

1 个答案:

答案 0 :(得分:-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"
        }
    }
}

对于映射和设置:

AS

有关详细信息,请参阅以下answerarticle