重视弹性搜索领域

我有一个包含产品的elasticsearch索引,我试图创建一个具有文本字段功能的搜索列表产品。

数据集的排序示例

{"name": "foo", "count": 10} {"name": "bar", "count": 5} {"name": "foo bar"} {"name": "foo baz", "count": 20}

一开始,我是在要求。

GET /product
/_search
{
  "query": {
    "match": {"name": "foo"}
  }
}

效果很好,但现在我想增加一些产品的重量(count现场)

我正在使用此查询

GET /product/_search
{
  "query": {
    "function_score": {
      "query": {
        "match": {"name": "foo bar"}
      },
      "field_value_factor": {
        "field": "count",
        "missing": 0
      }
    }
  }
}

但与此查询首先我有foo,然后barfoo bar看来,名称匹配具有比计数不太重要,我想有foo bar那么foobar

但是寻找foo我想要的foo bazfoofoo bar

ESCoder

但是寻找foo我想要foo baz,foo和foo bar

添加带有索引数据,搜索查询和搜索结果的工作示例

请参考功能分数查询以获取详细说明。

索引数据:

{"name": "foo", "count": 10} 
{"name": "bar", "count": 5} 
{"name": "foo bar"} 
{"name": "foo baz", "count": 20}

搜索查询:

但是寻找foo我想要foo baz,foo和foo bar

{
    "query": {
        "function_score": {
            "query": {
                "bool": {
                    "should": [
                        {
                            "match": {
                                "name": {
                                    "query": "foo"
                                }
                            }
                        }
                    ]
                }
            },
            "functions": [
                {
                    "field_value_factor": {
                        "field": "count",
                        "factor": 1.0,
                        "missing": 0
                    }
                }
            ],
            "boost_mode": "multiply"
        }
    }
}

搜索结果:

"hits": [
      {
        "_index": "stof_64169215",
        "_type": "_doc",
        "_id": "4",
        "_score": 6.2774796,
        "_source": {
          "name": "foo baz",
          "count": 20
        }
      },
      {
        "_index": "stof_64169215",
        "_type": "_doc",
        "_id": "1",
        "_score": 4.1299205,
        "_source": {
          "name": "foo",
          "count": 10
        }
      },
      {
        "_index": "stof_64169215",
        "_type": "_doc",
        "_id": "3",
        "_score": 0.0,
        "_source": {
          "name": "foo bar"
        }
      }
    ]

更新1:

我想要foo bar然后foo和bar

搜索查询:

{
    "query": {
        "function_score": {
            "query": {
                "bool": {
                    "should": [
                        {
                            "match": {
                                "name": {
                                    "query": "foo bar"
                                }
                            }
                        }
                    ]
                }
            },
            "functions": [
                {
                    "field_value_factor": {
                        "field": "count",
                        "factor": 1.0,
                        "missing": 0,
                        "modifier": "sqrt"
                    }
                }
            ],
            "boost_mode": "sum"
        }
    }
}

说明API结果:

要了解上述搜索查询,您需要了解如何计算查询的分数。

  1. 搜索是针对制造"name": "foo bar",这应该理想回报foo bar,然后foo,然后bar通过使用普通匹配查询foo bar(没有功能得分查询),您将获得结果。
  2. 现在根据您的用例,您要在count使用Function score query字段上增加权重,该字段允许您修改查询检索的文档的分数。
  3. 此外,可以组合几个功能。function_score查询提供几种类型的得分函数。使用field_value_factor函数可以使用文档中的字段来影响得分。
  4. 在field_value_factor中,有几个选项:

factor-与字段值相乘的可选因子,默认为1

修饰符-应用于字段值
丢失的修饰符-如果文档没有该字段,则使用该值。

生成以下得分公式:

sqrt(1.0 * doc ['count']。value)

现在,对于包含的文档foo bar,没有count字段,因此将使用缺失值(在查询中定义,即9)。分数将是sqrt(1.0 * 9) = 3.0

如果您采用任何小于9的缺失值,那么结果的顺序将改变。因为该count字段的得分会有所不同(当您将缺少的值指定为时0,则foo bar仅根据match查询获得得分,而field_value_factor不会添加任何得分)。最终分数是根据match查询+ field_value_factorcount现场)得出的因此,的总得分foo bar将小于其他文档。

例如:对于foo bar,最终得分计算为0.78038335+3.0=3.7803833请仔细阅读下面的结果,以详细了解如何计算得分。

请浏览此博客以了解评分在Elasticsearch中的工作原理

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 3,
      "relation": "eq"
    },
    "max_score": 3.7803833,
    "hits": [
      {
        "_shard": "[stof_64169215][0]",
        "_node": "fVeabsK0Q1GnCZ_8oROXjA",
        "_index": "stof_64169215",
        "_type": "_doc",
        "_id": "3",
        "_score": 3.7803833,
        "_source": {
          "name": "foo bar"
        },
        "_explanation": {
          "value": 3.7803833,
          "description": "sum of",
          "details": [
            {
              "value": 0.78038335,
              "description": "sum of:",
              "details": [
                {
                  "value": 0.39019167,
                  "description": "weight(name:foo in 0) [PerFieldSimilarity], result of:",
                  "details": [
                    {
                      "value": 0.39019167,
                      "description": "score(freq=1.0), computed as boost * idf * tf from:",
                      "details": [
                        {
                          "value": 2.2,
                          "description": "boost",
                          "details": []
                        },
                        {
                          "value": 0.47000363,
                          "description": "idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:",
                          "details": [
                            {
                              "value": 2,
                              "description": "n, number of documents containing term",
                              "details": []
                            },
                            {
                              "value": 3,
                              "description": "N, total number of documents with field",
                              "details": []
                            }
                          ]
                        },
                        {
                          "value": 0.37735844,
                          "description": "tf, computed as freq / (freq + k1 * (1 - b + b * dl / avgdl)) from:",
                          "details": [
                            {
                              "value": 1.0,
                              "description": "freq, occurrences of term within document",
                              "details": []
                            },
                            {
                              "value": 1.2,
                              "description": "k1, term saturation parameter",
                              "details": []
                            },
                            {
                              "value": 0.75,
                              "description": "b, length normalization parameter",
                              "details": []
                            },
                            {
                              "value": 2.0,
                              "description": "dl, length of field",
                              "details": []
                            },
                            {
                              "value": 1.3333334,
                              "description": "avgdl, average length of field",
                              "details": []
                            }
                          ]
                        }
                      ]
                    }
                  ]
                },
                {
                  "value": 0.39019167,
                  "description": "weight(name:bar in 0) [PerFieldSimilarity], result of:",
                  "details": [
                    {
                      "value": 0.39019167,
                      "description": "score(freq=1.0), computed as boost * idf * tf from:",
                      "details": [
                        {
                          "value": 2.2,
                          "description": "boost",
                          "details": []
                        },
                        {
                          "value": 0.47000363,
                          "description": "idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:",
                          "details": [
                            {
                              "value": 2,
                              "description": "n, number of documents containing term",
                              "details": []
                            },
                            {
                              "value": 3,
                              "description": "N, total number of documents with field",
                              "details": []
                            }
                          ]
                        },
                        {
                          "value": 0.37735844,
                          "description": "tf, computed as freq / (freq + k1 * (1 - b + b * dl / avgdl)) from:",
                          "details": [
                            {
                              "value": 1.0,
                              "description": "freq, occurrences of term within document",
                              "details": []
                            },
                            {
                              "value": 1.2,
                              "description": "k1, term saturation parameter",
                              "details": []
                            },
                            {
                              "value": 0.75,
                              "description": "b, length normalization parameter",
                              "details": []
                            },
                            {
                              "value": 2.0,
                              "description": "dl, length of field",
                              "details": []
                            },
                            {
                              "value": 1.3333334,
                              "description": "avgdl, average length of field",
                              "details": []
                            }
                          ]
                        }
                      ]
                    }
                  ]
                }
              ]
            },
            {
              "value": 3.0,
              "description": "min of:",
              "details": [
                {
                  "value": 3.0,
                  "description": "field value function: sqrt(doc['count'].value?:9.0 * factor=1.0)",
                  "details": []
                },
                {
                  "value": 3.4028235E38,
                  "description": "maxBoost",
                  "details": []
                }
              ]
            }
          ]
        }
      },
      {
        "_shard": "[stof_64169215][0]",
        "_node": "fVeabsK0Q1GnCZ_8oROXjA",
        "_index": "stof_64169215",
        "_type": "_doc",
        "_id": "1",
        "_score": 3.685826,
        "_source": {
          "name": "foo",
          "count": 10
        },
        "_explanation": {
          "value": 3.685826,
          "description": "sum of",
          "details": [
            {
              "value": 0.52354836,
              "description": "sum of:",
              "details": [
                {
                  "value": 0.52354836,
                  "description": "weight(name:foo in 0) [PerFieldSimilarity], result of:",
                  "details": [
                    {
                      "value": 0.52354836,
                      "description": "score(freq=1.0), computed as boost * idf * tf from:",
                      "details": [
                        {
                          "value": 2.2,
                          "description": "boost",
                          "details": []
                        },
                        {
                          "value": 0.47000363,
                          "description": "idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:",
                          "details": [
                            {
                              "value": 2,
                              "description": "n, number of documents containing term",
                              "details": []
                            },
                            {
                              "value": 3,
                              "description": "N, total number of documents with field",
                              "details": []
                            }
                          ]
                        },
                        {
                          "value": 0.50632906,
                          "description": "tf, computed as freq / (freq + k1 * (1 - b + b * dl / avgdl)) from:",
                          "details": [
                            {
                              "value": 1.0,
                              "description": "freq, occurrences of term within document",
                              "details": []
                            },
                            {
                              "value": 1.2,
                              "description": "k1, term saturation parameter",
                              "details": []
                            },
                            {
                              "value": 0.75,
                              "description": "b, length normalization parameter",
                              "details": []
                            },
                            {
                              "value": 1.0,
                              "description": "dl, length of field",
                              "details": []
                            },
                            {
                              "value": 1.3333334,
                              "description": "avgdl, average length of field",
                              "details": []
                            }
                          ]
                        }
                      ]
                    }
                  ]
                }
              ]
            },
            {
              "value": 3.1622777,
              "description": "min of:",
              "details": [
                {
                  "value": 3.1622777,
                  "description": "field value function: sqrt(doc['count'].value?:9.0 * factor=1.0)",
                  "details": []
                },
                {
                  "value": 3.4028235E38,
                  "description": "maxBoost",
                  "details": []
                }
              ]
            }
          ]
        }
      },
      {
        "_shard": "[stof_64169215][0]",
        "_node": "fVeabsK0Q1GnCZ_8oROXjA",
        "_index": "stof_64169215",
        "_type": "_doc",
        "_id": "2",
        "_score": 2.7596164,
        "_source": {
          "name": "bar",
          "count": 5
        },
        "_explanation": {
          "value": 2.7596164,
          "description": "sum of",
          "details": [
            {
              "value": 0.52354836,
              "description": "sum of:",
              "details": [
                {
                  "value": 0.52354836,
                  "description": "weight(name:bar in 0) [PerFieldSimilarity], result of:",
                  "details": [
                    {
                      "value": 0.52354836,
                      "description": "score(freq=1.0), computed as boost * idf * tf from:",
                      "details": [
                        {
                          "value": 2.2,
                          "description": "boost",
                          "details": []
                        },
                        {
                          "value": 0.47000363,
                          "description": "idf, computed as log(1 + (N - n + 0.5) / (n + 0.5)) from:",
                          "details": [
                            {
                              "value": 2,
                              "description": "n, number of documents containing term",
                              "details": []
                            },
                            {
                              "value": 3,
                              "description": "N, total number of documents with field",
                              "details": []
                            }
                          ]
                        },
                        {
                          "value": 0.50632906,
                          "description": "tf, computed as freq / (freq + k1 * (1 - b + b * dl / avgdl)) from:",
                          "details": [
                            {
                              "value": 1.0,
                              "description": "freq, occurrences of term within document",
                              "details": []
                            },
                            {
                              "value": 1.2,
                              "description": "k1, term saturation parameter",
                              "details": []
                            },
                            {
                              "value": 0.75,
                              "description": "b, length normalization parameter",
                              "details": []
                            },
                            {
                              "value": 1.0,
                              "description": "dl, length of field",
                              "details": []
                            },
                            {
                              "value": 1.3333334,
                              "description": "avgdl, average length of field",
                              "details": []
                            }
                          ]
                        }
                      ]
                    }
                  ]
                }
              ]
            },
            {
              "value": 2.236068,
              "description": "min of:",
              "details": [
                {
                  "value": 2.236068,
                  "description": "field value function: sqrt(doc['count'].value?:9.0 * factor=1.0)",
                  "details": []
                },
                {
                  "value": 3.4028235E38,
                  "description": "maxBoost",
                  "details": []
                }
              ]
            }
          ]
        }
      }
    ]
  }
}

搜索结果:

"hits": [
      {
        "_index": "stof_64169215",
        "_type": "_doc",
        "_id": "3",
        "_score": 3.7803833,
        "_source": {
          "name": "foo bar"
        }
      },
      {
        "_index": "stof_64169215",
        "_type": "_doc",
        "_id": "1",
        "_score": 3.685826,
        "_source": {
          "name": "foo",
          "count": 10
        }
      },
      {
        "_index": "stof_64169215",
        "_type": "_doc",
        "_id": "2",
        "_score": 2.7596164,
        "_source": {
          "name": "bar",
          "count": 5
        }
      }
    ]

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章