How combine query, must and must_not in elasticsearch? - elasticsearch

The following documents should be found:
matches query 'my text' AND (has not field OR field with value)
I have tried the following:
GET /myIndex/_search
{
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "my text"
}
},
{
}
],
"filter": {
"bool": {
"must_not": {
"exists": {
"field": "myField"
}
},
"must": {
"terms": {
"myField": [
"myValue"
]
}
}
}
}
}
}
}
It should (but does not) work liek that:
bool combine via OR must and filter parts
first must select by query
filter uses bool which combines by OR must andmust_not`
But this behaves like must combined with must_not via AND clause. E.g. when I removed "must" or "must_not" query works.
How change query to combine "must" with "must_not" via OR clause?
Elasticsearch version is 5.3.2

You need an extra bool filter between the must and must_not terms.
You should also think about your myField field. If it is defined as text or as keyword.
Tested with elasticsearch 5.6.1 the following query works:
{
"query": {
"bool": {
"must": [{
"query_string": {
"query": "this is a text content"
}
}],
"filter": {
"bool": {
"should" : [{
"bool" : {
"must_not": {
"exists": {
"field": "myField"
}
}
}
},
{
"bool" : {
"must": {
"terms": {
"myField.keyword": ["my field exists and has a value"]
}
}
}
}]
}
}
}
}
}

On Elasticsearch 7.X, you can simply do:
{
"query": {
"bool": {
"must": [
{"match": {"field1": {"query": "Hero"}}}
],
"must_not": [
{"terms":{"field3":["Batman"]}},
]
}
}
}

Related

Elasticsearch must_not inside nested query

My elastic search index having nested fields. and I want to use must, which contains a must_not query in it with a nested query. I have tried a must_not query separately in the following way:
{
"bool": {
"must_not": [{
"nested": {
"path": "fields",
"query": {
"terms": {
"fields.value.raw": [
"200"
]
}
}
}
}]
}
}
above query gives me a valid result but when I was tried this with must query then it will not give me any result.I am using following query:
{
"bool": {
"must": [{
"nested": {
"path": "fields",
"query": {
"bool": {
"must": [{
"match": {
"fields.uid": "number"
}
}, {
"bool": {
"must_not": [{
"nested": {
"path": "fields",
"query": {
"terms": {
"fields.value.raw": [
"200"
]
}
}
}
}]
}
}]
}
}
}
}]
}
}
above query not gives me a valid result. What is wrong in above query?
How I can use a must_not in must with nested query?
You should use must and must_not in the same bool query.
{
"bool": {
"must_not": [{
"nested": {
"path": "fields",
"query": {
"terms": {
"fields.value.raw": [
"200"
]
}
}
}
}],
"must": [{
"match": {
"fields.uid": "number"
}
}]
}
}

Specify fields to search into in elasticsearch

I have this json:
body: {
"sort" : queryBody.sort,
"query": {
"bool": {
"must": [
{"query_string": { "query": '"mystring"' } }
],
"filter": {
query_string: { query: '"mystring"' } } search-filter [{"term":{"accessType":1}}]
}
}
}
}
This json seems to be searching inside all the fields in the document. How can I specify the exact fields I want to search into according to current json?
I have seen resources explaining you should add the "fields"-array. But I could not find a structure that lookes like mine, which have the schema
query.bool.must.query_string
So I don't really understand where to place the fields-array.
You are very close. You need to add fields array.Try following query:
{
"query": {
"bool": {
"must": [
{
"query_string": {
"fields": [
"field1" ,
"field2"
],
"query": "this AND that OR thus"
}
}
]
}
}
}

How to write a conditional query with Elasticsearch?

I have a simple JSON query for Elasticsearch that looks like this:
"query": {
"bool": {
"must": { "match": { "id": "1" }} ,
"must": { "match": { "tags.name": "a1"}}
}
}
How can I execute the second 'must' criteria ONLY if the value ('a1' in this case) is not empty?
You can achieve it using the following -
{
"query": {
"bool": {
"must": [
{
"match": {
"id": "1"
}
},
{
"bool": {
"should": [
{
"missing": {
"field": "tags.name"
}
},
{
"match": {
"tags.name": "a1"
}
}
]
}
}
]
}
}
}
I don't think "missing" can be used in the latest versions of elasticsearch.
But one can use Conditional Clauses instead.
https://www.elastic.co/guide/en/elasticsearch/reference/6.3/search-template.html#_conditional_clauses

Elasticsearch - combining query_string and bool query in filter

Is it possible to combine query_string and bool query in filter query?
For Example -
{
"filter": {
"query_string": {
"query": "field:text"
}
},
"bool": {
"should": {
"match": {
"field": "text"
}
}
}
}
bool is meant to be used to club various queries together into a single bool query.
You can use bool to combine multiple queries in this manner -
{
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "field:text"
}
},
{
"match": {
"field": "text"
}
}
]
}
}
}
The must clause will make sure all the conditions are matched.
You can also use should which will make sure either one of the query is matched in case of only should is used.
As bool is just another query type , you can also club bool queries inside bool queries as follows -
{
"query": {
"bool": {
"must": [
{
"bool": {
"must": [
{
"query_string": {
"query": "field:text"
}
},
{
"match": {
"field": "value"
}
}
]
}
},
{
"match": {
"field": "text"
}
}
]
}
}
}

How to distinguish hits of several should clauses

I have a query with several "should" clauses:
{
"query": {
"filtered": {
"query": {
"bool": {
"should": [
{
"query_string": {
"query": "<condition1>"
}
},
{
"query_string": {
"query": "<condition1>"
}
}
]
}
},
}
},
"size": 1000,
"sort": [
{
"#timestamp": {
"order": "asc"
}
}
]
}
How can I find out which query results were produced by condition1, and which by condition2? Is it possible to inject a field with different values for different conditions, or distinguish hits in any other way?
You can use named queries to achieve this.
{
"query": {
"bool": {
"should": [
{
"query_string": {
"query": "<condition1>",
"_name": "sub_query_1"
}
},
{
"query_string": {
"query": "<condition1>",
"_name": "sub_query_2"
}
}
]
}
}
}
You result will then contain a matched_filters array with either sub_query_1, sub_query_2, or both in it.
Update
Play link: https://www.found.no/play/gist/af1a1fa2b5cf3aa279b1

Resources