我在Elasticsearch中使用“完成建议程序”来允许部分单词匹配查询。在我的索引(products_index)中,我希望能够查询产品名称字段和品牌字段。这是我的映射:

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 字段赋予比品牌字段更高的权重之外,这非常有用。有什么办法可以做到这一点?我已经在使用 bool 查询中研究了这个article,但是我对Elasticsearch还是很陌生,并且不确定如何在完成建议者的情况下应用它。

非常感谢!

最佳答案

就像氧化还原所说的那样,完成建议器确实很简单,并且不支持条目提升。
我的解决方案是创建两个建议字段,一个用于品牌,一个用于产品名称:

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

您可以在删除重复项之后将品牌建议建议列表附加到产品建议之一中,以仅具有相关建议,无重复项和产品建议的形式拥有建议列表。

另一个解决方案是使用查询来提升品牌和产品,而不是使用建议者。但是,此实现比较慢,因为它不使用建议程序。

关于在具有不同权重的多字段上的Elasticsearch完成建议,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28268418/

10-17 03:03