假设我存储了以下数据,并希望在object Main extends App {
args.length match {
case 0 => ???
case 1 => ???
case _ => ???
}
}
为xy
或old_value
的文档的new_value
和field_name
字段中搜索字词curriculum_name_en
或curriculum_name_pr
:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 98,
"max_score": 1,
"hits": [
{
"_index": "my_index",
"_type": "audit_field",
"_id": "57526c197e83c",
"_score": 1,
"_source": {
"session_id": 119,
"trans_seq_no": 1,
"table_seq_no": 1,
"field_id": 2,
"field_name": "curriculum_id",
"new_value": 118,
"old_value": null
}
},
{
"_index": "my_index",
"_type": "audit_field",
"_id": "57526c197f2c3",
"_score": 1,
"_source": {
"session_id": 119,
"trans_seq_no": 1,
"table_seq_no": 1,
"field_id": 3,
"field_name": "curriculum_name_en",
"new_value": "Test Index creation",
"old_value": null
}
},
{
"_index": "my_index",
"_type": "audit_field",
"_id": "57526c198045c",
"_score": 1,
"_source": {
"session_id": 119,
"trans_seq_no": 1,
"table_seq_no": 1,
"field_id": 4,
"field_name": "curriculum_name_pr",
"new_value": null,
"old_value": null
}
},
{
"_index": "my_index",
"_type": "audit_field",
"_id": "57526c1981512",
"_score": 1,
"_source": {
"session_id": 119,
"trans_seq_no": 1,
"table_seq_no": 1,
"field_id": 5,
"field_name": "curriculum_name_pa",
"new_value": null,
"old_value": null
}
}
]
}
}
可能还有更多字段,现在用户可以选择其中一个或多个字段,并在他/她选择的字段中定义搜索字词,挑战在这里,我们如何说弹性考虑{{1 }}匹配用户选择的字段,然后搜索field_name
和old_value
例如,如果用户选择new_value
和curriculum_name_en
,然后想要在curriculum_name_pr
这些文档的xy
和old_value
字段内搜索new_value
field_name
在领域之上。
我们该怎么做?
答案 0 :(得分:1)
这个要求的想法是你需要做出类似的事情:只有当new_value
与某个值匹配时,查询才需要匹配old_value
和/或field_name
井。没有类似程序的方式来说明如果那样。
我建议的是这样的:
{
"query": {
"bool": {
"must": [
{
"terms": {
"field_name": [
"curriculum_name_en",
"curriculum_name_pr"
]
}
},
{
"multi_match": {
"query": "Test Index",
"fields": ["new_value","old_value"]
}
}
]
}
}
}
那么,你的如果那个条件是来自must
查询的bool
语句,那么 if 和然后< / em>分支位于must
内。
答案 1 :(得分:0)
这可以解决您的问题
{
"query": {
"filtered": {
"filter": {
"and": [
{
"query" : {
"terms" : {
"field_name" : [
"curriculum_name_en",
"curriculum_name_pr"
],
"minimum_match" : 1
}
}
},
{
"query" : {
"terms" : {
"new_value" : [
"test", "index"
],
"minimum_match" : 1
}
}
}
]
}
}
}
}
}