1

I have structure like this in my ElasticSearch

{
        _index: 'index',
        _type: 'product',
        _id: '896',
        _score: 0,
        _source: {
          entity_id: '896',
          category: [
            {
              category_id: 2,
              is_virtual: 'false'
            },
            {
              category_id: 82,
              is_virtual: 'false'
            }
          ]
        }
      }

I want return all "producs" that have "82" category_id.

{
  "query": {
    "bool": {
      "filter": {
        "terms": {
          "category.category_id": [
            82
          ]
        }
      }
    }
  }
}

This query gives me 0 hits.

What is right way to do this?

2 Answers 2

2

Adding working example, you need to define the category as nested field and modify your search query by including the nested path

Index Mapping

{
    "mappings": {
        "properties": {
            "entity_id": {
                "type": "text"
            },
            "category": {
                "type": "nested"
            }
        }
    }
}

Index your document

{
    "entity_id": "896",
    "category": [
        {
            "category_id": 2,
            "is_virtual": false
        },
        {
            "category_id": 82,
            "is_virtual": false
        }
    ]
}

Proper search query, note we are using nested query which doesn't support normal filter(so your query gives error)

{
    "query": {
        "nested": {
            "path": "category",
            "query": {
                "bool": {
                    "must": [
                        {
                            "match": {
                                "category.category_id": 82
                            }
                        }
                    ]
                }
            }
        }
    }
}

Search result retuns indexed doc

 "hits": [
            {
                "_index": "complexnested",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                    "entity_id": "896",
                    "category": [
                        {
                            "category_id": 2,
                            "is_virtual": false
                        },
                        {
                            "category_id": 82,
                            "is_virtual": false
                        }
                    ]
                }
            }
        ]
Sign up to request clarification or add additional context in comments.

8 Comments

Given his query doesn't return anything, it probably means that category is already nested, but he is not using a nested query. So I conclude that his mapping is right, but his query is not
ok you mean if category wasn't nested then his query would have returned the reult?
Yes, it would have returned something because it is a query on a simple object field
@Val, yeah but he has not provided his mapping so I thought of mentioning this but got your point.
Yeah, it's fine, don't worry, +1 for you ;-)
|
1

If your query gives you no results, I suspect that category is of type nested in your index mapping. If that's the case, that's good and you can modify your query like this to use the nested query:

{
  "query": {
    "bool": {
      "filter": {
        "nested": {
          "path": "category",
          "query": {
            "terms": {
              "category.category_id": [
                82
              ]
            }
          }
        }
      }
    }
  }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.