我在弹性搜索索引中有一个文本类型的 cityName 字段
“城市名称”:{ “类型”:“文本”, “索引”:假 }
要对此进行查询,我需要进行完整的重新索引,还是有其他方法可以做到这一点?我正在使用弹性7.1版本.
是的,有一些方法可以使字段建立索引。
update_by_query
第一种方法是
update_by_query
使用非索引字段映射
PUT /index_field
{
"mappings": {
"properties": {
"cityName": {
"type": "text",
"index": false
}
}
}
}
文件
PUT /index_field/_bulk
{"create":{"_id":1}}
{"cityName":"Kungur"}
update_by_query
将 cityName
字段复制到 city_name_indexed
字段
POST /index_field/_update_by_query
{
"script": {
"source": "ctx._source['city_name_indexed'] = ctx._source['cityName']"
}
}
让我们通过全文查询来搜索
GET /index_field/_search?filter_path=hits.hits
{
"query": {
"match": {
"city_name_indexed": "kungur"
}
}
}
回应
{
"hits" : {
"hits" : [
{
"_index" : "index_field",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.2876821,
"_source" : {
"cityName" : "Kungur",
"city_name_indexed" : "Kungur"
}
}
]
}
}
让我们通过
GET /index_field/_mapping
探索映射
回应
{
"index_field" : {
"mappings" : {
"properties" : {
"cityName" : {
"type" : "text",
"index" : false
},
"city_name_indexed" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
现在
city_name_indexed
字段已建立索引
第二种方法是通过查询更新映射
PUT /index_field/_mapping
{
"properties": {
"city_name_indexed_too": { "type": "text"}
}
}
然后
update_by_query
与 city_name_indexed_too
字段