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?

user88831
  • 177
  • 1
  • 1
  • 9

2 Answers2

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
                        }
                    ]
                }
            }
        ]
  • 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 – Val Jun 04 '20 at 06:46
  • ok you mean if category wasn't nested then his query would have returned the reult? –  Jun 04 '20 at 06:49
  • Yes, it would have returned something because it is a query on a simple `object` field – Val Jun 04 '20 at 06:51
  • @Val, yeah but he has not provided his mapping so I thought of mentioning this but got your point. –  Jun 04 '20 at 06:53
  • Yeah, it's fine, don't worry, +1 for you ;-) – Val Jun 04 '20 at 06:58
  • @Val thanks but didn't get +1 from you on my answer. –  Jun 04 '20 at 07:02
  • Phone call inbetween, there you go :-) – Val Jun 04 '20 at 07:02
  • @user88831 can you accept the answer if it was useful :) –  Jun 04 '20 at 07:33
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
              ]
            }
          }
        }
      }
    }
  }
}
Val
  • 207,596
  • 13
  • 358
  • 360