Deprecated: Optional parameter $keys declared before required parameter $cms_id is implicitly treated as a required parameter in /home/www/dev/work/class/blog/CmsKey.php on line 75

Deprecated: Creation of dynamic property lvesu\lvesu\controller\blog\main::$outlink is deprecated in /home/www/dev/work/website/lvesu/template/blog/cms/cms.tpl on line 2

Deprecated: Creation of dynamic property lvesu\lvesu\controller\blog\main::$status is deprecated in /home/www/dev/work/website/lvesu/template/blog/index.head.php on line 2
Elasticsearch 映射参数 fields - 互联网笔记

略微加速

略速 - 互联网笔记

Elasticsearch 映射参数 fields

2021-03-01 leiting (4397阅读)

标签 Elasticsearch

fields



处于不同的目的,通过不同的方法索引相同的字段通常非常有用。这也是多字段的目的。例如,一个字符串字段可以映射为text字段用于全文本搜索,也可以映射为keyword字段用于排序或聚合。

PUT my_index
{
  "mappings": {
    "_doc": {
      "properties": {
        "city": {
          "type": "text",
          "fields": {
            "raw": { 
              "type":  "keyword"
            }
          }
        }
      }
    }
  }
}

note:city.raw字段是city字段的keyword版本。

GET my_index/_search
{
  "query": {
    "match": {
      "city": "york" 
    }
  },
  "sort": {
    "city.raw": "asc" 
  },
  "aggs": {
    "Cities": {
      "terms": {
        "field": "city.raw" 
      }
    }
  }
}

note:city字段用于全文本搜索。
note:city.raw用于排序与聚合。

多字段不能修改原始_source字段。

对于相同索引中具有相同名称的字段,fields设置允许有不同的设置。可以使用PUT映射API将新的多字段添加到已存在的字段中。

带有多个分析的多字段

多字段的另一个应用场景是使用不同的方法分析相同的字段以求获得更好的相关性。

PUT my_index
{
  "mappings": {
    "_doc": {
      "properties": {
        "text": { 
          "type": "text",
          "fields": {
            "english": { 
              "type":     "text",
              "analyzer": "english"
            }
          }
        }
      }
    }
  }
}

note:text.field字段使用english分析器。

翻译源:Elasticsearch 6.4 文档

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3