multi_match单字符串多字段的三种场景:
- 最佳字段(Best Fields)
当字段之间存在互相竞争,又有相互关联。如:title和body这样的情况,评分来自最佳字段; - 多数字段(Most Fields)
处理英文内容时:一种常见的手段是,在主字段加上英文分词器(English Analyzer),抽取词干,加入同义词,以匹配更多的文档。相同的文本,加入子字段加上标准分词器(Standard Analyzer),以提供更加精准的匹配。其他字段作为匹配温度提高相关度的信号。匹配字段越多越好。 - 混合字段(Cross Fields)
对于某些实体,例如人名,地址,图书信息。需要在多个字段中确定信息,单个字段只能作为整体的一部分。希望在任何这些列出的字段中找到尽可能多的词。
Best Fields案例
数据准备
PUT /blogs/_doc/1
{
"title": "Quick brown rabbits",
"body": "Brown rabbits are commonly seen."
}
PUT /blogs/_doc/2
{
"title": "Keeping pets healthy",
"body": "My quick brown fox eats rabbits on a regular basis."
}
multi_match
POST blogs/_search
{
"query": {
"multi_match": {
"type": "best_fields",#
"query": "Quick pets",
"fields": ["title","body"],
"tie_breaker": 0.2,
"minimum_should_match": "20%"
}
}
}
说明:
- best_fields 可以不用指定,默认类型
- "tie_breaker": 0.2 也可以用来调整_score的策略,具体参考Elasticsearch学习笔记(17)
- minimum_should_match:
在elastic官方文档上介绍就是最小匹配度,它有多种匹配方式
对于minimum_should_match设置值不同的值,下面举几个案例:- minimum_should_match:"3"
无论可选子句的数量如何,都表示固定值. - minimum_should_match:"-2"
表示可选子句的总数减去此数字应该是必需的。 - minimum_should_match:"75%"
表示最少匹配的子句个数,例如有五个可选子句,最少的匹配个数为5*75%=3.75.向下取整为3,这就表示五个子句最少要匹配其中三个才能查到. - minimum_should_match:"-25%"
和上面的类似,只是计算方式不一样,假如也是五个子句,5*25%=1.25,向下取整为1,5最少匹配个数为5-1=4. - minimum_should_match:"3<90%"
表示如果可选子句的数量等于(或小于)设置的值,则它们都是必需的,但如果它大于设置的值,则适用规范。在这个例子中:如果有1到3个子句,则它们都是必需的,但是对于4个或更多子句,只需要90%的匹配度.
- minimum_should_match:"3"
result
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.815141,
"hits" : [
{
"_index" : "blogs",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.815141,
"_source" : {
"title" : "Keeping pets healthy",
"body" : "My quick brown fox eats rabbits on a regular basis."
}
},
{
"_index" : "blogs",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.6931471,
"_source" : {
"title" : "Quick brown rabbits",
"body" : "Brown rabbits are commonly seen."
}
}
]
}
}
Most Fields案例
场景:英文分词器导致精确度降低,时态信息丢失。
数据准备:
# 创建索引
PUT /titles
{
"mappings": {
"properties": {
"title": {
"type": "text",
"analyzer": "english"
}
}
}
}
# 添加数据
POST titles/_bulk
{ "index": { "_id": 1 }}
{ "title": "My dog barks" }
{ "index": { "_id": 2 }}
{ "title": "I see a lot of barking dogs on the road " }
# 查询
GET titles/_search
{
"query": {
"match": {
"title": "barking dogs"
}
}
}
result
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 0.42221838,
"hits" : [
{
"_index" : "titles",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.42221838,
"_source" : {
"title" : "My dog barks"
}
},
{
"_index" : "titles",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.320886,
"_source" : {
"title" : "I see a lot of barking dogs on the road "
}
}
]
}
}
result中不难看出文档2明显更符合查询条件,为什么评分反而更低呢?
我们可以对检索条件分词后分析
GET _analyze
{
"analyzer": "english",
"text":"barking dogs"
}
# 结果
{
"tokens" : [
{
"token" : "bark",
"start_offset" : 0,
"end_offset" : 7,
"type" : "<ALPHANUM>",
"position" : 0
},
{
"token" : "dog",
"start_offset" : 8,
"end_offset" : 12,
"type" : "<ALPHANUM>",
"position" : 1
}
]
}
这两个文档中输入的term是一样的,第一篇文档语句更短,所以分数更高。
问题解决,添加子字段并设置standard analyzer。english analyzer可以提升搜索的recall的值,standard analyzer可以提升搜索的精度:
#删除
DELETE /titles
#重新创建索引
PUT /titles
{
"mappings": {
"properties": {
"title": {
"type": "text",
"analyzer": "english",
"fields": {"std": {"type": "text","analyzer": "standard"}}
}
}
}
}
#数据
POST titles/_bulk
{ "index": { "_id": 1 }}
{ "title": "My dog barks" }
{ "index": { "_id": 2 }}
{ "title": "I see a lot of barking dogs on the road " }
#查询1 multi_match默认type是best_fields,这里需要修改一下
GET /titles/_search
{
"query": {
"multi_match": {
"query": "barking dogs",
"type": "most_fields",
"fields": [ "title", "title.std" ]
}
}
}
#查询2 ^10增加title字段的权重
GET /titles/_search
{
"query": {
"multi_match": {
"query": "barking dogs",
"type": "most_fields",
"fields": [ "title^10", "title.std" ]
}
}
}
查询1结果:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.4569323,
"hits" : [
{
"_index" : "titles",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.4569323,
"_source" : {
"title" : "I see a lot of barking dogs on the road "
}
},
{
"_index" : "titles",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.42221838,
"_source" : {
"title" : "My dog barks"
}
}
]
}
}
Cross Fields案例
跨字段搜索的案例(该案例可能不太合适)
#数据准备
PUT address/_doc/1
{
"street": "5 Poland Street",
"city": "London",
"contry":"United Kingdom",
"postcode":"W1V 3DG"
}
# 使用multi_match 的 "type": "most_fields", "operator": "and", 查询不到
POST address/_search
{
"query": {
"multi_match": {
"query": "Poland Street W1V",
"type": "most_fields",
"operator": "and",
"fields": ["street","city","contry","postcode"]
}
}
}
办法1:使用copy_to字段可以解决,但需要更大的存储空间。
办法2:multi_match时使用"type":"cross_fields",这种类型支持operator,较copy_to而言可以在搜索时为单个字段增加权重。
POST address/_search
{
"query": {
"multi_match": {
"query": "Poland Street W1V",
"type": "cross_fields",
"operator": "and",
"fields": ["street","city","contry","postcode"]
}
}
}
#结果
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.8630463,
"hits" : [
{
"_index" : "address",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.8630463,
"_source" : {
"street" : "5 Poland Street",
"city" : "London",
"contry" : "United Kingdom",
"postcode" : "W1V 3DG"
}
}
]
}
}