您好我有这样的文件
{
"_index": "undp",
"_type": "visit",
"_id": "10403_1",
"_score": 1,
"_source": {
"visit_id": 1,
"beneficiary_id": 10403,
"answers": [{
"person_id": "1",
"question_id": "102",
"sub_question_id": "0",
"option_id": "0",
"question_value": "Ahmad Al-Saheb",
"question_type": "input",
"is_beneficiary": "1"
},
{
"person_id": "1",
"question_id": "103",
"sub_question_id": "0",
"option_id": "1",
"question_value": "1",
"question_type": "option",
"is_beneficiary": "1"
},
{
"person_id": "1",
"question_id": "104",
"sub_question_id": "0",
"option_id": "1",
"question_value": "1",
"question_type": "option",
"is_beneficiary": "1"
},
{
"person_id": "1",
"question_id": "105",
"sub_question_id": "0",
"option_id": "0",
"question_value": "32",
"question_type": "input",
"is_beneficiary": "1"
}
]
}
}
如何使用和条件
应用多个问题值的搜索(answers.question_id = 103 and answers.question_value = 1)
and
(answers.question_id = 104 and answers.question_value = 1)
以及如何使用或条件
搜索多个问题值(answers.question_id = 103 and answers.question_value = 1)
or
(answers.question_id = 104 and answers.question_value = 1)
所以在搜索之后我会得到这份文件
答案 0 :(得分:0)
您可以使用Bool Query DSL来实现此目的。
在Bool查询DSL中,must = AND, should = OR and must_not = NOT.
例如:
查询(answers.question_id = 103 and answers.question_value = 1)
将是:
{
"bool": {
"must": [
{ "term": { "answers.question_id" : "103" }},
{ "term": { "answers.question_value" : "1" }}
]
}
}
由于您有嵌套的逻辑运算符,您可以使用嵌套的bool
查询dsl,如下所示。
(answers.question_id = 103 and answers.question_value = 1)
and
(answers.question_id = 104 and answers.question_value = 1)
{
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{ "term": { "answers.question_id" : "103" }},
{ "term": { "answers.question_value" : "1" }}
]
}
},
{
"bool": {
"should": [
{ "term": { "answers.question_id" : "104" }},
{ "term": { "answers.question_value" : "1" }}
]
}
}
]
}
}
}
(answers.question_id = 103 and answers.question_value = 1)
or
(answers.question_id = 104 and answers.question_value = 1)
{
"query": {
"bool": {
"should": [
{
"bool": {
"should": [
{ "term": { "answers.question_id" : "103" }},
{ "term": { "answers.question_value" : "1" }}
]
}
},
{
"bool": {
"should": [
{ "term": { "answers.question_id" : "104" }},
{ "term": { "answers.question_value" : "1" }}
]
}
}
]
}
}
}