I’m working with Elasticsearch and have documents with fields that can be empty (“”). Here’s an example of the document structure:
[
{
"company_code": "ABC",
"client_id": "12345",
"section": "abc",
"module": "substitute",
"priority": "1"
},
{
"company_code": "ABC",
"client_id": "",
"section": "abc",
"module": "buy_it_again_products",
"priority": 1
}
{
"company_code": "",
"client_id": "",
"section": "abc",
"module": "buy_it_again_products",
"priority": 1
}
]
I need to create an Elasticsearch query that follows these conditions:
- When
company_code
,client_id
, andsection
are given, return documents matching all three fields with non-empty values. - If no documents match the first condition, then only return documents where
company_code
andsection
match, butclient_id
is empty (“”). - If no documents match the first and second conditions, then only return documents where only
section
matches, and bothcompany_code
andclient_id
are empty (“”).
The response should include documents satisfying only one condition according to the priority listed above.
I have tried the following query and also msearch
.
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{ "term": { "company_code": "ABC" } },
{ "term": { "client_id": "12345" } },
{ "term": { "section": "abc" } }
],
"boost": 100
}
},
{
"bool": {
"must": [
{ "term": { "company_code": "ABC" } },
{ "term": { "section": "abc" } },
{ "term": { "client_id": "" } }
],
"boost": 20
}
},
{
"bool": {
"must": [
{ "term": { "section": "abc" } },
{ "term": { "company_code": "" } },
{ "term": { "client_id": "" } }
],
"boost": 1
}
}
],
"minimum_should_match": 1
}
},
"sort": [
{ "_score": "desc" }
]
}
The issue with this approach is, this returns all the documents matching all three conditions. Any way can get the expected result through ES?