0

My ElasticSearch documents contain a nested collection of form fields. Each field has a name and a value and the mapping is as follows:

form: {
  properties: {
    id:         { type: 'integer' },
    name:       { type: 'text' },
    form_data:  {
      type: 'nested',
      properties: {
        'name':  { type: 'keyword' },
        'value': { type: 'text', analyzer: 'full_text_analyzer' }
      }
    }
  }
}

I need to allow the user to search for multiple form fields to refine their search. They can choose which fields to search by and assign a value to each. For example

applicant_name = 'Joe'
pet_type = 'dog'

This would find all documents that contained a field named applicant_name which had a value fuzzy matching Joe as well as a field named pet_type and a value fuzzy matching dog.

The query I'm trying to do this with is as follows.:

{
  "query": {
    "bool": {
      "must": [{
        "nested": {
          "path": "form_data",
          "query": {
            "filter": {
              "bool": {
                "must": [
                  {
                    "bool": {
                      "must": [
                        { "term": { "form_data.name": "applicant_name" } },
                        { "match": { "form_data.value": "Joe" } }
                      ]
                    }
                  },
                  {
                    "bool": {
                      "must": [
                        { "term": { "form_data.name": "pet_type" } },
                        { "match": { "form_data.value": "dog" } }
                      ]
                    }
                  }
                ]
              }
            }
          }
        }
      }]
    }
  }
}

However, I get 0 results.

1 Answer 1

1

Try using a nested query per condition in your initial "must" clause:

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "form_data",
            "query": {
              "bool": {
                "must": [
                  { "term": { "form_data.name": "applicant_name" } },
                  { "match": { "form_data.value": "Joe" } }
                ]
              }
            }
          }
        },
        {
          "nested": {
            "path": "form_data",
            "query": {
              "bool": {
                "must": [
                  { "term": { "form_data.name": "pet_type" } },
                  { "match": { "form_data.value": "dog" } }
                ]
              }
            }
          }
        }
      ]
    }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

That did it. It would be great if you could include an explanation of why this approach worked while the original one did not.
Glad it worked! Unfortunately my understanding of ES is still pretty shallow so i'm not sure why the original query did not work.

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.