# Query with terms does not work properly

**URL:** https://discuss.elastic.co/t/query-with-terms-does-not-work-properly/24717
**Category:** Elasticsearch
**Created:** [July 1, 2015, 11:33am UTC](https://discuss.elastic.co/t/query-with-terms-does-not-work-properly/24717 "2015-07-01T11:33:34Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Andreas\_Jung](https://avatars.discourse-cdn.com/v4/letter/a/c67d28/32.png) [@Andreas\_Jung](https://discuss.elastic.co/u/Andreas_Jung)
#### Post date: [July 1, 2015, 11:33am UTC](https://discuss.elastic.co/t/query-with-terms-does-not-work-properly/24717/1 "2015-07-01T11:33:34Z")

</div>

I have this document in Elasticsearch (1.6)

```
{
"_index": "onkopedia",
"_type": "document_",
"_id": "0afa26afc2d1440a8ed03dac0e8511fc",
"_version": 1,
"_score": null,
"_source": {
"description": "",
"contributors": [],
"metaTypeName": "Connector",
"sortableTitle": "mammakarzinom der frau",
"subject": [],
"authorizedUsers": [
"Anonymous"
],
"language": "",
"title": "Mammakarzinom der Frau",
"url": "http://dev1.veit-schiele.de:9080/onkopedia/de/onkopedia/guidelines/mammakarzinom-der-frau",
"author": "ajung",
"modified": "2015-05-11T05:21:14",
"metaType": "xmldirector.plonecore.connector",
"content": " Mammakarzinom der Frau Stand: Januar 2013 Autoren der aktuellen .....",
"authorName": "ajung",
"created": "2015-05-11T05:21:14",
"review_state": "published"
},
"sort": [
null
]
}

```

containing a key

```
'authorizedUsers': ['Anonymous']

```

The following query is supposed to return the document above however it does not:

```
 {
  "sort": [
    "_score"
  ], 
  "from": 0, 
  "fields": [
    "url", 
    "title", 
    "description", 
    "metaType", 
    "metaTypeName", 
    "author", 
    "authorName", 
    "contributors", 
    "modified", 
    "subject", 
    "review_state", 
    "language", 
    "content"
  ], 
  "query": {
    "filtered": {
      "filter": {
        "and": [
          {
            "terms": {
              "execution": "or", 
              "metaType": [
                "Document", 
                "FormFolder", 
                "Collection", 
                "Discussion Item", 
                "News Item", 
                "xmldirector.plonecore.connector", 
                "CaptchaField"
              ]
            }
          }, 
          {
            "terms": {
              "execution": "or", 
              "authorizedUsers": [
                "Manager", 
                "Authenticated", 
                "Anonymous", 
                "user:ajung"
              ]
            }
          }
        ]
      }, 
      "query": {
        "query_string": {
          "query": "mammakarzinom", 
          "default_operator": "AND", 
          "fields": [
            "title^3", 
            "contributors^2", 
            "subject^2", 
            "description", 
            "content"
          ]
        }
      }
    }
  }, 
  "highlight": {
    "fields": {
      "content": {
        "fragment_size": 250, 
        "number_of_fragments": 3
      }, 
      "description": {
        "fragment_size": 250, 
        "number_of_fragments": 2
      }, 
      "title": {
        "number_of_fragments": 0
      }
    }
  }, 
  "size": 15
}

```

The query without the filter for 'authorizedUsers' does return the document. Why? 'Anonymous' as value for 'authorizedUsers' is available within the query, so I would expect that the document would be found by the first query, or?

```
{
  "sort": [
    "_score"
  ], 
  "from": 0, 
  "fields": [
    "url", 
    "title", 
    "description", 
    "metaType", 
    "metaTypeName", 
    "author", 
    "authorName", 
    "contributors", 
    "modified", 
    "subject", 
    "review_state", 
    "language", 
    "content"
  ], 
  "query": {
    "filtered": {
      "filter": {
        "and": [
          {
            "terms": {
              "execution": "or", 
              "metaType": [
                "Document", 
                "FormFolder", 
                "Collection", 
                "Discussion Item", 
                "News Item", 
                "xmldirector.plonecore.connector", 
                "CaptchaField"
              ]
            }
          }

```

Both subqueries for 'authorizedUsers' and 'metaType' must be ANDed. 'authorizedUsers' itself is a multi-value field.

---

<div class="post-metadata">

### Author: ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)
#### Post date: [July 1, 2015, 12:46pm UTC](https://discuss.elastic.co/t/query-with-terms-does-not-work-properly/24717/2 "2015-07-01T12:46:33Z")

</div>

The most likely reason for this issue is that the field `authorizedUsers` is mapped as analyzed with an analyzer that lowercases the value of this field. So, during indexing the value `Anonymous` becomes `anonymous`. The `terms` filter doesn't analyze the terms, so it is searching for the term `Anonymous` (with upper case `A`) which doesn't exist in the index. Judging from the usage the fields `authorizedUsers` and `metaType` should be reindexed as `not_analyzed` fields. I would recommend checking the [Mapping and Analysis section](https://www.elastic.co/guide/en/elasticsearch/guide/current/mapping-analysis.html) of The Definitive Guide for more details.

---

<div class="post-metadata">

### Author: ![Andreas\_Jung](https://avatars.discourse-cdn.com/v4/letter/a/c67d28/32.png) [@Andreas\_Jung](https://discuss.elastic.co/u/Andreas_Jung)
#### Post date: [July 1, 2015, 1:12pm UTC](https://discuss.elastic.co/t/query-with-terms-does-not-work-properly/24717/3 "2015-07-01T13:12:07Z")

</div>

Not exactly right but your thoughts brought me on the right track.

What was happening here:

- I have some document\_ mappings for holding documents by language
- some documents did not have a language property and got indexed into 'document\_' which  
had no predefinied mapping definition...so ES defaults applied
- all documents indexed under document\_ could not be found properly

---

<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: [July 6, 2017, 12:04am UTC](https://discuss.elastic.co/t/query-with-terms-does-not-work-properly/24717/4 "2017-07-06T00:04:14Z")

</div>


