2

I'm trying to perform a search with ElasticSearch, that must match some search terms in the sections index, but only for sections that belong to certain projects, namely, the projects to which the current user has access.

So I'm trying to apply a filter to the query, so that it will filter sections that have the project_id included in a small set of 2 or 3 different values. If a section's project_id is among the valid project_id's, it should be included. Otherwise it won't.

This is all in the context of a rails api using the elasticsearch-model gem.

This is the query I'm passing in. It pretends to filter sections, so that only sections in projects 2 and 5 are included in the results.

{
  "query": {
    "filtered": {
      "query": {
        "multi_match": {
          "query": "search terms here",
          "type": "cross_fields",
          "fields": ["title^3", "contents"],
          "operator": "and"
        }
      },
      "filter": {
        "or": {
          "filters": [
            { "exists": { "project_id": 2 } },
            { "exists": { "project_id": 5 } }
          ]
        }
      }
    }
  }
}

And I get this error:

QueryParsingException[[sections] [exists] filter does not support [project_id]]

Any ideas?

1 Answer 1

8

The exist filter means checking the fields is exist or not.refer.

To get what you trying to achieve.

 {
 "query": {
  "filtered": {
     "query": {
        "multi_match": {
           "query": "search terms here",
           "type": "cross_fields",
           "fields": [
              "title^3",
              "contents"
           ],
           "operator": "and"
        }
     },
     "filter": {
        "terms": {
           "project_id": ["1","2"]
        }
      }
    }
  }
}

HOpe it helps..!

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

1 Comment

Thanks! Even simpler than what I was doing with that weird "or" filter. This was more alike to how I thought initially it would be, but the documentation I found on the "terms" filter didn't have any examples using an array for possible values. Thanks a lot!

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.