0

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
          ]
        }
      }
    ]
  }
}
4
  • You've showed details related to how to be done in SQL but no details about your current ES environment. What exactly you want to count? Are these indices, types, documents? Give example from your ES environment. Commented Nov 26, 2014 at 14:26
  • Here comes the environment Commented Nov 26, 2014 at 14:34
  • 1
    And what is your query so far? Commented Nov 26, 2014 at 14:40
  • Here it is, I only have the query part and its return Commented Nov 26, 2014 at 14:47

2 Answers 2

3

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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" ]
      }
    } ]
  }
}

4 Comments

Yes, but I need to have a resultset like "tata" : '1', 'toto' : 2 'titi' : 3, "cout" : 3
is "tata":'1' a count of matching records (i.e. only one tata record matched) or the value of the data field for tata?
it is a value of the field tata
in that case what you're asking for isn't possible - your application needs to get the data from hits.total not from within the fields object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.