Elasticsearch query text field by term

I have read the docs,Term query | Elasticsearch Guide [8.0] | Elastic

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

{
  "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

{
          "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:

#! [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

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

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.

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