0

I have a Product-Merchant mapping which looks like the following

  catalog_map = {

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

"product" has objects, say , product_id , product_name , product_price. Products and merchants are mapped, such that :

    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)

The data gets indexed smoothly, in the desired form. Now, when I query the data from given index, I do it in the following way :

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

This query gives me the result of all the products in the index with merchant name "Sir John" . However, I want it to return the details of the product "Vanilla" sold by "Sir John" instead.

On someone's recommendation, I used "_source" while querying, but that doesn't help.

How can I single out the information of one single object from the entire "catalog" index of the merchant?

1
  • it's not clear from your question what the structure of your data is. The documentation on nesting has a good explanation of the flattening that happens in Elasticsearch - this might be the cause of the query issue. Ways round it include using explicit nesting or a child document. Commented Nov 16, 2014 at 16:34

1 Answer 1

2

Once your bool query has a must clause, all the conditions inside of it are required. The conditions inside of the should clause are not required. They will only boost the results. (http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html#query-dsl-bool-query)

So, going back to your query, it will retrieve all catalogs matching merchant_name "Sir John". This is the only required (must) condition. The name "Vanilla" will only boost results with the name "Vanilla" to the top, because it is not required.

If you want to retrieve "Vanilla" sold by "Sir John", put both conditions inside of the must clause and change your query to this:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "merchant_name": {
              "query": "Sir John"
            }
          }
        },
        {
          "match": {
            "product_name": {
              "query": "Vanilla"
            }
          }
        }
      ]
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.