Have certain ids ignore parts of query

Hi there. I'm having a bit of an issue figuring out how to write a query that my project needs need. We have an endpoint where we can pass in a keyword and find users based on this.

In our docs, we have an is_private field. Usually we exclude results that have this field set to true, but now we are introducing a new flag to the endpoint: allowed_ids. With this flag, we could have a query with data like:

"username": "userna",
"id": [1, 2, 3],
"is_private": false

With this we would get users that have a username like userna... whom are not private. What I want to achieve with the allowed_ids is to still get results for these ids if they match the keyword, even though they are also private. Having those specific documents ignoring the is_private field basically.

I am really not sure how to go about this. I don't really have a lot of examples to show, of what I've already tried, since I've only ended up writing a basic query and then gotten stuck straight away. Any push in the right direction would be greatly appreciated!

Managed to figure it out myself. This does the trick for me:

GET index/users/_search
{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": [
            {
              "multi_match": {
                "query": "userna",
                "type": "phrase_prefix",
                "fields": [
                  "title",
                  "username^3",
                  "email"
                ]
              }
            },
            {
              "bool": {
                "should": [
                  {
                    "bool": {
                      "must": [
                        {
                          "terms": {
                            "id": [
                              1
                            ]
                          }
                        }
                      ]
                    }
                  },
                  {
                    "term": {
                      "is_private": false
                    }
                  }
                ]
              }
            }
          ]
        }
      }
    }
  }
}

With a little help from https://stackoverflow.com/a/40755927/2984408

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.