0

我有一个产品-商家映射,如下所示

  catalog_map = {

        "catalog": {
            "properties": {
                "merchant_id": {
                    "type": "string",
                },
                "products": {
                    "type": "object",
                },
                "merchant_name" :{
                    "type" : "string"
                }
            }
        }
    }

“产品”具有对象,例如 product_id 、 product_name 、 product_price 。产品和商家被映射,这样:

    for merchant in Merchant.objects.all() :

        products = [{"product_name" : x.product.name, "product_price" : x.price, "product_id" : x.product.id , "product_category" : x.product.category.name} for x in MerchantProductMapping.objects.filter(merchant=merchant)]

        tab = {
            'merchant_id': merchant.id,
            'merchant_name': merchant.name,
            'product': products
            }

       res = es.index(index="my-index", doc_type='catalog', body=tab)

数据以所需的形式顺利索引。现在,当我从给定索引查询数据时,我按以下方式进行:

GET /esearch-index/catalog/_search
{ 
"query": {
"bool" :{
    "must": [
       {"match": {
          "merchant_name": {
              "query": "Sir John"
          }
       }}], 
       "should": [
          {"match": {
             "product_name": {
                 "query": "Vanilla"
             }
          }}
       ]
    }}

此查询为我提供了索引中商家名称为 "Sir John" 的所有产品的结果。但是,我希望它返回“约翰爵士”出售的产品“香草”的详细信息。

根据某人的建议,我在查询时使用了“_source”,但这并没有帮助。

如何从商家的整个“目录”索引中挑出单个对象的信息?

4

1 回答 1

2

一旦你的bool 查询有一个must子句,它里面的所有条件都是必需的。should子句中的条件不是必需的。他们只会提高结果。(http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html#query-dsl-bool-query

因此,返回到您的查询,它将检索与商家名称“Sir John”匹配的所有目录。这是唯一需要(必须)的条件。名称“Vanilla”只会将名称为“Vanilla”的结果提升到顶部,因为它不是必需的。

如果要检索“Sir John”出售的“Vanilla”,请将两个条件都放在 must 子句中,并将查询更改为:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "merchant_name": {
              "query": "Sir John"
            }
          }
        },
        {
          "match": {
            "product_name": {
              "query": "Vanilla"
            }
          }
        }
      ]
    }
  }
}
于 2014-11-17T11:18:44.847 回答