You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Elasticsearch中分别统计手机号与许可证号的匹配结果数

分别获取以"861"开头的手机号与许可证号匹配结果及计数

问题背景

需要在emp_details_new索引中查询以"861"开头的phone(手机号)和licence_num(许可证号)字段。当前使用multi_match短语前缀查询能得到总计4条匹配,但需要拆分两个字段的匹配结果,分别获取各自的条目及计数。

当前查询语句

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

当前输出结果

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

修正后的期望输出(原格式存在重复键问题)

{
  "licence_num_matches": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "hits": [
      {
        "_index": "emp_details_new",
        "_type": "_doc",
        "_id": "20",
        "_source": {
          "id": 20,
          "licence_num": "8616829169"
        }
      },
      {
        "_index": "emp_details_new",
        "_type": "_doc",
        "_id": "252",
        "_source": {
          "id": 252,
          "licence_num": "8616203799"
        }
      }
    ]
  },
  "phone_matches": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "hits": [
      {
        "_index": "emp_details_new",
        "_type": "_doc",
        "_id": "243",
        "_source": {
          "id": 243,
          "phone": "8617323318"
        }
      },
      {
        "_index": "emp_details_new",
        "_type": "_doc",
        "_id": "777",
        "_source": {
          "id": 777,
          "phone": "8613789726"
        }
      }
    ]
  }
}

实现方案:使用Multi-Search API

通过一次请求执行两个独立的match_phrase_prefix查询,分别针对两个字段,同时指定返回所需字段:

GET _msearch
{"index": "emp_details_new"}
{
  "_source": ["id", "licence_num"],
  "min_score": 0.5,
  "query": {
    "match_phrase_prefix": {
      "licence_num": "861"
    }
  }
}
{"index": "emp_details_new"}
{
  "_source": ["id", "phone"],
  "min_score": 0.5,
  "query": {
    "match_phrase_prefix": {
      "phone": "861"
    }
  }
}

效果说明

  • 第一个查询返回licence_num以"861"开头的所有条目及计数(共2条)
  • 第二个查询返回phone以"861"开头的所有条目及计数(共2条)
  • 结果会按查询顺序返回两个独立的结果集,完全满足分别统计的需求

内容的提问来源于stack exchange,提问作者Green

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.22 17:27:37