《Elastic Stack 实战手册》——三、产品能力——3.4.入门篇——3.4.2.Elasticsearch基础应用——3.4.2.3.Search通过Kibana(13) https://developer.aliyun.com/article/1231057
Multi-match
多字段匹配,可以在多个字段中匹配查询相关信息,通过 type 参数可以调整结果集:
#查询商品名称和品牌名称中包含苹果的文档信息 POST /my_goods/_search { "query": { "multi_match": { "query": "苹果", "type": "best_fields", "fields": ["goodsName","brandName"], "tie_breaker": 0.3 } } }
type 参数类型详解:
l best_fields :默认,匹配 fields,将评分最高的分数做为整个查询的分数返回;
l most_fields:查询匹配的文档,并且返回各个字段的分数之和的平均值;
l cross_fields:跨字段匹配,匹配多个字段中是否包含查询词组,对每个字段分别进行打分,然后执行 max 运算获取打分最高的;
l phrase:以 match_phrase 方式运行查询,并返回最佳匹配的评分做为总评分;
l phrase_prefix:以 match_phrase_prefix 方式运行查询,并返回最佳匹配的评分做为总评分;
l bool_prefix:在每个字段上运行 match_bool_prefix 查询,并组合每个字段的评分,详情参考 bool_prefix 以 cross_fields 为例进行实战讲解。
#插入测试数据 PUT my_shop { "settings": { "number_of_shards": 1, "number_of_replicas": 1 }, "mappings": { "properties": { "firstName":{ "type":"text" }, "lastName":{ "type":"text" } } } } POST my_shop/_bulk {"index":{"_id":1}} {"first_name":"Will","last_name":"Smith","age":25} {"index":{"_id":2}} {"first_name":"Smith","last_name":"hello","age":21} {"index":{"_id":3}} {"first_name":"Will","last_name":"hello","age":20} #查询姓名为 Will Smith 的信息 GET /my_shop/_search { "query": { "multi_match" : { "query": "Will Smith", "type": "cross_fields", "fields": [ "first_name^2", "last_name" ], "operator": "and" } } } #返回 "max_score" : 1.9208363, "hits" : [ { "_index" : "my_shop", "_type" : "_doc", "_id" : "1", "_score" : 1.9208363, "_source" : { "first_name" : "Will", "last_name" : "Smith", "age" : 25 } } ]
另外,first_name 提升了权重,默认为1。
《Elastic Stack 实战手册》——三、产品能力——3.4.入门篇——3.4.2.Elasticsearch基础应用——3.4.2.3.Search通过Kibana(15) https://developer.aliyun.com/article/1231055