# Elasticsearch query text field by term

**URL:** https://discuss.elastic.co/t/elasticsearch-query-text-field-by-term/298693
**Category:** Elasticsearch
**Created:** [March 3, 2022, 4:29am UTC](https://discuss.elastic.co/t/elasticsearch-query-text-field-by-term/298693 "2022-03-03T04:29:02Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![jiankunking](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jiankunking/32/39320_2.png) [@jiankunking](https://discuss.elastic.co/u/jiankunking)
#### Post date: [March 3, 2022, 4:29am UTC](https://discuss.elastic.co/t/elasticsearch-query-text-field-by-term/298693/1 "2022-03-03T04:29:02Z")

</div>

I have read the docs，[Term query | Elasticsearch Guide [8.0] | Elastic](https://www.elastic.co/guide/en/elasticsearch/reference/8.0/query-dsl-term-query.html)

I know that the text field should not be queried with term.

But there's another sentence in the document

> The `term` query does **not** analyze the search term. The `term` query only searches for the **exact** term you provide. This means the `term` query may return poor or no results when searching `text` fields.

**I wonder why I can't query text by term, because in my example below, I've already segmented text**

My index template is as follows

```auto
{
  "order": 0,
  "index_patterns": [
    "resource*",
    "resource-dev*"
  ],
  "settings": {
    "index": {
      "number_of_shards": "6",
      "number_of_replicas": "1",
      "refresh_interval": "200ms"
    }
  },
  "mappings": {
    "dynamic_templates": [
      {
        "strings": {
          "mapping": {
            "type": "keyword"
          },
          "match_mapping_type": "string"
        }
      }
    ],
    "properties": {
      "id": {
        "type": "keyword"
      },
      "almScode": {
        "type": "keyword"
      },
      "name": {
        "type": "keyword"
      },
      "desc": {
        "type": "text"
      },
      "parentId": {
        "type": "keyword"
      },
      "fullId": {
        "type": "text",
        "analyzer": "pattern",
        "fields": {
          "raw": {
            "type": "keyword"
          }
        }
      },
      "type": {
        "type": "keyword"
      },
      "attrs": {
        "type": "object"
      },
      "creator": {
        "type": "keyword"
      },
      "updater": {
        "type": "keyword"
      },
      "createdAt": {
        "format": "epoch_second",
        "type": "date"
      },
      "updatedAt": {
        "format": "epoch_second",
        "type": "date"
      }
    }
  }
}

```

The document format is as follows

```auto
{
          "id" : "jiankunking_hdyst_modeProgress",
          "almScode" : "jiankunking",
          "name" : "hdyst_modeProgress",
          "desc" : "新品型号进度界面",
          "parentId" : "jiankunking_hdyst_root",
          "fullId" : "hdyst_root.hdyst_modeProgress",
          "type" : "menu",
          "attrs" : {
            "path" : {
              "attrValue" : "/hdy/modeProgress",
              "attrType" : "String",
              "attrDesc" : "菜单路径"
            },
            "sort" : {
              "attrValue" : "5",
              "attrType" : "String",
              "attrDesc" : "排序"
            }
          },
          "creator" : "jiankunking",
          "updater" : "jiankunking",
          "createdAt" : 1646130169,
          "updatedAt" : 0,
          "updateAt" : 1646130169
      }

```

We need to pay attention to the fullId field。

I looked at the segmentation results by vectors \_termvectors：

```auto
#! [types removal] Specifying types in term vector requests is deprecated.
{
  "_index" : "resource-dev-new2-jiankunking",
  "_type" : "_doc",
  "_id" : "jiankunking_hdyst_modeProgress",
  "_version" : 1,
  "found" : true,
  "took" : 7,
  "term_vectors" : {
    "fullId" : {
      "field_statistics" : {
        "sum_doc_freq" : 932,
        "doc_count" : 244,
        "sum_ttf" : 932
      },
      "terms" : {
        "hdyst_modeprogress" : {
          "term_freq" : 1,
          "tokens" : [
            {
              "position" : 1,
              "start_offset" : 11,
              "end_offset" : 29
            }
          ]
        },
        "hdyst_root" : {
          "term_freq" : 1,
          "tokens" : [
            {
              "position" : 0,
              "start_offset" : 0,
              "end_offset" : 10
            }
          ]
        }
      }
    }
  }
}

```

My question is why doc cannot be queried through the following query statement

```auto
GET /resource-dev-jiankunking/_search
{
  "size": 2000, 
	"query": {
		"bool": {
			"filter": [
				{
					"terms": {
						"fullId": [
						  	"hdyst_modeProgress"
								]
						}
				}	
			]
		}
	}
}

```

---

<div class="post-metadata">

### Author: ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)
#### Post date: [March 3, 2022, 8:04am UTC](https://discuss.elastic.co/t/elasticsearch-query-text-field-by-term/298693/2 "2022-03-03T08:04:25Z")

</div>

The short answer is you can query text fields with a term query.  
The caveat should ideally be _…if you really know what you’re doing_  
Most users are best advised to take what the user types into the search box and run it through something like the ‘match’ query because that will treat the string with the same analysis pipeline applied to the documents you are trying to match.  
However, there are cases where search criteria is not typed in by users and does not need stemming/lower casing etc. For example, the ‘significant\_text’ aggregation is used to suggest interesting terms directly from the search index and if a user chooses to click to drill down on one of these raw terms they would do so using a ‘term’ query.

---

<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: [March 31, 2022, 8:04am UTC](https://discuss.elastic.co/t/elasticsearch-query-text-field-by-term/298693/3 "2022-03-31T08:04:43Z")

</div>

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