# Have certain ids ignore parts of query

**URL:** <https://discuss.elastic.co/t/have-certain-ids-ignore-parts-of-query/224113>\
**Category:** Elasticsearch\
**Created:** [March 18, 2020, 1:13pm UTC](https://discuss.elastic.co/t/have-certain-ids-ignore-parts-of-query/224113 "2020-03-18T13:13:58Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SeverinDK](https://avatars.discourse-cdn.com/v4/letter/s/ac8455/32.png) [@SeverinDK](https://discuss.elastic.co/u/SeverinDK)\
**Post date:** [March 18, 2020, 1:13pm UTC](https://discuss.elastic.co/t/have-certain-ids-ignore-parts-of-query/224113/1 "2020-03-18T13:13:58Z")

</div>

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:

```auto
"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!

---

<div class="post-metadata">

**Author:** ![SeverinDK](https://avatars.discourse-cdn.com/v4/letter/s/ac8455/32.png) [@SeverinDK](https://discuss.elastic.co/u/SeverinDK)\
**Post date:** [March 18, 2020, 2:13pm UTC](https://discuss.elastic.co/t/have-certain-ids-ignore-parts-of-query/224113/2 "2020-03-18T14:13:20Z")

</div>

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

```auto
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](https://stackoverflow.com/a/40755927/2984408)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 15, 2020, 2:14pm UTC](https://discuss.elastic.co/t/have-certain-ids-ignore-parts-of-query/224113/3 "2020-04-15T14:14:52Z")

</div>

Closed on April 15, 2020, 2:14pm UTC

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