3-Elasticsearch深入搜索-多字段搜索

一起来玩Elasticsearch,加我微信:wx1250134974

Elasticsearch认证复习准备

https://www.elastic.co/guide/cn/elasticsearch/guide/current/getting-started.html

 

##多字段搜索概念

用相同或不同的字符串查询一个或多个字段

 

  1. 多字符串查询

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

 

 

  1. 最佳字段搜索

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 ,最佳字段评分为主,其他字段评分为辅。

 

 

 

 

  1. 最佳字段、多数字段、跨字段的简便写法

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

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值