I am trying to use this query to get all documents with a closed or pending status or with no status at all.but this query always return nothing.
"query": {
"bool": {
"should": [
"terms": {
"status": ["closed","pending"]
},
"missing":{
"field":"status"
}
]
}
}
Individually the queries are working.If I use:
"query": {
"bool": {
"should": [
"terms": {
"status": ["closed","pending"]
}
]
}
}
then it returns 2 documents.
And if I use
"query": {
"bool": {
"should": [
"missing":{
"field":"status"
}
]
}
}
it returns 3.
These results are correct for each query.I just want to combine the 2 queries and get the result 5.What is wrong with the first query?
This ES 5.0 query and it should work on version 2.0
POST es1/example/_search
{
"query": {
"bool": {
"should": [
{
"terms": {
"status": [
"closed",
"pending"
]
}
},
{
"bool": {
"must_not": [
{
"exists": {
"field": "status"
}
}
]
}
}
]
}
}
}
Related
I am querying elastic search using status field and range but getting an error:
"type": "parsing_exception","reason": "[status] query malformed, no
start_object after query name"
Query looks as below:
{
"_source": {
"includes": []
},
"query": {
"bool": {
"must": [
{
"status": "IN_PROGRESS"
},
{
"range": {
"requestDate": {
"gte": "2018-10-01T08:00:00.000Z",
}
}
}
]
}
},
"sort": {
"requestDate": {
"order": "desc"
}
}
}
The error is that you haven't specified the query type - term or match - against status field. So if status is a text datatype, you should perform a match query:
{
"_source": {
"includes": []
},
"query": {
"bool": {
"must": [
{
"match":{ "status": "IN_PROGRESS"
}},
{
"range": {
"requestDate": {
"gte": "2018-10-01T08:00:00.000Z",
}
}
}
]
}
},
"sort": {
"requestDate": {
"order": "desc"
}
}
}
I need to "transform" mysql query to Elasticsearch query. What is blocking me is "where" statement. Basically what I need is to find all items that are within distance of 25 miles based on latitude and longitude and where pickup is enabled OR where items do have delivery enabled and provided zip code
where
(
(
`enabled_pickup` = '1'
and(
ST_Distance_Sphere(
POINT(- 122.41941550000001 , 37.7749295) ,
geolocation
) / 1000 * 0.62137119223733
) <= 25
)
or(
`enabled_delivery` = '1'
`zip_code` = '94116'
)
)
and this is Elasticsearch query that is not working as expected
{
"query": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"bool": {
"must": [
{
"match": {
"enabled_pickup": "1"
}
},
{
"geo_distance": {
"distance": "25 mi",
"geo_location": {
"lat": "37.7749295",
"lon": "-122.41941550000001"
}
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"dispensary.delivery": "1"
}
},
{
"term": {
"zip_code": "94116"
}
}
]
}
}
]
}
}
]
}
}
}
can somebody please point me in right direction?
I was reading about the similar issue, you can give a try with below query. you can find a similar question being solved by this stackoverflow discussion
Hope this helps, Cheers!
{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{"match": { "enabled_pickup": "1"}}
,{ "match": {
"geo_distance": {
"distance": "25 mi",
"geo_location": {
"lat": "37.7749295",
"lon": "-122.41941550000001"
}
}
}
}
]
}
,
"bool": {
"must": [
{ "match": {"dispensary.delivery": "1"} }
,{"match": {"zip_code": "94116"} }
]
}
}
]
}
}
}
Hi my data is structured in the following manner:
student{
subjects{
biology:{
0: "A2-Level"
1:"AS-Level"
2:"University"
}
}
}
My main index is at student, therefore my properties are as follows
"mappings":{"student":{"properties":{"subjects":{"type":"nested","properties":{"biology":{"type":"string"},"english":{"type":"string"}}}
Im currently doing this search :
var query = {
index: 'firebase',
type: 'student',
"body": {
"query": {
"bool": {
"must": [
{
"nested": {
"path": "subjects",
"query": {
"bool": {
"must": [
{ "exists": { "field": english } }
]
}
}
}
}
]
to return the students that are studying english. However nothing is being returned? Can anyone suggest where I am going wrong?
You should check for subjects.english field instead of english
So, your query should look something like this:
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "subjects",
"query": {
"bool": {
"must": [
{
"exists": {
"field": "subjects.english"
}
}
]
}
}
}
}
]
}
}
}
I need to search for a nested field like following:
"bool": {
"should": {
"nested": {
"path": "partnerData",
"query": {
"query_string": {
"query": "Dimmi",
"fields": ["partnerData.partnerName"]
}
}
}
}
}
What I need to do now is to make this filter optional from a query parameter.
If online-booking: true apply this filter, if online-booking: false do not apply the filter.
How can I achieve this?
Thanks,
Marco
You should use multiple bool queries.
First query make sure that online-booking is false, second make sure that its true and second condition is also true. At least one query must match to be returned
{
"bool": {
"should": [
{
"term": {
"online-booking": false
}
},
{
"bool": {
"must": [
{
"term": {
"online-booking": true
}
},
{
"nested": {
"path": "partnerData",
"query": {
"query_string": {
"query": "Dimmi",
"fields": [
"partnerData.partnerName"
]
}
}
}
}
]
}
}
]
}
}
"bool": {
"should": [{
"nested": {
"path": "partnerData",
"query": {
"query_string": {
"query": "Dimmi",
"fields": ["partnerData.partnerName"]
}
}
}
},{
"query_string": {
"query": "$negate-online-booking$",
"default_field": "name"
}
}]
}
If negate-online-booking is different from _exists_:name filter will be applied. If its value is equal to _exists_:name filter will not applied.
If there is a better way to achieve the same result please suggest it.
I need to do as this sentence sql in elasticsearch but not run me
select * from user where (name like '%juan%') and position = 2 and catid in (4,3,76453,345,345,345345,345) and prof = 9
This is one of the examples I've used
{
"size": 25,
"query":
{
"filtered":
{
"filter":
{
"and":
[
{ "term": { "user":"juan" } },
{ "term": { "position":"2" } },
{
"or":
[
{ "term": { "catid":"4" } },
{ "term": { "catid":"3" } }
]
}
]
}
}
}
}
Other :
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"bool": {
"should": [{
"term": {
"name": "juan"
}
}]
}
},
{
"bool": {
"should": [{
"term": {
"position": "2"
}
}]
}
}
]
},
"or":
[
{ "term": { "catid":"4" } },
{ "term": { "catid":"3" } }
]
}
}
},
"size": 1000
}
I really need to search for word and with many variables , here I do not put all but given time are at least 10 more ..
The filters were removed in version 2.0 of Elasticsearch and you have to use the queries instead. If you want to filter, you have two options now:
Use bool query with filter clause (equivalent to a filtered query containing scoring)
Use constant_score query (equivalent to a filtered query without scoring)
Moreover, you should avoid and and or queries, as they are deprecated and will be removed soon.
Regarding your SQL query, I'd translate it like that:
"query": {
"bool": {
"filter": {
"bool": {
"must": [
{
"match": { "user": "juan" }
},
{
"term": { "position":"2" }
},
{
"terms": {
"catid": [4,3,76453,345]
}
}
]
}
}
}
}
Please note that term-query do exact matches on your fields! If e.g. your user field contains names containing of multiple words. The term-query won't return the expected result. In this case, you should use the match-query.
It seems to work
{
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"numeric_range": {"stocktotal": {"gte": 2,"lte": 99999999999}}
}
,
{
"term": { "cat_name":"limpiadoasabarra_derechabarra_derechaapulidoras__de__superficies" }
},
{"terms": {"supplier_id": [8634, 916]}}
]
}
}
},
"size" : 1000,
"aggs" : {
"etiquetas" : {"terms" : { "field" : "supplier_name" }, "size" : 20 },
"nombre_familia" : {"terms" : { "field" : "nombre_familia", "size" : 20 }},
"nombre_subfamilia" : {"terms" : { "field" : "nombre_subfamilia", "size" : 20 }},
"cat_name" : {"terms" : { "field" : "cat_name" }, "size" : 20}
}
}