Score:0

How to fetch Individual output of different match_phrase inside one request

ng flag
abc

I am able to run below query which is giving response as total of two match_phrase.

Inserting dummy data as below.

POST /mod1/_bulk
{ "index" : { } }
{ "msg": "BA1" }
{ "index" : { } }
{ "msg": "BA2" }
{ "index" : { } }
{ "msg": "BA2" }
{ "index" : { } }
{ "msg": "BA1" }
{ "index" : { } }
{ "msg": "BA2" }
{ "index" : { } }
{ "msg": "BA1" }
{ "index" : { } }
{ "msg": "BA1" }
{ "index" : { } }

Request -

GET mod1/_search
{
  "size": 0,
  "query": {
    "bool": {
    "should": [
        {
          "match_phrase": {
            "msg": "BA1"
          }
        },
        {
          "match_phrase": {
            "msg": "BA2"
          }
        }
      ]
    }
  }
}

Response -

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 7,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  }
}

Getting response as 7, because BA1 is 4 and BA2 is 3, so it is giving sum of two match_phrase, How can I get response as below i.e individual output of two match_phrase?

.
.
"BA1"
"hits" : {
    "total" : {
      "value" : 4,

.
.
"BA2"
"hits" : {
    "total" : {
      "value" : 3,

Thanks,

abc avatar
ng flag
abc
only way currently i know is to run individual request with each 'match_phrase', but there are many 'match_phrase' to match and i do not want to create multiple requests and instead looking to create single curl request which will have multiple 'match_phrase'.
ng flag
It won't always be a sum since a document can match few queries at once. Unless it's really a `keyword` field; then a simple `terms` aggregation would do.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.