elasticsearch 我们如何使用和查询关键字字段?

mhd8tkvw  于 2022-12-17  发布在  ElasticSearch
关注(0)|答案(1)|浏览(179)

当我做了

PUT /vehicles/_doc/123
{
  "make" : "Honda Civic", 
  "color" : "Blue", 
  "from": "Japan",
  "size": "Big",
  "comment": "deja vu",
  "HP" : 250, 
  "milage" : 24000, 
  "price": 19300.97
}

自动生成如下指标定义:

{
  "vehicles": {
    "aliases": {},
    "mappings": {
      "properties": {
        "HP": {
          "type": "long"
        },
        "color": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "comment": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "from": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "make": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "milage": {
          "type": "long"
        },
        "price": {
          "type": "float"
        },
        "size": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
      }
    },
    "settings": {
      "index": {
        "routing": {
          "allocation": {
            "include": {
              "_tier_preference": "data_content"
            }
          }
        },
        "number_of_shards": "1",
        "provided_name": "vehicles",
        "creation_date": "1670864230815",
        "number_of_replicas": "1",
        "uuid": "etLFicsvSXCpeuFiYCiT0g",
        "version": {
          "created": "8050299"
        }
      }
    }
  }
}

在索引中,比如color,它的类型是text,并且有一个字段keyword,我们如何使用和查询keyword字段?

voase2hg

voase2hg1#

当您要查询关键字字段时,只需在查询中使用color.keyword,如果您只想查询text部分,则只需在字段名称中使用color
textkeyword字段以不同方式进行标记和存储,并用于不同的场景,此answer将有助于理解差异。

相关问题