我有这样的文件
{
"field_1": ["val_1", "val_2"],
"field_2": ["val_21", "val_14"],
"field_3": ["val_6", "val_9"]
}
如果我搜索" val_1",我想得到" field_1"如果我搜索" val_9"我想得到" field_3"来自elasticsearch的回应。所以要知道值匹配的字段。
我怎么能在elasticsearch中做到这一点。
我正在使用:
Version: 2.3.2, Build: b9e4a6a/2016-04-21T16:03:47Z, JVM: 1.8.0_91
答案 0 :(得分:0)
POST /www/test/1
{"field_1":["val_1","val_2"],"field_2":["val_21","val_14"],"field_3":["val_6","val_9"]}
GET /www/test/_search
{
"query": {
"multi_match": {
"query": "val_1",
"fields": ["field_*"]
}
},
"highlight": {
"fields": {
"field_*":{}
}
}
}
对于这个数据和查询,这将是输出:
"hits": [
{
"_index": "www",
"_type": "test",
"_id": "1",
"_score": 0.058849156,
"_source": {
"field_1": [
"val_1",
"val_2"
],
"field_2": [
"val_21",
"val_14"
],
"field_3": [
"val_6",
"val_9"
]
},
"highlight": {
"field_1": [
"<em>val_1</em>"
]
}
}
]
因此,要突出显示,您会看到一个单独的部分 - highlight
- 其中,根据查询,您会在某个字段中获得突出显示的值。