2

I want to filter the document by nested field value. My document is like this and I want to filter it by Color parameter:

{
  "_index": "myindex",
  "_type": "product",
  "_id": "984984",
  "_source": {
    "id": "98418",
    "name": "Product1",
    ..
    "parameters": {
      "Color": [
        "Black",
        "Gold"
      ]
    }
  }
}

My mapping is:

{
  "myindex": {
    "mappings": {
      "product": {
        "properties": {
          ..
          "parameters": {
            "type": "nested",
            "properties": {
            "Color": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            },
            ..
          }
        }
      }
    }
  }
}

And my filter query is following:

{
  "query": {
    "bool": {
      "filter": {
        "nested": {
          "path": "parameters",
          "query": {
            "term": {    
              "parameters.Color":"Gold"
            }
          }  
        }
      }
    }
  }
}

But unfortunatelly I'm getting zero documents and I do not understand why?

Thank you

EDIT

Event this is working:

{
  "query": {
    "nested": {
      "path": "parameters",
      "query": {
        "bool": {
          "must": [
            { "match": { "parameters.Color": "Gold" }}
          ]
        }
      }
    }
  }
}

..but this is not:

{
  "query": {
    "nested": {
      "path": "parameters",
      "query": {
        "bool": {
          "filter": [
            { "term": { "parameters.Color": "Gold" }}
          ]
        }
      }
    }
  }
}

Why??

1 Answer 1

3

Your term query is looking for an exact match and the match query is analyzed before searching. If you are using the standard analyzer it will lowercase your terms when it analyzes them.

You could use the keyword field if you need to do an exact match.

{
  "query": {
    "bool": {
      "filter": {
        "nested": {
          "path": "parameters",
          "query": {
            "term": {    
              "parameters.Color.keyword":"Gold"
            }
          }  
        }
      }
    }
  }
}
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.