Reputation: 171
I'm looking how to merge elasticsearch hits into one. Basicly I want to do the equivalent of
SELECT COUNT(*) WHERE TOTO = 1 AND TATA = 2 AND TITI = 3
I've manage to deal with the WHERE
part using filters but I get all the hits in separate entities
so what I have is
SELECT TOTO, TATA, TITI WHERE TOTO = 1 AND TATA =2 AND TATA =3
How do I manage to get only one hit containing the COUNT(*)
value ?
Environment
{
"my_element": {
"mappings": {
"test": {
"properties": {
"baskets": {
"type": "nested",
"properties": {
"basket_id": {
"type": "string"
},
"num_basket": {
"type": "integer"
},
"tp_basket": {
"type": "string"
}
}
},
"test_id": {
"type": "string"
},
"test_name": {
"type": "string"
}
}
}
}
}
}
so I want to count how many test element have a basket_id
of X and a num_basket
of 3 (if they do that means baskets are Identical, so I want also to show baskets field)
{
"fields": [
"bucket_list",
"baskets.basket_id",
"baskets.num_basket"
],
"query": {
"filtered": {
"filter": {
"and": {
"filter": [
{
"nested": {
"path": "baskets",
"filter": {
"and": {
"filters": [
{
"or": {
"filters": [
{
"bool": {
"must": [
{
"term": {
"baskets.basket_id": "40"
}
},
{
"term": {
"baskets.num_basket": "1"
}
},
{
"term": {
"baskets.tp_basket": "1"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"baskets.basket_id": "41"
}
},
{
"term": {
"baskets.num_basket": "1"
}
},
{
"term": {
"baskets.tp_basket": "1"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"baskets.basket_id": "342"
}
},
{
"term": {
"baskets.num_basket": "1"
}
},
{
"term": {
"baskets.tp_basket": "1"
}
}
]
}
},
{
"bool": {
"must": [
{
"term": {
"baskets.basket_id": "342"
}
},
{
"term": {
"baskets.num_basket": "1"
}
},
{
"term": {
"baskets.tp_basket": "1"
}
}
]
}
}
]
}
}
]
}
}
}
},
{
"nested": {
"path": "baskets",
"filter": {
"and": {
"filters": [
{
"bool": {
"must": [
{
"term": {
"baskets.basket_id": "15"
}
},
{
"term": {
"baskets.num_basket": "2"
}
}
]
}
}
]
}
}
}
},
{
"nested": {
"path": "baskets",
"filter": {
"and": {
"filters": [
{
"bool": {
"must": [
{
"term": {
"baskets.basket_id": "15"
}
},
{
"term": {
"baskets.num_basket": "3"
}
}
]
}
}
]
}
}
}
}
]
}
}
}
}
}
and result
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "my_element",
"_type": "test",
"_id": "sMHPv3i4RTqNCIChGh4Iew",
"_score": 1,
"fields": {
"baskets.basket_id": [
"15",
"15",
"15"
],
"baskets.num_basket": [
2,
3,
1
]
}
},
{
"_index": "my_element",
"_type": "test",
"_id": "KL3U-g-7RtuusNV8hi9YHQ",
"_score": 1,
"fields": {
"baskets.basket_id": [
"15",
"15",
"15"
],
"baskets.num_basket": [
1,
2,
3
]
}
}
]
}
}
Upvotes: 0
Views: 197
Reputation: 371
Probably I am answering really late, but for those who are looking for the answer. This query can be written in very simple way, as follows.
GET _count
{
"query":
{
"bool" : {
"must" : [ {
"term" : {
"basket_id" : "X"
}
}, {
"term" : {
"num_basket" : 3
}
} ]
}
}
}
The above query will give you following result.
{
"count": 6,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
}
}
You can read the count from above response, which is your desired result.
Hope this was helpful.
Upvotes: 3
Reputation: 6180
When the search results are returned from Elasticsearch you should see a total field - this has the count of matching documents.
for example, see "total" : 2,
below:
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 12,
"successful" : 12,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 2.098612,
"hits" : [ {
"_index" : "mytest",
"_type" : "message",
"_id" : "P9wGgJHjQmK8GUvw8M5Q8A",
"_score" : 2.098612,
"fields" : {
"tata" : [ "1" ],
"toto" : [ "1" ],
"titi" : [ "2" ]
}
}, {
"_index" : "mytest",
"_type" : "message",
"_id" : "M26ychoyRR6HkordRdS_HA",
"_score" : 0.30685282,
"fields" : {
"tata" : [ "1" ],
"toto" : [ "1" ],
"titi" : [ "2" ]
}
} ]
}
}
Upvotes: 0