具有不同权重的多字段上的弹性搜索完成建议器



我在Elasticsearch中使用完成建议器来允许部分单词匹配查询。在我的索引 (products_index) 中,我希望能够同时查询product_name字段和品牌字段。以下是我的映射:

POST /product_index
mappings: {
  products: {
    properties: {
      brand: {
        type: "string",
        analyzer: "english"
      },
      product_name: {
        type: "string",
        analyzer: "english"
      },
      id: {
        type: "long"
      },
      lookup_count: {
        type: "long"
      },
      suggest: {
        type: "completion",
        analyzer: "simple",
        payloads: true,
        preserve_separators: true,
        preserve_position_increments: true,
        max_input_length: 50
      },
      upc: {
        type: "string"
      }
    }
  }
}

这是我的数据:

POST /product_index/products/2
{
  id: 2,
  brand: "Coca-Cola",
  product_name: "Classic Coke",
  suggest: {
    input: [
      "Classic Coke",
      "Coca-Cola"
    ],
    output: "Classic Coke - Coca-Cola",
    payload: {
      id: 2,
      product_name: "Classic Coke",
      brand: "Coca-Cola",
      popularity: 10
    },
    weight: 0
  }
}

这是我的查询:

POST /product_index/_search
"suggest": {
  "product_suggest": {
    "text": 'coca-co',
    "completion": {
      "field": 'suggest'
    }
  }
}

这很好用,除了我想给product_name字段比品牌字段更高的权重。有什么方法可以实现吗?我已经研究了这篇关于使用布尔查询的文章,但我对 Elasticsearch 很陌生,不确定如何在完成建议器的情况下应用它。

多谢!

正如Redox所说,完成建议器非常简单,不支持条目提升。我的解决方案是制作两个建议器字段,一个用于品牌,一个用于产品名称:

POST /product_index
{
  "mappings": {
    "products": {
      "properties": {
        "brand": {
          "type": "string",
          "analyzer": "english"
        },
        "product_name": {
          "type": "string",
          "analyzer": "english"
        },
        "id": {
          "type": "long"
        },
        "lookup_count": {
          "type": "long"
        },
        "product-suggest": {
          "type": "completion",
          "analyzer": "simple",
          "payloads": true,
          "preserve_separators": true,
          "preserve_position_increments": true,
          "max_input_length": 50
        },
        "brand-suggest": {
          "type": "completion",
          "analyzer": "simple",
          "payloads": true,
          "preserve_separators": true,
          "preserve_position_increments": true,
          "max_input_length": 50
        },
        "upc": {
          "type": "string"
        }
      }
    }
  }
}

编制索引时,请填写两个字段:

POST /product_index/products/2
{
  "id": 2,
  "brand": "Coca-Cola",
  "product_name": "Classic Coke",
  "brand-suggest": {
    "input": [
      "Coca-Cola"
    ],
    "output": "Classic Coke - Coca-Cola",
    "payload": {
      "id": 2,
      "product_name": "Classic Coke",
      "brand": "Coca-Cola",
      "popularity": 10
    }
  },
  "product-suggest": {
    "input": [
      "Classic Coke"
    ],
    "output": "Classic Coke - Coca-Cola",
    "payload": {
      "id": 2,
      "product_name": "Classic Coke",
      "brand": "Coca-Cola",
      "popularity": 10
    }
  }
}

查询时,请同时对品牌和产品建议器提出一个建议:

POST /product_index/_search
{
    "suggest": {
      "product_suggestion": {
        "text": "coca-co",
        "completion": {
          "field": "product-suggest"
        }
      },
      "brand_suggestion": {
        "text": "coca-co",
        "completion": {
          "field": "brand-suggest"
        }
      }
    }
}
删除重复项

后,您可以将品牌建议列表附加到产品建议列表中,以仅包含相关建议的建议列表,没有重复项,并首先列出产品建议。

另一种解决方案是使用查询来提升品牌和产品,而不是使用建议器。但是,此实现速度较慢,因为它不使用建议器。

完成建议器实际上在评分方面非常有限:你不能这样做。您唯一能做的就是提升某些条目,但不提升条目中的属性(请参阅weight选项 http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-suggesters-completion.html#indexing)。

这是因为完成建议器不执行"真实搜索" ->它不使用索引。这是一个简单的"字典",旨在比索引+倒排列表更快地进行"前缀"扩展。

您应该尝试一下Algolia ->该引擎旨在实时回答前缀搜索+每个属性具有不同的"权重"。这是一个针对多个字段实现自动完成菜单的教程

最新更新