0

I come from a related database background and something like this would be so simple there, but I can't figure this out. I've been trying to learn Elasticsearch for a week or so and I'm trying to figure out what I think is a nested query. Here's some sample data:

PUT /myindex/pets/_mapping
{
   "pets": {
      "properties": {
         "name": {
            "type": "string"
         },
         "pet": {
            "type": "nested", 
            "properties": {
               "name": {"type": "string"}
            }
         }
      }
   }
}

POST /myindex/pets/
{"pet": {"name": "rosco"}, "name": "sam smith"}

POST /myindex/pets/
{"pet": {"name": "patches"}, "name": "sam smith"}

POST /myindex/pets
{"pet": {"name": "rosco"}, "name": "jack mitchell"}

What would the query look like that only returns documents matching:

  • owner name is "sam smith"
  • pet name is "rosco"

I've tried a mixmatch of bool, match, nested, filtered/filter type queries, but I just keep getting errors. Stuff like this stands out in the errors:

nested: ElasticsearchParseException[Expected field name but got START_OBJECT \"nested\"];

Here was the query:

GET /myindex/pets/_search
{
  "query": {
    "match": {
      "name": "sam smith"
    },
    "nested": {
      "path": "pet",
      "query": {
        "match": {
          "pet.name": "rosco"
        }
      }
    }
  }
}

I'm beginning to think that I just can't target something this specific due to the relevant nature of Elasticsearch.

Any ideas?

1 Answer 1

1

Man, these queries are tricky sometimes... This seems to work:

GET /myindex/pets/_search
{
  "query": {
    "filtered": {
      "query": {
        "match": {
          "name": "sam smith"
        }
      },
      "filter": {
        "nested": {
          "path": "pet",
          "query": {
            "match": {
              "pet.name": "rosco"
            }
          }
        }
      }
    }
  }
}
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.