小编典典

Elasticsearch使用数组字段索引multi_field

elasticsearch

我是Elasticsearch的新手,正在尝试使用stringarray of strings字段创建多字段索引。有了这些string字段,它们都工作得很好,但是当我尝试在数组中获取一些结果时,它返回一个空的数组。

我的资料:

{
  "string_field_one": "one",
  "string_field_two": "two",
  "array_of_strings_field": [
     "2010", "2011", "2012", "2013"
  ]
}

对应:

{
    "string_field_one" : {
        "type" : "string",
        "analyzer": "snowball",
         "copy_to": "group"
     },
    "string_field_two" : {
        "type" : "string",
        "analyzer": "snowball",
         "copy_to": "group"
     },
    "array_of_strings_field" : {
         "type" : "string",
         "analyzer": "keyword",
         "copy_to": "group"
    }
    "group" : {
        "type": "multi_field"
    }
}

搜索:

 "body": {
          "query": {
              "multi_match": {
                  "query": "one two 2010",
                  type: "cross_fields",
                  operator: "and",
                  fields: [
                      "string_field_one",
                      "string_field_two",
                      "array_of_strings_field",
                      "group"
                  ]
              }
          }
      }

期望:

  • onetwo或搜索,2010应返回结果
  • 搜索one two应返回结果
  • 搜索one two 2010应返回结果
  • 搜索依据one two 2008不应返回结果

我想念什么?


阅读 797

收藏
2020-06-22

共1个答案

小编典典

Cross_fields具有约束,所有字段应具有相同的搜索分析器,或者所有查询项应出现在具有相同搜索分析器的字段中。这里不是这种情况。在上述情况下,您需要使用query_string

例:

"body": {
           "query": {
              "query_string": {
                  "query": "one two 2010",
                  "default_operator": "AND",
                  "fields": [
                      "string_field_one",
                      "string_field_two",
                      "array_of_strings_field",
                      "group"
                  ]

              }
          }
   }
2020-06-22