2

I'm trying to apply a terms query on a nested field using mongoid-elasticsearch and ElasticSearch 2.0. This has come to be quite frustrating since the trial-error didn't pay off much and the docs on the subject are rather sparse. Here is my query:

  {
    "query": {
      "nested": {
        "path": "awards",
        "query": {
          "bool": {
            "must": [
              { "match": { "awards.year":  "2010"}}
            ]
          }
        }
      },
      "nested":{
        "path": "procuring_entity",
        "query": {
          "bool": {
            "must": [
              { "terms": { "procuring_entity.country": ["ES", "PL"]}}
            ]
          }
        }
      }
    }
  }

While "match" and "term", work just fine, when combined with the "terms" query it returns no results, even thought it should. My mappings looks like this:

  elasticsearch!({
    prefix_name: false,
    index_name: 'documents',
    index_options: {
      mappings: {
        document: {
          properties: {
            procuring_entity: {
              type: "nested"
            },
            awards: {
              type: "nested"
            }
          }
        }
      }
    },
    wrapper: :load
  })

If "nested" doesn't count as analyzer (which as far as I know doesn't), than there's no problem with that. As for the second example,I don't think it's the case since the array of values that it's matched against comes from exterior. Is terms query possible on nested fields? Am I doing something wrong? Is there any other way to match a nested field against multiple values?

Any thoughts would be much appreciated.

2
  • a shot in the dark could you use lowercase in the terms query example :` ["es", "pl"]` since terms query are not analyzed and it is probably that the data for procuring_entity is being analyzed as standard while indexing Commented Nov 23, 2015 at 18:53
  • Unfortunately it doesn't work like that either. Commented Nov 23, 2015 at 19:01

1 Answer 1

1

I think you would need to change your mappings for your nested types for this - the terms query only works on not_analyzed fields. If you update your mapping to something like:

elasticsearch!({
    prefix_name: false,
    index_name: 'documents',
    index_options: {
    mappings: {
        document: {
            properties: {
                procuring_entity: {
                    type: 'nested',
                    properties: {
                        country: {
                            'type': 'string',
                            'index': 'not_analyzed'
                        }
                    }
                },
                awards: {
                    type: 'nested'
                }
            }
        }
    }
    },
    wrapper: :load
})

I think the query should work if you do that.

Sign up to request clarification or add additional context in comments.

2 Comments

It worked. So one has to explicitly map a field as not_analyzed or it will otherwise be analyzed. Thank you!
No problem! This behavior can be surprising. Don't forget to select this as the answer, so it shows up resolved for other people.

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.