# Term query return no document

**URL:** <https://discuss.elastic.co/t/term-query-return-no-document/126170>\
**Category:** Elasticsearch\
**Created:** [March 30, 2018, 12:45am UTC](https://discuss.elastic.co/t/term-query-return-no-document/126170 "2018-03-30T00:45:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![alimardani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alimardani/32/26532_2.png) [@alimardani](https://discuss.elastic.co/u/alimardani)\
**Post date:** [March 30, 2018, 12:45am UTC](https://discuss.elastic.co/t/term-query-return-no-document/126170/1 "2018-03-30T00:45:20Z")

</div>

Hi,

I am trying to load a document by having the value of specific column. It loads the document once I use match query but doesn't load the document when I use term query.

```
GET /keydate/_search
{
  "query": {
"bool": {
  "filter": {
    "term": {
      "keyDateId": "649ad18e-aee3-492e-887f-c0d080fd5dc2"
    }
  }
}
  }  
}

GET /keydate/_search
{
  "query": {
"bool": {
  "must": [
    { "match": {
      "keyDateId": "649ad18e-aee3-492e-887f-c0d080fd5dc2"
    }}  
  ]
}
  }  
}

```

Is there any issue in my query?  
Thanks

---

<div class="post-metadata">

**Author:** ![vishnu-team8](https://avatars.discourse-cdn.com/v4/letter/v/22d042/32.png) [@vishnu-team8](https://discuss.elastic.co/u/vishnu-team8)\
**Post date:** [March 30, 2018, 5:44am UTC](https://discuss.elastic.co/t/term-query-return-no-document/126170/2 "2018-03-30T05:44:06Z")

</div>

Hi Ali,

You need to create keyDateId with both text and keyword analyzers.

```
PUT keydate/
{
     "mappings": {
    "_doc": {
      "properties": {
        "keyDateId": {
          "type": "text" 
        },
        "keyDateId": {
          "type": "keyword" 
        }
      }
    }
  }
}

```

[https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html)

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/0/5/05da70ecbb038784aafed3ffe45a28a2bf8e080c.png)

---

<div class="post-metadata">

**Author:** ![alimardani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alimardani/32/26532_2.png) [@alimardani](https://discuss.elastic.co/u/alimardani)\
**Post date:** [March 30, 2018, 5:52pm UTC](https://discuss.elastic.co/t/term-query-return-no-document/126170/3 "2018-03-30T17:52:07Z")

</div>

Hi,

based on the documentation, my query should be fine. KeyDateId is keyword and not being analayzed .

right?

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [April 2, 2018, 9:20am UTC](https://discuss.elastic.co/t/term-query-return-no-document/126170/4 "2018-04-02T09:20:53Z")

</div>

If that match query returns a result, and the term query doesn't, then `keyDateId` is most likely an analyzed field. Can you post the mapping of the index that you're querying (the output of `GET keydate`)

---

<div class="post-metadata">

**Author:** ![alimardani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alimardani/32/26532_2.png) [@alimardani](https://discuss.elastic.co/u/alimardani)\
**Post date:** [April 2, 2018, 6:18pm UTC](https://discuss.elastic.co/t/term-query-return-no-document/126170/5 "2018-04-02T18:18:25Z")

</div>

Here is my keydate index:

```
  {
  "keydate": {
    "aliases": {},
    "mappings": {
      "keydate": {
        "properties": {
          "address": {
            "properties": {
              "city": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "country": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "descriptor": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "objectState": {
                "type": "long"
              },
              "state": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "streetLine1": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "streetLine2": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              },
              "zipCode": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }
                }
              }
            }
          },
          "classId": {
            "type": "keyword"
          },
          "createdBy": {
            "type": "keyword"
          },
          "createdOn": {
            "type": "date",
            "format": "date_optional_time"
          },
          "date": {
            "type": "date",
            "format": "date_optional_time"
          },
          "description": {
            "type": "text"
          },
          "endTime": {
            "type": "keyword"
          },
          "keyDateId": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "keydateId": {
            "type": "keyword"
          },
          "locationAddress": {
            "properties": {
              "city": {
                "type": "keyword"
              },
              "country": {
                "type": "keyword"
              },
              "state": {
                "type": "keyword"
              },
              "streetLine1": {
                "type": "text"
              },
              "streetLine2": {
                "type": "text"
              },
              "zipCode": {
                "type": "keyword"
              }
            }
          },
          "message": {
            "type": "keyword"
          },
          "modifiedBy": {
            "type": "keyword"
          },
          "modifiedOn": {
            "type": "date",
            "format": "date_optional_time"
          },
          "objectState": {
            "type": "long"
          },
          "photoes": {
            "properties": {
              "url": {
                "type": "keyword"
              }
            }
          },
          "pinToTop": {
            "type": "boolean"
          },
          "reminder": {
            "properties": {
              "value": {
                "type": "integer"
              }
            }
          },
          "scheduleDate": {
            "type": "date",
            "format": "date_optional_time"
          },
          "schoolId": {
            "type": "keyword"
          },
          "sendNow": {
            "type": "boolean"
          },
          "sendText": {
            "type": "boolean"
          },
          "startTime": {
            "type": "keyword"
          },
          "title": {
            "type": "text"
          }
        }
      }
    },
    "settings": {
      "index": {
        "creation_date": "1522271916303",
        "number_of_shards": "5",
        "number_of_replicas": "1",
        "uuid": "5cophHReQIapRS0_H87tYg",
        "version": {
          "created": "6000199"
        },
        "provided_name": "keydate"
      }
    }
  }
}
```

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [April 3, 2018, 8:16am UTC](https://discuss.elastic.co/t/term-query-return-no-document/126170/6 "2018-04-03T08:16:19Z")

</div>

Alright, the `keyDateId` field has been mapped as a text field, which is analyzed:

```auto
          "keyDateId": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }

```

That's why your term query does not return any hits: the term query does not analyze the search terms, so there's a mismatch between your query and the way the field in the document has been indexed.

What you want to do is query the `.keyword` multi-field instead. This is a second way the `keyDateId` field has been indexed: as an non-analyzed field of type keyword. Instead of `keyDateId` you'd provide `keyDateId.keyword`:

```auto
GET /keydate/_search
{
  "query": {
    "bool": {
      "filter": {
        "term": {
          "keyDateId.keyword": "649ad18e-aee3-492e-887f-c0d080fd5dc2"
        }
      }
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![alimardani](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alimardani/32/26532_2.png) [@alimardani](https://discuss.elastic.co/u/alimardani)\
**Post date:** [April 3, 2018, 5:44pm UTC](https://discuss.elastic.co/t/term-query-return-no-document/126170/7 "2018-04-03T17:44:34Z")

</div>

Thank you so much.

---

<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:** [May 1, 2018, 5:44pm UTC](https://discuss.elastic.co/t/term-query-return-no-document/126170/8 "2018-05-01T17:44:36Z")

</div>

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