ElasticSearch: If a search word needs to match multiple fields, only one field needs to be queried. However, if a specific field matches, no other results are returned. How should this query statement be written?
Currently, the system queries data based on the sequence of different fields. If a specified field is queried and a result is returned, the system does not query the remaining fields. Is there a way to write this logic as a query?
The way I’m doing it now is to run the following statements in order. If the first code is returned, I don’t continue the query.
GET metadata/_search
{
"query": {
"bool": {
"must": [
{"simple_query_string": {
"query": "test",
"fields": ["code.keyword"]
}}
]
}
}
}
GET metadata/_search
{
"query": {
"bool": {
"must": [
{"simple_query_string": {
"query": "test",
"fields": ["instanceName.keyword"]
}}
],
"must_not": [
{"simple_query_string": {
"query": "test",
"fields": ["code.keyword"]
}}
]
}
}
}
GET metadata_hw_dam-ai_dataset/_search
{
"query": {
"bool": {
"must": [
{
"simple_query_string": {
"query": "*test*",
"fields": [
"instanceName.keyword"
]
}
}
],
"must_not": [
{
"simple_query_string": {
"query": "test",
"fields": [
"code.keyword"
]
}
},
{
"simple_query_string": {
"query": "test",
"fields": [
"instanceName.keyword"
]
}
}
]
}
}
}
程鹏杰 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
1