Home > OS >  Elasticsearch query get multiple hits count
Elasticsearch query get multiple hits count

Time:08-18

I have a scenario where my search query has to search the phone number and license number that starts with "861". This query output all the phone number and license number that begins with "861" with hits count. The output has a total count of license numbers and phone numbers.

But I am expecting to get the output hits separately for phone and license numbers as below the output

Below is my query and output. Also my expected output is below that.

GET emp_details_new/_search 
{
   "_source": [],
   "min_score": 0.5,
   "query": {
      "multi_match": {
        "query": "861",
         "fields": ["phone","licence_num"],
         "type": "phrase_prefix"
      }
   }
}

Output:

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : 6.5032897,
    "hits" : [
      {
        "_index" : "emp_details_new",
        "_type" : "_doc",
        "_id" : "20",
        "_score" : 6.5032897,
        "_source" : {
          "id" : 20,
          "firstname" : "Millard",
          "phone" : "1531243932",
          "licence_num" : "8616829169"
        }
      },
      {
        "_index" : "emp_details_new",
        "_type" : "_doc",
        "_id" : "243",
        "_score" : 6.5032897,
        "_source" : {
          "id" : 243,
          "firstname" : "Darbie",
          "phone" : "8617323318",
          "licence_num" : "9154243943"
        }
      },
      {
        "_index" : "emp_details_new",
        "_type" : "_doc",
        "_id" : "252",
        "_score" : 6.5032897,
        "_source" : {
          "id" : 252,
          "firstname" : "Angus",
          "phone" : "2425841984",
          "licence_num" : "8616203799"
        }
      },
      {
        "_index" : "emp_details_new",
        "_type" : "_doc",
        "_id" : "777",
        "_score" : 6.5032897,
        "_source" : {
          "id" : 777,
          "firstname" : "Julio",
          "phone" : "8613789726",
          "licence_num" : "1355139833"
        }
      }
    ]
  }
}

My expected output is to get the separate count for phone number and license number as below.

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 6.5032897,
    "hits" : [
      {
        "_index" : "emp_details_new",
        "_type" : "_doc",
        "_id" : "20",
        "_score" : 6.5032897,
        "_source" : {
          "id" : 20,
          "licence_num" : "8616829169"
        }
      },
      {
        "_index" : "emp_details_new",
        "_type" : "_doc",
        "_id" : "252",
        "_score" : 6.5032897,
        "_source" : {
          "id" : 252,
          "licence_num" : "8616203799"

        }
      }
    ],  
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 6.5032897,
    "hits" : [
      {
        "_index" : "emp_details_new",
        "_type" : "_doc",
        "_id" : "243",
        "_score" : 6.5032897,
        "_source" : {
          "id" : 243,
          "phone" : "8617323318"
        }
      },
      {
        "_index" : "emp_details_new",
        "_type" : "_doc",
        "_id" : "777",
        "_score" : 6.5032897,
        "_source" : {
          "id" : 777,
          "phone" : "8613789726"
        }
      }
    ]
  }
}

CodePudding user response:

What I believe to be an option is this:

GET _msearch
{"index": "test"}
{ "_source": ["id", "licence_num"], "min_score": 0.5, "query": { "multi_match": { "query": "861", "fields": ["licence_num"], "type": "phrase_prefix" } }}
{"index": "test"}
{ "_source": ["id", "phone"], "min_score": 0.5, "query": { "multi_match": { "query": "861", "fields": ["phone"], "type": "phrase_prefix" } }}

Response:

  "responses" : [
    {
      "took" : 2,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 2,
          "relation" : "eq"
        },
        "max_score" : 1.2039728,
        "hits" : [
          {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "eb_apoIBOFCrGsmFSmdS",
            "_score" : 1.2039728,
            "_source" : {
              "licence_num" : "8616829169",
              "id" : 20
            }
          },
          {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "e7_apoIBOFCrGsmFVmeW",
            "_score" : 1.2039728,
            "_source" : {
              "licence_num" : "8616203799",
              "id" : 252
            }
          }
        ]
      },
      "status" : 200
    },
    {
      "took" : 1,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 2,
          "relation" : "eq"
        },
        "max_score" : 1.2039728,
        "hits" : [
          {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "er_apoIBOFCrGsmFUGfI",
            "_score" : 1.2039728,
            "_source" : {
              "phone" : "8617323318",
              "id" : 243
            }
          },
          {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "fL_apoIBOFCrGsmFXmdO",
            "_score" : 1.2039728,
            "_source" : {
              "phone" : "8613789726",
              "id" : 777
            }
          }
        ]
      },
      "status" : 200
    }
  ]
  • Related