一起来玩Elasticsearch,加我微信:wx1250134974
Elasticsearch认证复习准备
https://www.elastic.co/guide/cn/elasticsearch/guide/current/getting-started.html
##多字段搜索概念
用相同或不同的字符串查询一个或多个字段
- 多字符串查询
GET /_search
{
"query": {
"bool": {
"should": [
{ "match": { "title": "War and Peace" }},
{ "match": { "author": "Leo Tolstoy" }}
]
}
}
}
注:两条语句同时匹配的文档比只与一条语句匹配的文档得分要高
GET /_search
{
"query": {
"bool": {
"should": [
{ "match": {
"title": {
"query": "War and Peace",
"boost": 2
}}},
{ "match": {
"author": {
"query": "Leo Tolstoy",
"boost": 2
}}},
{ "bool": {
"should": [
{ "match": { "translator": "Constance Garnett" }},
{ "match": { "translator": "Louise Maude" }}
]
}}
]
}
}
}
注:should中前两个语句权重占到了4/5
- 最佳字段搜索
GET /_search
{
"query": {
"dis_max": {
"queries": [
{ "match": { "title": "Brown fox" }},
{ "match": { "body": "Brown fox" }}
]
}
}
}
注:将任何与任一查询匹配的文档作为结果返回,但只将最佳匹配的评分作为查询的评分结果返回。
GET /_search
{
"query": {
"dis_max": {
"queries": [
{ "match": { "title": "Quick pets" }},
{ "match": { "body": "Quick pets" }}
],
"tie_breaker": 0.3
}
}
}
注:"tie_breaker": 0.3 ,最佳字段评分为主,其他字段评分为辅。
- 最佳字段、多数字段、跨字段的简便写法
GET /_search
{
"multi_match": {
"query": "Quick brown fox",
"type": "best_fields", #最佳字段方式(默认)
"fields": [ "title", "body" ],
"tie_breaker": 0.3, #非最佳字段也给点分
"minimum_should_match": "30%" #每个字段都必须包含30%terms
}
}
注:和上边最佳字段写法效果一样。
GET /_search
{
"multi_match": {
"query": "Quick brown fox",
"fields": "*_title"
}
}
注:字段名字通配
GET /_search
{
"multi_match": {
"query": "Quick brown fox",
"fields": [ "*_title", "chapter_title^2" ]
}
}
注:提升单个字段权重
GET /my_index/_search
{
"query": {
"multi_match": {
"query": "jumping rabbits",
"type": "most_fields",
"fields": [ "title^10", "title.std" ]
}
}
}
注:多数字段方式,多个字段匹配比单个字段匹配分数更高。权重设置类似。
此方式存在的问题:
A、为多数字段匹配 任意 词设计的,而不是在 所有字段 中找到最匹配的。——多个字段匹配得分比单个字段高
B、、不能使用 operator 或 minimum_should_match 参数来降低次相关结果造成的长尾效应。——(效果作用在单个字段匹配的比例,而不是多个语句之间)
C、词频对于每个字段是不一样的,而且它们之间的相互影响会导致不好的排序结果。——字段之间的词频不一样,会引发排序混乱。
4、词中心式可以很好解决多字段(字段中心式)中的问题
PUT /my_index
{
"mappings": {
"person": {
"properties": {
"first_name": {
"type": "string",
"copy_to": "full_name" #考到full_name字段
},
"last_name": {
"type": "string",
"copy_to": "full_name"
},
"full_name": {
"type": "string"
}
}
}
}
}
注:多个字段的数据索引到一个字段中,可以消除上述问题。
GET /books/_search
{
"query": {
"multi_match": {
"query": "peter smith",
"type": "cross_fields",
"fields": [ "title^2", "description" ]
}
}
}
注:cross_fields将所有字段当成一个大字段进行查询
GET /_validate/query?explain
{
"query": {
"multi_match": {
"query": "peter smith",
"type": "cross_fields",
"operator": "and",
"fields": [ "first_name", "last_name" ]
}
}
}
注:词中心式 会使用以下逻辑:
+(first_name:peter last_name:peter)
+(first_name:smith last_name:smith)
GET /_validate/query?explain
{
"query": {
"multi_match": {
"query": "peter smith",
"type": "most_fields",
"operator": "and",
"fields": [ "first_name", "last_name" ]
}
}
}
注:
字段中心式 会使用以下逻辑:
(+first_name:peter +first_name:smith)
(+last_name:peter +last_name:smith)
一起来玩Elasticsearch,加我微信:wx1250134974