elasticsearch 基于属性的弹性搜索权重,elasticsearch,elasticsearch" /> elasticsearch 基于属性的弹性搜索权重,elasticsearch,elasticsearch" />

elasticsearch 基于属性的弹性搜索权重

elasticsearch 基于属性的弹性搜索权重,elasticsearch,elasticsearch,在弹性搜索中,是否有一种方法可以根据搜索查询所用属性以外的属性对结果进行加权。例如,我们搜索字段“name”,但所有将“with_pictures”归因于true的文档的权重更高。您可以在单个字段上使用,这些字段将自动提升 — 在相关性得分中计算更多 — 在查询时,使用boost参数 添加索引数据、映射和搜索查询的工作示例 索引映射: { "mappings": { "properties": { "with_pictur

在弹性搜索中,是否有一种方法可以根据搜索查询所用属性以外的属性对结果进行加权。例如,我们搜索字段“name”,但所有将“with_pictures”归因于true的文档的权重更高。

您可以在单个字段上使用,这些字段将自动提升 — 在相关性得分中计算更多 — 在查询时,使用boost参数

添加索引数据、映射和搜索查询的工作示例

索引映射:

{
  "mappings": {
    "properties": {
      "with_pictures": {
        "type": "boolean",
        "boost": 2 
      },
      "name": {
        "type": "keyword"
      }
    }
  }
}
{
    "name": "A",
    "with_pictures": false
}

{
    "name": "A",
    "with_pictures": true
}
{
    "name": "B",
    "with_pictures": true
}
{
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {
          "bool": {
            "should": [

              {
                "term": {
                  "name": "A"
                }
              },
              {
                "term": {
                  "with_pictures": true
                }
              }
            ]
          }
        }
      ]
    }
  }
}
 "hits": [
      {
        "_index": "fd_cb1",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.4100108,
        "_source": {
          "name": "A",
          "with_pictures": true
        }
      },
      {
        "_index": "fd_cb1",
        "_type": "_doc",
        "_id": "3",
        "_score": 0.9400072,
        "_source": {
          "name": "B",
          "with_pictures": true
        }
      },
      {
        "_index": "fd_cb1",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.4700036,
        "_source": {
          "name": "A",
          "with_pictures": false
        }
      }
    ]
索引数据:

{
  "mappings": {
    "properties": {
      "with_pictures": {
        "type": "boolean",
        "boost": 2 
      },
      "name": {
        "type": "keyword"
      }
    }
  }
}
{
    "name": "A",
    "with_pictures": false
}

{
    "name": "A",
    "with_pictures": true
}
{
    "name": "B",
    "with_pictures": true
}
{
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {
          "bool": {
            "should": [

              {
                "term": {
                  "name": "A"
                }
              },
              {
                "term": {
                  "with_pictures": true
                }
              }
            ]
          }
        }
      ]
    }
  }
}
 "hits": [
      {
        "_index": "fd_cb1",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.4100108,
        "_source": {
          "name": "A",
          "with_pictures": true
        }
      },
      {
        "_index": "fd_cb1",
        "_type": "_doc",
        "_id": "3",
        "_score": 0.9400072,
        "_source": {
          "name": "B",
          "with_pictures": true
        }
      },
      {
        "_index": "fd_cb1",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.4700036,
        "_source": {
          "name": "A",
          "with_pictures": false
        }
      }
    ]
搜索查询:

{
  "mappings": {
    "properties": {
      "with_pictures": {
        "type": "boolean",
        "boost": 2 
      },
      "name": {
        "type": "keyword"
      }
    }
  }
}
{
    "name": "A",
    "with_pictures": false
}

{
    "name": "A",
    "with_pictures": true
}
{
    "name": "B",
    "with_pictures": true
}
{
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {
          "bool": {
            "should": [

              {
                "term": {
                  "name": "A"
                }
              },
              {
                "term": {
                  "with_pictures": true
                }
              }
            ]
          }
        }
      ]
    }
  }
}
 "hits": [
      {
        "_index": "fd_cb1",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.4100108,
        "_source": {
          "name": "A",
          "with_pictures": true
        }
      },
      {
        "_index": "fd_cb1",
        "_type": "_doc",
        "_id": "3",
        "_score": 0.9400072,
        "_source": {
          "name": "B",
          "with_pictures": true
        }
      },
      {
        "_index": "fd_cb1",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.4700036,
        "_source": {
          "name": "A",
          "with_pictures": false
        }
      }
    ]
搜索结果:

{
  "mappings": {
    "properties": {
      "with_pictures": {
        "type": "boolean",
        "boost": 2 
      },
      "name": {
        "type": "keyword"
      }
    }
  }
}
{
    "name": "A",
    "with_pictures": false
}

{
    "name": "A",
    "with_pictures": true
}
{
    "name": "B",
    "with_pictures": true
}
{
  "query": {
    "bool": {
      "minimum_should_match": 1,
      "should": [
        {
          "bool": {
            "should": [

              {
                "term": {
                  "name": "A"
                }
              },
              {
                "term": {
                  "with_pictures": true
                }
              }
            ]
          }
        }
      ]
    }
  }
}
 "hits": [
      {
        "_index": "fd_cb1",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.4100108,
        "_source": {
          "name": "A",
          "with_pictures": true
        }
      },
      {
        "_index": "fd_cb1",
        "_type": "_doc",
        "_id": "3",
        "_score": 0.9400072,
        "_source": {
          "name": "B",
          "with_pictures": true
        }
      },
      {
        "_index": "fd_cb1",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.4700036,
        "_source": {
          "name": "A",
          "with_pictures": false
        }
      }
    ]
同时满足
名称
属性
条件的文档得分最高。但是具有
名称:B
带有图片:true
的文档的得分高于
名称:A
带有图片:false
(因为在带有图片的
上应用了增强)


您还可以参考允许您修改通过查询检索的文档分数的内容。

您是否有机会查看我的答案,并期待从您那里获得反馈:)感谢您接受答案:)如果您也能对我的答案进行投票,那将是非常好的:)