Elasticsearch bool query with nested as well as non-nested clauses - elasticsearch

I have data with nested as well as non-nested components. I cannot remap the data, it is in production. Sample is
{
"name":[
"first":"Tom",
"last":"Smith"
],
"status":"married"
}
I need to query within nested as well as non-nested values . Ie, all entries who are not 'married' but whose first names are 'Tom'.
To do a query just for 'Tom', I would do
GET /_search
{
"query": {
"nested": {
"path": "name",
"query": {
"bool": {
"must": [
{
"match" : {"name.first" : "Tom"}}
]
}
}
}
}
}
But how do I combine this with a must_not query on status(which is non nested)

You need to put a must_not behind the must. I tested this in my environment (except I used term instead of match) and it filtered the unwanted elements.
GET /_search
{
"query": {
"nested": {
"path": "name",
"query": {
"bool": {
"must": [
{
"match" : {"name.first" : "Tom"}}
],
"must_not": [
{
"match" : {"status" : "married"}}
]
}
}
}
}
}

Related

Elastic Search nested query string?

I want to search 2 values in 2 different columns using wildcard but its not running as expected with 2 values but runs fine on single query_string
This works with single column
{
"query": {
"query_string" : {
"default_field" : "Phone",
"query" : "*568072*"
}
}
}
I tried to expand it to use it with 2 columns with 2 different values.
{
"query":
{
"bool":
{
"should": [
{
"query_string":
{
"query": "*Chicago*",
"fields": ["Sources"]
},
"query_string":
{
"query": "*493*",
"fields": ["Phone"]
}
}
]
}
}
}
Where am I wrong ?
You have a mistake in your query. You have one big object inside should array, that has two same keys that does not make sense. Instead it should be array of objects like this:
{
"query": {
"bool": {
"should": [
{ "query_string": { "query": "*Chicago*", "fields": ["Sources"] } },
{ "query_string": { "query": "*493*", "fields": ["Phone"] } }
]
}
}
}

ElasticSearch Multi Match with Multiple Query Parameters

We have the following multi match query in Elastic Search
{
"query": {
"bool": {
"must": {
"multi_match": {
"query": "90803",
"type": "cross_fields",
"fields": [
"POSTAL_CODE^5",
"ADDRESS",
"CITY"
],
"operator": "and"
}
}
}
}}
How can we pass multiple query parameters. For e.g. we want to pass multiple ID in the query to match against the field Postal Code.
First, is POSTAL_CODE an analyzed field? If it's not the case you could use a Terms Query:
{
"query": {
"terms" : {
"POSTAL_CODE" : ["90803", "90809"]
}
}
}
If you want to use Match for some reason, there is not a Match Query that matches several values, you have to use a Bool Query with should or must depending on your use case.
Example with must:
{
"query": {
"bool": {
"must": [{
"match": { "POSTAL_CODE": "90803" }
}, {
"match": { "POSTAL_CODE": "90809" }
}]
}
}
}

How combine query, must and must_not in 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"]}},
]
}
}
}

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"
}
}
]
}
}
}

Disable hits and use exclusively inner_hits

>Beginner here.
I have made a architecture for my profile - photo's application. In this application user can search for member by member's attributes and photo's attributes. And returned is only the photo's that have matched the query.
The problem is that one user might have thousands of photo's and each time a search is ran it return's hits: full object's of the profiles( with the nested photos ).
How can i make elasticsearch return only the value's of inner_hits?
Here is my query:
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "photo",
"query": {
"bool": {
"must": [
{
"match": {
"photo.make": "BMW"
}
},
{
"match": {
"photo.model": "111"
}
}
]
}
},
"inner_hits" : {"size": 1}
}
}
]
}}}
Duplicate of: Elasticsearch: Return only nested inner_hits
Quoting:
Should be able to achieve it by disabling source-field at top-level by specifying "_source" : false
POST /networkcollection/branch_routers/_search/
{
"_source" : false,
"query": {
"nested": {
"path": "queries",
"query": {
"bool": {
"must": [
{ "match":
{ "queries.dateQuery": "20160101T200000.000Z" }
}
]
}
},
"inner_hits" : {}
}
}
}

Resources