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"} }
]
}
}
]
}
}
}
Related
I am trying to do a DSL Query filter in Kibana for a specific URI, while matching multiple IP subnets.
So far I have managed to make it work with only one IP subnet:
{
"query": {
"bool": {
"must": [
{
"match": {
"uripath": "/specific-uri-path"
}
},
{
"match": {
"clientip": "10.0.0.0/8"
}
}
]
}
}
}
But if I try to match for multiple subnets, it fails (probably because match can only contain different fields):
{
"query": {
"bool": {
"must": [
{
"match": {
"uripath": "/specific-uri-path"
}
},
{
"match": {
"clientip": "10.0.0.0/8",
"match": {
"clientip": "14.0.0.0/8"
}
}
]
}
}
}
I was able to make it work with the terms query. That means that I can also use an array of values for "clientip", so overall it's cleaner. I hope it will help someone else too.
{
"query": {
"bool": {
"must": [
{
"match": {
"uripath.keyword": "/specific-uri-path"
}
},
{
"terms": {
"clientip": [
"10.0.0.0/8",
"14.0.0.0/8",
"20.0.0.0/8"
]
}
}
]
}
}
}
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"
}
}
]
}
}
}
}
]
}
}
}
Im having some problems querying with elasticsearch. So basically i wanted to know the sql equivalent of elasticsearch.
Whats the equivalent elasticsearch syntax for this sql query ?
SELECT *
FROM people
WHERE name LIKE( $name% )
AND ( gender = 0 OR age < 18 )
AND id IN( 1, 2, 3 )
AND id NOT IN( 4, 5, 6 )
AND dead = 0
ORDER BY status desc,
TIME desc
* is just for example
Using boolean query (and nested boolean query ) allows you to express same things than sql request.
Terms query is validated when one of its elements is found (OR).
POST /index_name/people/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "John"
}
}
],
"filter": [
{
"bool": {
"must": [
{
"terms": {
"id": [
1,
2,
3
]
}
},
{
"term": {
"dead": 0
}
},
{
"bool": {
"should": [
{
"term": {
"gender": 0
}
},
{
"range": {
"age": {
"lt": 18
}
}
}
]
}
}
],
"must_not": [
{
"terms": {
"id": [
4,
5,
6
]
}
}
]
}
}
]
}
},
"sort": [
{
"status": {
"order": "desc"
},
"time": {
"order": "desc"
}
}
]
}
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"
}
}
]
}
}
]
}
}
}
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}
}
}