3

Is it possible to have a Nested Type with List inside of it in ElasticSearch? An Example would be something of I was thinking is like this

{
  "name" : "Zach",
  "car" : [
    {
      "make" : "Saturn",
      "model" : "SL",
      "colors": ["Red","Blue","Green"]
     },
    {
      "make" : "Saturn",
      "model" : "Imprezza",
      "colors": ["Pink","Green"]
    }
  ]
}

How would I query such the FF?

1) I want to query all persons that their cars are "make" with "Saturn" and has colors of "Green"

2) I want to query all persons that their cars has colors of "Green" or "PINK"

1
  • So, did it solved your problem or not? Commented Aug 7, 2014 at 15:28

2 Answers 2

1

Yes. Here's how I would do it:

Step 1. Set up mapping:

PUT /index_name
{
  "mappings": {
    "type_name": {
      "properties": {
        "name": {
          "type": "string"
        },
        "car": {
          "type": "nested",
          "properties": {
            "make": {
              "type": "string"
            },
            "model": {
              "type": "string"
            },
            "colors": {
              "type": "string"
            }
          }
        }
      }
    }
  }
}

Step 2. Populate the index:

PUT /index_name/type_name/1
{
  "name": "Zach",
  "car": [
    {
      "make": "Saturn",
      "model": "SL",
      "colors": [
        "Red",
        "Blue",
        "Green"
      ]
    },
    {
      "make": "Saturn",
      "model": "Imprezza",
      "colors": [
        "Pink",
        "Green"
      ]
    }
  ]
}

Step 3. Query the index. Note: You'll need to use the bool query in order to retrieve documents with multiple query parameters.

GET /index_name/type_name/_search
{
  "query": {
    "nested": {
      "path": "car",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "make": "Saturn"
              }
            },
            {
              "match": {
                "colors": "Green"
              }
            }
          ]
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

10 Comments

Question, why "car.colors.color": "Green" ? why not "car.colors" only?
Good question. I changed the colors property from an object to an array as described here. That should simplify things.
Another question, why did you added an index on colors? "index_name": "tag" ?
You're right, it's not necessary. I removed it from the answer.
why didn't also use a nested query when querying nested objects?
|
0

Yes, You can have array of nested documents.

Here is your solution.

Create index, put mapping and data.

PUT x3
PUT x3/x4/_mapping
{
   "x4": {
      "properties": {
         "name": {
            "type": "string"
         },
         "car": {
            "type": "nested",
            "properties": {
               "make": {
                  "type": "string",
                  "index": "not_analyzed"
               },
               "model": {
                  "type": "string",
                  "index": "not_analyzed"
               },
               "colors": {
                  "type": "string",
                  "index": "not_analyzed"
               }
            }
         }
      }
   }
}

PUT /x3/x4/1
{
  "name": "Zach",
  "car": [
    {
      "make": "Saturn",
      "model": "SL",
      "colors": [
        "Red",
        "Blue",
        "Green"
      ]
    },
    {
      "make": "Saturn",
      "model": "Imprezza",
      "colors": [
        "Pink",
        "Green"
      ]
    }
  ]
}

1) I want to query all persons that their cars are "make" with "Saturn" and has colors of "Green"

POST x3/x4/_search
{
    "filter": {
        "nested": {
           "path": "car",
           "filter": {
               "and": {
                  "filters": [
                     {
                         "term": {
                            "make":"Saturn"
                         }
                     },{
                         "term": {
                            "colors":"Green"
                         }
                     }
                  ]
               }
           }
        }
    }
}

2) I want to query all persons that their cars has colors of "Green" or "PINK"

POST x3/x4/_search
{
   "filter": {
      "nested": {
         "path": "car",
         "filter": {
            "terms": {
               "car.colors": [
                  "Green" , "Pink"
               ]
            }
         }
      }
   }
}

References:: nested filter, mapping , term filter

Hope this helps!!

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.