天天看点

单字符串多字段查询:Dis Max Que

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."

}

博客标题

● ⽂档 1 中出现 “Brown”

● 博客内容

● ⽂档 1 中出现了 “Brown”

● “Brown fox” 在⽂档 2 中全部出

现,并且保持和查询⼀致的顺序

(⽬测相关性最⾼)

POST /blogs/_search

{

    "query": {

        "bool": {

            "should": [

                { "match": { "title": "Brown fox" }},

                { "match": { "body":  "Brown fox" }}

            ]

        }

    }

}

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.90425634,
    "hits" : [
      {
        "_index" : "blogs",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.90425634,
        "_source" : {
          "title" : "Quick brown rabbits",
          "body" : "Brown rabbits are commonly seen."
        }
      },
      {
        "_index" : "blogs",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.77041256,
        "_source" : {
          "title" : "Keeping pets healthy",
          "body" : "My quick brown fox eats rabbits on a regular basis."
        }
      }
    ]
  }
}
           

算分过程

● 查询 should 语句中的两个查询

● 加和两个查询的评分

● 乘以匹配语句的总数

● 除以所有语句的总数

单字符串多字段查询:Dis Max Que

Disjunction Max Query 查询

● 上例中,title 和 body 相互竞争

● 不应该将分数简单叠加,⽽是应该找到单个最佳匹配的字段的评分

● Disjunction Max Query

● 将任何与任⼀查询匹配的⽂档作为结果返回。采⽤字段上最匹配的评分最终评分

POST blogs/_search

{

    "query": {

        "dis_max": {

            "queries": [

                { "match": { "title": "Quick pets" }},

                { "match": { "body":  "Quick pets" }}

            ]

        }

    }

}

{
  "took" : 11,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.6931472,
    "hits" : [
      {
        "_index" : "blogs",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.6931472,
        "_source" : {
          "title" : "Keeping pets healthy",
          "body" : "My quick brown fox eats rabbits on a regular basis."
        }
      },
      {
        "_index" : "blogs",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.6931472,
        "_source" : {
          "title" : "Quick brown rabbits",
          "body" : "Brown rabbits are commonly seen."
        }
      }
    ]
  }
}
           

     有⼀些情况下,同时匹配 title 和 body 字段的

⽂档⽐只与⼀个字段匹配的⽂档的相关度更⾼

    但 disjunction max query 查询只会简单地使

⽤单个最佳匹配语句的评分 _score 作为整体

评分。怎么办?

POST blogs/_search

{

    "query": {

        "dis_max": {

            "queries": [

                { "match": { "title": "Quick pets" }},

                { "match": { "body":  "Quick pets" }}

            ]

        }

    }

}

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.6931472,
    "hits" : [
      {
        "_index" : "blogs",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.6931472,
        "_source" : {
          "title" : "Keeping pets healthy",
          "body" : "My quick brown fox eats rabbits on a regular basis."
        }
      },
      {
        "_index" : "blogs",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.6931472,
        "_source" : {
          "title" : "Quick brown rabbits",
          "body" : "Brown rabbits are commonly seen."
        }
      }
    ]
  }
}
           

通过 Tie Breaker 参数调整

1. 获得最佳匹配语句的评分 _score 。

2. 将其他匹配语句的评分与 tie_breaker 相乘

3. 对以上评分求和并规范化

  Tier Breaker 是⼀个介于 0-1 之间的浮点数。0

代表使⽤最佳匹配;1 代表所有语句同等重要。

POST blogs/_search

{

    "query": {

        "dis_max": {

            "queries": [

                { "match": { "title": "Quick pets" }},

                { "match": { "body":  "Quick pets" }}

            ],

            "tie_breaker": 0.2

        }

    }

}

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 0.8151411,
    "hits" : [
      {
        "_index" : "blogs",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 0.8151411,
        "_source" : {
          "title" : "Keeping pets healthy",
          "body" : "My quick brown fox eats rabbits on a regular basis."
        }
      },
      {
        "_index" : "blogs",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.6931472,
        "_source" : {
          "title" : "Quick brown rabbits",
          "body" : "Brown rabbits are commonly seen."
        }
      }
    ]
  }
}
           
es

继续阅读