【问题标题】:nested field search in elastic search for structured data结构化数据弹性搜索中的嵌套字段搜索
【发布时间】:2016-01-06 10:12:19
【问题描述】:

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

结构化数据的例子是

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

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

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

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

我的查询如下所示:

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

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

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

【问题讨论】:

    标签: search elasticsearch highlighting


    【解决方案1】:

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

    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>"
               ]
            }
         }
      ]
    

    【讨论】:

      猜你喜欢
      • 2020-06-14
      • 1970-01-01
      • 2012-07-15
      • 1970-01-01
      • 2016-05-22
      • 2020-10-12
      • 1970-01-01
      • 2021-01-21
      • 1970-01-01
      相关资源
      最近更新 更多