# Term Query for short keywords that include spaces

**URL:** <https://discuss.elastic.co/t/term-query-for-short-keywords-that-include-spaces/190351>\
**Category:** Elasticsearch\
**Created:** [July 13, 2019, 2:50pm UTC](https://discuss.elastic.co/t/term-query-for-short-keywords-that-include-spaces/190351 "2019-07-13T14:50:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![droplet](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@droplet](https://discuss.elastic.co/u/droplet)\
**Post date:** [July 13, 2019, 2:50pm UTC](https://discuss.elastic.co/t/term-query-for-short-keywords-that-include-spaces/190351/1 "2019-07-13T14:50:40Z")

</div>

Hi there,  
stuck on a simple problem.

I have an index containing various RAM sizes (1GB, 2GB, 3GB, 4GB...24GB)

Is there a way to use Term query to check if RAM size exists in the index?

Search for "4 GB" (notice the space), should return exactly the document that contains 4GB

Here a simple index with some test documents:

```auto
PUT ram

POST _bulk
{ "index": { "_index": "ram"} }
{ "thefield": "1GB"}
{ "index": { "_index": "ram"} }
{ "thefield": "2GB"}
{ "index": { "_index": "ram"} }
{ "thefield": "3GB"}
{ "index": { "_index": "ram"} }
{ "thefield": "4GB"}
{ "index": { "_index": "ram"} }
{ "thefield": "5GB"}
{ "index": { "_index": "ram"} }
{ "thefield": "6GB"}
{ "index": { "_index": "ram"} }
{ "thefield": "7GB"}
{ "index": { "_index": "ram"} }
{ "thefield": "8GB"}
{ "index": { "_index": "ram"} }
{ "thefield": "24GB"}

```

```auto
this returns no results
GET ram/_search
{
  "query": {
    "term": {
      "thefield": {
        "value": "4 GB"
      }
    }
  }
}

```

```auto
this returns 1 match, exactly what I need
GET ram/_search
{
  "query": {
    "term": {
      "thefield.keyword": {
        "value": "4GB"
      }
    }
  }
}

```

```auto
but this does not...
GET ram/_search
{
  "query": {
    "term": {
      "thefield.keyword": {
        "value": "4gb"
      }
    }
  }
}

```

```auto
this returns no results also
GET ram/_search
{
  "query": {
    "term": {
      "thefield.keyword": {
        "value": "4 GB"
      }
    }
  }
}

```

Tried my best with Analyzers, Tokenizer, Filters, etc... from the documentation and some online courses. Couldn't get it to work. Before you slam me with "did you google for it?", I did and results are related to performance issues or hardware requirements for elastic. Probably because of my specific need to match RAM size with space and without.

---

<div class="post-metadata">

**Author:** ![Murphy](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@Murphy](https://discuss.elastic.co/u/Murphy)\
**Post date:** [July 15, 2019, 6:14am UTC](https://discuss.elastic.co/t/term-query-for-short-keywords-that-include-spaces/190351/2 "2019-07-15T06:14:18Z")

</div>

Term Query, returns documents that contain an **exact** term in a provided field.  
So, for term query, search 4gb and 4 gb will not hit 4GB.

The simplest way is convert the 4gb and 4 gb to 4GB before send the query to ES.

---

<div class="post-metadata">

**Author:** ![droplet](https://avatars.discourse-cdn.com/v4/letter/d/bcef8e/32.png) [@droplet](https://discuss.elastic.co/u/droplet)\
**Post date:** [July 15, 2019, 11:44am UTC](https://discuss.elastic.co/t/term-query-for-short-keywords-that-include-spaces/190351/3 "2019-07-15T11:44:02Z")

</div>

Thanks for replying.  
The thing is, it will eventually get more "attributes" so it will end up as a huge problem. I will have to hire an ML expert to get working on feature extraction out of 10m html documents.

---

<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:** [August 12, 2019, 11:44am UTC](https://discuss.elastic.co/t/term-query-for-short-keywords-that-include-spaces/190351/4 "2019-08-12T11:44:10Z")

</div>

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