es查询multi_match多字段匹配

Posted xiejunna

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了es查询multi_match多字段匹配相关的知识,希望对你有一定的参考价值。

语句
es查询的数据结构较简单时,数据示例如下:


  "took" : 11,
  "timed_out" : false,
  "_shards" : 
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  ,
  "hits" : 
    "total" : 1,
    "max_score" : 0.0,
    "hits" : [
      
        "_index" : "my_index_name-20220614",
        "_type" : "doc",
        "_id" : "123",
        "_score" : 0.0,
        "_source" : 
          "qd_data" : "123456",
          "date" : "20220614",
          "enddatetime" : "2022-06-14 00:10:11.298",
          "bizno" : "456789",
          "requesttime" : "20220614000011522",
          "@timestamp" : "2022-06-13T16:00:10.726Z",
          "begindatetime" : "2022-06-14 00:00:10.726",
          "jobid" : "1234567890",
          "id" : "123",
          "version" : "1",
          "reqtsn" : "123456123456123456",
          "sys_data" : "xyz"
        
      
    ]
  


查询语句

POST /my_index_name-20220615/doc/_search?

	"query": 
		"bool": 
			"filter": [
				[
					"multi_match": 
						"type": "best_fields",
						"query": "这里放查询的内容,例如:123456123456123456",
						"lenient": true
					
				]
			],
			"should": []
		
	

es查询的数据结构比较复杂时,数据示例如下:


  "took" : 11,
  "timed_out" : false,
  "_shards" : 
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  ,
  "hits" : 
    "total" : 1,
    "max_score" : 0.0,
    "hits" : [
      
        "_index" : "my_index_name-20220614",
        "_type" : "doc",
        "_id" : "123",
        "_score" : 0.0,
        "_source" : 
          "qd_data" : [
		  
              "qd_remsg" : "",
              "qd_params_sn" : "123456",
              "qd_hostno" : "123",
              "qd_haoshi" : "0.543",
              "qd_sendparams" : "aaa",
              "qd_rtimeout" : 3000,
              "qd_ctimeout" : 2000,
              "qd_jfflag" : "0",
              "qd_endtime" : "2022-06-14 00:01:11.111",
              "qd_resultcode" : "",
              "qd_starttime" : "2022-06-14 00:01:10.222",
              "qd_returncode" : "1",
              "errcode" : "200",
              "qd_requestsn" : "123456",
              "qd_remsg" : "一致",
              "qd_reno" : "123456",
              "qdresult" : "MSG":"success","guid":"123456","RES":"1"
            
		  ],
          "date" : "20220614",
          "enddatetime" : "2022-06-14 00:10:11.298",
          "bizno" : "456789",
          "requesttime" : "20220614000011511",
          "@timestamp" : "2022-06-13T16:00:10.726Z",
          "begindatetime" : "2022-06-14 00:00:10.726",
		  "sysreq_data": 
            "reqsn" : "123456",
            "aaa" : "我是张三",
            "bbb" : "2323",
            "ccc" : "111",
            "requesttime" : "20220614000010511"
          ,
          "jobid" : "1234567890",
          "id" : "123",
          "version" : "1",
          "requestsn" : "123456",
          "sys_data" : "xyz"
        
      
    ]
  

查询语句:

POST /my_index_name-20220615/doc/_search?

	"query": 
		"bool": 
			"filter": [
				[
					"multi_match": 
						"type": "best_fields",
						"query": "sysreq_data=我是张三",
						"lenient": true
					
				]
			],
			"should": []
		
	

字段释义:
lenient:当查询报错时是否忽略该文档,默认为false
type: 定义内部查询方式和打分方式

描述
best_fields按照match检索,所有字段单独计算得分并取最高分的field为最终_score,默认值,但不建议使用,数据量上来后查询性能会下降
most_fields按照match检索,融合所有field得分为最终_score
cross_fields将fields中的所有字段融合成一个大字段进行match检索,此时要求所有字段使用相同分析器
phrase按照match_phrase检索,默认slop为0,执行短语精确匹配,所以即便设置 minimum_should_match 也无效; 取最高字段得分
phrase_prefix按照match_phrase_prefix检索,滑动步长slop默认为0;取最高字段得分
bool_prefix按照match_bool_prefix检索

官方文档参考:https://www.elastic.co/guide/cn/elasticsearch/guide/current/multi-match-query.html

以上是关于es查询multi_match多字段匹配的主要内容,如果未能解决你的问题,请参考以下文章

Elasticsearch系列---多字段搜索

ElasticSearch_06_ES数据查询

ElasticSearch_06_ES数据查询

ElasticSearch_06_ES数据查询

ElasticSearch_06_ES数据查询

[Elasticsearch] 多字段搜索 - multi_match查询和多数字段