0

I'd like to build a query based on two script filters, but I can't seem to get it to work. I've tried using nesting (following the example in the doc), but keep getting a syntax error:

QueryParsingException[[my_index] [_na] filter malformed, no field after start_object

The query is:

{
    "query": {
        "filtered": {
            "query":{
                "query_string": {
                    "query": "things.type:1 AND things.status:1"
                }
            }, 
            "filter": {
                "nested": {
                   "path": "obj",
                   "_cache": true, 
                   "filter": {
                        "bool": {
                            "must": [
                                {
                                    "script": "doc['things.type'].values.size() == 1"
                                },
                                {
                                    "script": "obj['other_things.key'].values.size() >= 1"
                                }
                            ]
                       }
                   }
                }
            }
        }
    }
}

I could just pull the records from the first script ("script": "doc['things.type'].values.size() == 1") and iterate over the list in Python (sing pyelasticsearch to execute these queries), but it seems that elastic search should be able to do this.

1
  • Note that by doing this, you are loading all the values of the fields into memory, just to get the count. You should consider adding the size of the lists at index-time, so you can simply filter on its size. That can save you a lot of memory. elasticsearch.org/guide/en/elasticsearch/reference/current/… Commented Nov 18, 2013 at 19:38

1 Answer 1

2

You have two nested objects, so you need two separated nested filters. Each nested filter applies to a single document, so you can't access different documents in one nested clause. You can have as many filters as you need, connected with and:

{
  "query": {
    "filtered": {
      "query": {
        "query_string": {
          "query": "things.type:1 AND things.status:1"
        }
      },
      "filter": {
        "and": {
          "filters": [
            {
              "nested": {
                "path": "other_things",
                "filter": {
                  "script": {
                    "script": "doc['other_things.key'].values.size() >= 1"
                  }
                }
              }
            },
            {
              "nested": {
                "path": "things",
                "filter": {
                  "script": {
                    "script": "doc['things.type'].values.size() == 1"
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Not quite, I should have been more specific. things and other_things are both attributes of master_thing.
I edited to use two separate nested filters, one for each nested type.

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.