OR & AND Operators in Elastic search - elasticsearch

Hi I want to achieve this in Elasticsearch.
select * from products where brandName = Accu or brandName = Perfor AND cat=lube(any where in any filed of an elastic search ).
I am using this query in Elasticsearch.
{
"bool": {
"must": {
"query_string": {
"query": "oil"
}
},
"should": [
{
"term": {
"BrandName": "Accu"
}
},
{
"term": {
"BrandName": "Perfor"
}
}
]
}
}
By this query m not getting the combination exact results.

You need to add minimum_should_match: 1 to your query and probably use match instead of term if your BrandName field is an analyzed string.
{
"bool" : {
"must" : {
"query_string" : {
"query" : "oil OR lube OR lubricate"
}
},
"minimum_should_match": 1, <---- add this
"should" : [ {
"match" : {
"BrandName" : "Accu"
}
}, {
"match" : {
"BrandName" : "Perfor"
}
} ]
}
}

This query satisfy your condition.
{
"bool" : {
"must" : { "term" : { "cat" : "lube" } },
"should" : [
{ "term" : { "BrandName" : "Accu" } },
{ "term" : { "BrandName" : "Perfor" } }
],
"minimum_should_match" : 1
}
}

Related

how can get the data using elastic search when they are "AND-OR"conditions (Query builder)

For example, if I had to fetch the Results from the text matching from all the fileds
"(userId OR currentUserId) AND (customeromerId OR customercode OR customerbacth) AND (customerph or customer addrss ) AND customercity".
If I use the below code I am getting the results which are matched according to the keywords irrespective of AND OR conditions.
$queryMsg = ['query_string' => ['query' => $query]];
But I want to fetch the results, for example, a record which matches userId or currentUserId and customeromerId should be fetched.
Please help me out how to write a query in elastic search so that I can fetch the results.
Thanks a lot in advance.
{
"query" : {
"must" : [
{
"bool" : {
"should" : [
{
"match" : {'userId' : id}
},
{
"match" : {'currentUserId' : id}
}
],
"minimum_should_match" : 1
}
},
{
"bool": {
"should": [
{
"match": {'customeromerId': code}
},
{
"match": {'customercode': code}
},
{
"match": {'customerbacth': code}
}
],
"minimum_should_match" : 1
},
},
{
"bool": {
"should": [
{
"match": {'customerph': address}
},
{
"match": {'customer addrss': address}
}
],
"minimum_should_match" : 1
}
},
{
"bool" : {
"must" : {
"customercity" : city
}
}
}
]
}
}

How to combine "must" and "should" in Elasticsearch query?

I need to "translate" this pseudo-SQL query in Elasticsearch query DSL:
select from invoice where invoiceType = 'REGULAR' and receiver =
'CUSTOMER' and (invoiceStatus = 'DISPATCHED' or invoiceStatus = 'PAYED')
I have this:
{
"query": {
"bool": {
"must": [
{ "match": { "invoiceType": "REGULAR" }},
{ "match": { "receiver": "CUSTOMER" }},
{ "bool" : {
"should": [
{"match" : {"invoiceStatus": "DISPATCHED"}},
{"match" : {"invoiceStatus": "PAYED"}}
]
}
}
]
}
}
}
That query is returning 0 results, but I know there are many that matches what I'm searching for. AFAIK, must would be like 'AND' and should like 'OR'. What am I missing?
Not sure that it will work for you or not but you can make a try and see what you get? Though I did some change with match to term. Hope this will help you.
GET /invoice/_search
{
"query" : {
"constant_score" : {
"filter" : {
"bool" : {
"must" : [
{ "term" : {"invoiceType" : "REGULAR"}},
{ "term": { "receiver": "CUSTOMER" }},
{ "bool" : {
"should" : [
{"terms": {"invoiceStatus": ["DISPATCHED","PAYED"]}}
]
}}
]
}
}
}
}
}
OR
GET /invoice/_search
{
"query" : {
"constant_score" : {
"filter" : {
"bool" : {
"must" : [
{ "term" : {"invoiceType" : "REGULAR"}},
{ "term": { "receiver": "CUSTOMER" }},
{ "bool" : {
"should" : [
{"term": {"invoiceStatus": "DISPATCHED"}},
{"term": {"invoiceStatus": "PAYED"}}
]
}}
]
}
}
}
}
}

Search Documents Containing Certain Nested Objects in Elasticsearch

My documents in Elasticsearch index have following format:
{
timestamp: "123456789",
tags: [
{ key:"tag1", "value": "val1" }, ...
]
}
I want get all documents which contain for example { key:"tag1" } and { key:"tag2", "value": "val2" } in their tags field.
How can I do this?
You can try with a bool query, where you specify how many nested query you need in the must section:
GET test_nested/test/_search
{
"query": {
"bool": {
"must": [
{"nested" : {
"path" : "tags",
"query" : {
"bool" : {
"must" : [
{ "match" : {"tags.key" : "tag1"} }
]
}
}
}},
{"nested" : {
"path" : "tags",
"query" : {
"bool" : {
"must" : [
{ "match" : {"tags.key" : "tag2"} },
{ "match" : {"tags.value" : "val2"} }
]
}
}
}}
]
}
}
}
In this case i have one nested query for selecting all documents with key "tag1" and the second nested query to select all documents with the "tag2" and "value2".

How can i filter by field when have the same fields

I want to do filter on the field type on a doc,and the doc has two object message and tags, but both message and tags have the type field.
Doc:
{
"message":{
"type":2,
"body":"test"
},
"tags":{
"tagname":"td",
"type":0
}
}
How can i filter the doc with message's type is 2 ?,i tried this but no result returned.
"filter" : {
"query" : {
"bool" : {
"must" : {
"bool" : {
"should" : {
"match" : {
"type" : {
"query" : 2,
"type" : "boolean",
"operator" : "AND"
}
}
}
}
}
}
}
Simply filtering on the field message.type will work given that the field message is not of nested type:
{
"query": {
"term": {
"message.type": {
"value": 2
}
}
}
}
If message is of nested type then use the following nested query.
{
"query": {
"nested": {
"path": "message",
"query": {
"term": {
"message.type": {
"value": 2
}
}
}
}
}
}

Filter Query Without Score in Elastic Search

I need to modify this query so that elastic search does not give it a score. I want my custom filter score to be the only thing giving any result a score. How do I accomplish this?
Each record should only every have a score of 0, 100, or 1000.
{
"size":50,
"from":0,
"query" : {
"custom_filters_score" : {
"query" : {
"filtered" : {
"query" : {
"bool" : {
"must" : [
{"term":{"type":"alpha"}},
{"field":{"sector":"exists"}},
{"field":{"sector.sub":"exists"}},
{"field":{"alpha_sector.sub.categories":"second"}},
{"field":{"beta_sector.sub.columns":"first"}},
{"term":{"beta_type":"beta"}},
{"term":{"area":"624"}}
]
}
},
"filter" : {
"or" : [
{
"and" : [
{"term":{"area":"624"}},
{"term":{"start":"07242013"}}
]
},
{
"and" : [
{"term":{"area":"624"}},
{"term":{"start":"blank"}}
]
}
]
}
}
},
"filters" : [
{"filter":{"term":{"resource":5726}}, "boost":"1000"},
{"filter":{"term":{"alpha_resource":5726}}, "boost":"100"}
],
"score_mode":"sum"
}
}
}
I am not quite sure what you are trying to achieve here
{"field":{"sector":"exists"}},
{"field":{"sector.sub":"exists"}},
but in general, if you don't want part of your query to affect the score, just make it a filter. It's also will be better to use "bool" with "term" filters instead of "and"/"or"/"not"
{
"size":50,
"from":0,
"query" : {
"custom_filters_score" : {
"query" : {
"filtered" : {
"query" : {
"match_all": {}
},
"filter" : {
"bool" : {
"must" : [
{"term":{"type":"alpha"}},
{"query":{"field":{"sector":"exists"}}},
{"query":{"field":{"sector.sub":"exists"}}},
{"query":{"field":{"alpha_sector.sub.categories":"second"}}},
{"query":{"field":{"beta_sector.sub.columns":"first"}}},
{"term":{"beta_type":"beta"}},
{"term":{"area":"624"}}
],
"should" : [
{
"bool" : {
"must" : [
{"term":{"area":"624"}},
{"term":{"start":"07242013"}}
]
}
},
{
"bool" : {
"must": [
{"term":{"area":"624"}},
{"term":{"start":"blank"}}
]
}
}
]
}
}
}
},
"filters" : [
{"filter":{"term":{"resource":5726}}, "boost":"1000"},
{"filter":{"term":{"alpha_resource":5726}}, "boost":"100"}
],
"score_mode":"total"
}
}
}

Resources