ElasticSearch sort by field - elasticsearch

i have this query:
GET /_search
{
"from" : 0, "size" : 30,
"query": {
"filtered": {
"query" : {
"query_string": {
"query": "((categoria.id:1752) AND (cidadeId:7300))
OR ((categoria.id:1752) and (estadoId:13)) "
}
}
}
},
"sort": [
{ "img": { "order": "desc" } }
]
}
I would like to order by cidadeId = 7300 and then the other but do not know how to do this in the sort

I have considered a small example :
PUT test/test/1
{
"categoria.id":1752,
"cidadeId":7300
}
PUT test/test/2
{
"categoria.id":1752,
"estadoId":13
}
PUT test/test/3
{
"categoria.id":175,
"cidadeId":1
"estadoId":13
}
With the few information you are giving in your answer, I can give you the following solution for ascendant sorting on cidadeId
GET test/_search
{
"from": 0,
"size": 30,
"query": {
"filtered": {
"query": {
"query_string": {
"query": "((categoria.id:1752) AND (cidadeId:7300)) OR ((categoria.id:1752) and (estadoId:13))"
}
}
}
},
"sort": [
{
"cidadeId": {
"order": "asc"
}
}
]
}
I hope this will help

Related

how to get value only from Elasticsearch sum aggregation query?

I'm using this query
POST /stats/_search?filter_path=aggregations.views.value
{
"aggs": {
"views": {
"sum": {
"field": "viewCount"
}
}
},
"size": 0,
"query": {
"bool": {
"must": [
{
"range": {
"timestamp": {"gte":"now-2d"}
}
}
]
}
}
}
and it gives the result
{
"aggregations" : {
"views" : {
"value" : 49198.0
}
}
}
I'm looking to only get "49189.0" or the least amount of info possible, maybe "{value: 49198.0}"

How to group events by multiple terms?

How can I group by year and month? My query works if I leave 1 term, for example, Month. But I cannot group by multiple terms.
GET traffic-data/_search?
{
"size":0,
"query": {
"bool": {
"must": [
{ "match": {
"VehiclePlateNumber": "111"
}}
]
} },
"aggs" : {
"years" : {
"terms" : {
"field" : "Year"
},
"aggs" : {
"months" : { "by_month" : { "field" : "Month" } }
}
}
}
}
I think your question's query is already close, try this:
GET traffic-data/_search?
{
"size": 0,
"query": {
"bool": {
"must": [
{
"match": {
"VehiclePlateNumber": "111"
}
}
]
}
},
"aggs": {
"years": {
"terms": {
"field": "Year",
"size": 100
},
"aggs": {
"months": {
"terms": {
"size": 12,
"field": "Month"
}
}
}
}
}
}
Edit - I am assuming your month is a string keyword field. Let me know if this is not the case (and please include the mappings) and I will revise.

Elasticsearch: querying documents by nested Array properties

I have something like this as the document:
{ name : "name1",
age : 30,
address :[{street : "st1" , no : 10},
{street : "st2", no : 20},
{street : "st3", no : 20}]
}
{ name : "name2",
age : 31,
address :[{street : "st2" , no : 10},
{street : "st3", no : 20},
{street : "st10", no : 20}]
}
I want to issue a query to find the records, in their address array both "st1"
AND "st2" are present. This query should return the first document in the above example.
Find all documents that have address in both "st1" AND "st2". I wrote this query which returns nothing.
{
"query": {
"nested": {
"path": "address",
"query": {
"bool": {
"must": [
{
"term": {
"address.street": "st1"
}
},
{
"term": {
"address.street": "st2"
}
}
]
}
}
}
}
}
How can I write this query?
I could find a way to write a query for mentioned problem with the help of my colleague. It may not the optimal solution but it works.
{
"query": {
"bool": {
"must": [
{
"query": {
"nested": {
"path": "address",
"query": {
"term": {
"address.street": "st1"
}
}
}
}
},
{
"query": {
"nested": {
"path": "address",
"query": {
"term": {
"address.street": "st2"
}
}
}
}
}
]
}
}
}
If you know the more optimal solution, please share it.

Elasticsearch Query for getting field with 'AND' relation

I'm having elastic document as below
I want a search query satisfying condition:
how to get the those OPERATIONS and CATEGORY values that has both AREA=Mumbai and AREA=Chennai
So Output should be CATEGORY:Consulting1 , OPERATIONS: Regulatory Operations
Use terms Query :
{
"query": {
"terms": {
"AREA": [
"Mumbai",
"Chennai"
]
}
}
}
May be that works:
{
"query": {
"bool": {
"must": [
{"term": { "AREA" : "Mumbai" }},
{"term": { "AREA" : "Chennai" }}
]
}
}
}
Try this and let me know:
{
"size": 0,
"query": {
"bool": {
"should": [
{
"term": {
"AREA": "mumbai"
}
},
{
"term": {
"AREA": "chennai"
}
}
]
}
},
"aggs": {
"unique_operations": {
"terms": {
"field": "OPERATIONS",
"size": 10
},
"aggs": {
"count_areas": {
"cardinality": {
"field": "AREA"
}
},
"top": {
"top_hits": {
"size": 2,
"_source": {
"include": ["CATEGORY"]
}
}
},
"areas_bucket_filter": {
"bucket_selector": {
"buckets_path": {
"areasCount": "count_areas"
},
"script": "areasCount == 2"
}
}
}
}
}
}
LATER EDIT: added top_hits aggregation to get back sample documents covering the request for the categories.
Please try this one.
{
"query": {
"bool": {
"should": [
{
"query_string": {
"default_field": "AREA",
"query": "mumbai"
}
},
{
"query_string": {
"default_field": "AREA",
"query": "chennai"
}
}
]
}
}
}[![result][1]][1]

Using aggregation with filters in elastic search

I have an elastic search running with documents like this one:
{
id: 1,
price: 620000,
propertyType: "HO",
location: {
lat: 51.41999,
lon: -0.14426
},
active: true,
rentOrSale: "S",
}
I'm trying to use aggregates to get statistics about a certain area using aggregations and the query I'm using is the following:
{
"sort": [
{
"id": "desc"
}
],
"query": {
"bool": {
"must": [
{
"term": {
"rentOrSale": "s"
}
},
{
"term": {
"active": true
}
}
]
},
"filtered": {
"filter": {
"and": [
{
"geo_distance": {
"distance": "15.0mi",
"location": {
"lat": 51.50735,
"lon": -0.12776
}
}
}
]
}
}
},
"aggs": {
"propertytype_agg": {
"terms": {
"field": "propertyType"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
},
"bed_agg": {
"terms": {
"field": "numberOfBedrooms"
},
"aggs": {
"avg_price": {
"avg": {
"field": "price"
}
}
}
}
}
}
But in the result I can't see the aggregations. As soon as I remove either the bool or filtered part of the query I can see the aggregations. I can't figure out why this is happening, nor how do I get the aggregations for these filters. I've tried using the answer to this question but I've not been able to solve it. Any ideas?
I think your query need to be slightly re-arranged - move the "filtered" further up and repeat the "query" command:
"query": {
"filtered": {
"query" : {
"bool": {
...
}
},
"filter": {
...
}
}
}

Resources