2015-10-08 171 views
0

我有一個名爲「data」的字段。 「data」字段會動態地填充結構化數據。對於結構化數據,我們沒有任何之前的FIXED結構。數據字段包含類型的字符串,日期,整型等數據做結構化數據的彈性搜索結構化數據的嵌套字段搜索

例子是

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

如何搜索該數據字段中的特定文本?

我應該能夠搜索數據字段內的任何文本並高亮顯示所選文本。

我在搜索中使用了類似data。*的模式。但是由於數據字段有許多類型的數據。我在運行時解析異常,並且所有碎片都無法返回任何內容。

我的查詢看起來象下面這樣:

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

如果我更換「數據」,我可以使這項工作與 「_all」 「數據。*」。但我無法突出的領域。

任何幫助,非常感謝。非常感謝你

回答

0

只是一個嘗試,也許它可以讓你開始,你可以進一步實驗:

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