结构化数据弹性搜索中的嵌套字段搜索

nested field search in elastic search for structured data

我有一个名为 "data" 的字段。 "data" 字段会动态填充结构化数据。对于结构化数据,我们没有任何先前的固定结构。数据字段包含 String、Date、Integer 等类型的数据。

结构化数据的示例是

"data":
{
   {
    "fname":"ravinder",
    "lastname":"reddy",
    "join":"2009-11-15T14:12:12"
     "address""
      {
       "Hno": "253",
       "Street" : "james Street"
      } 
   }
}

如何在该数据字段中搜索特定文本?

我应该能够搜索数据字段内的任何文本并突出显示所选文本。

我在搜索中使用了类似 data.* 的模式。但是由于数据字段有很多类型的数据。我在运行时解析异常,所有分片都无法 return 任何东西。

我的查询如下所示:

{
  "multi_match": {
    "query": "james street",
    "fields": [
      "data",
      "data.*"
    ],
    "type": {"phrase_prefix"}
  },
  "highlight":
   {
    "fields":{"data","data.*"}
   }
}

如果我将 "data"、"data.*" 替换为“_all”,我可以完成这项工作。但是我无法突出显示这些字段。

非常感谢任何帮助。非常感谢

只是一次尝试,也许它会让你开始,你可以进一步尝试:

DELETE /test
PUT /test
{
  "mappings": {
    "test": {
      "dynamic_templates": [
        {
          "data_template": {
            "match": "data",
            "mapping": {
              "copy_to": "my_custom_all_field",
              "term_vector": "with_positions_offsets",
              "store": true
            }
          }
        },
        {
          "inner_fields_of_data_template": {
            "path_match": "data.*",
            "mapping": {
              "copy_to": "my_custom_all_field",
              "term_vector": "with_positions_offsets",
              "store": true
            }
          }
        }
      ],
      "properties": {
        "my_custom_all_field": {
          "type": "string",
          "term_vector": "with_positions_offsets",
          "store": true
        }
      }
    }
  }
}

POST /test/test/1
{
  "data": {
    "fname": "ravinder",
    "lastname": "reddy",
    "join": "2009-11-15T14:12:12",
    "address": {
      "Hno": "253",
      "Street": "james Street"
    }
  }
}

POST /test/test/2
{
  "data": {
    "fname": "ravinder",
    "lastname": "james",
    "address": {
      "Hno": "253",
      "Street": "whatever Street"
    },
    "age": 55
  }
}

POST /test/test/3
{
  "data": {
    "fname": "mui",
    "lastname": "reddy",
    "address": {
      "Hno": "253",
      "Street": "james Street"
    },
    "age": 253
  }
}

GET test/test/_search
{
  "query": {
    "multi_match": {
      "query": "james street",
      "fields": [
        "my_custom_all_field"
      ],
      "type": "phrase_prefix"
    }
  },
  "highlight": {
    "fields": {
      "data.*": {}
    }
  }
}

对于上面的例子,你会得到这样的输出:

  "hits": [
     {
        "_index": "test",
        "_type": "test",
        "_id": "3",
        "_score": 0.53423846,
        "_source": {
           "data": {
              "fname": "mui",
              "lastname": "reddy",
              "address": {
                 "Hno": "253",
                 "Street": "james Street"
              },
              "age": 253
           }
        },
        "highlight": {
           "data.address.Street": [
              "<em>james Street</em>"
           ]
        }
     },
     {
        "_index": "test",
        "_type": "test",
        "_id": "1",
        "_score": 0.4451987,
        "_source": {
           "data": {
              "fname": "ravinder",
              "lastname": "reddy",
              "join": "2009-11-15T14:12:12",
              "address": {
                 "Hno": "253",
                 "Street": "james Street"
              }
           }
        },
        "highlight": {
           "data.address.Street": [
              "<em>james Street</em>"
           ]
        }
     }
  ]