Elasticsearch学习笔记2: _mapping

mapping

映射定义了一个文档和其包含的数据如何被索引到elasticsearch中的规则,哪些字段是数字,哪些字段是字符串,哪些字段是时间格式登,定义字符串字段的分析器,定义时间字段的格式等

获取或者更新一个索引或者类型的映射为_mapping

GET http://localhost:9200/website/blog/_mapping
{
    "bbs": {
        "mappings": {
            "thread": {
                "properties": {
                    "city_id": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    },
                    "content": {
                        "type": "text",
                        "analyzer": "ik_smart"
                    },
                    "forum_id": {
                        "type": "long"
                    },
                    "publish_time": {
                        "type": "date",
                        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
                    },
                    "reply_count": {
                        "type": "long"
                    },
                    "thread_id": {
                        "type": "long"
                    },
                    "title": {
                        "type": "text",
                        "analyzer": "ik_smart"
                    },
                    "user_id": {
                        "type": "long"
                    },
                    "username": {
                        "type": "text"
                    }
                }
            }
        }
    }
}

设置

PUT http://localhost:9200/website/blog/_mapping
{
    "bbs": {
        "mappings": {
            "thread": {
                "properties": {
                    "city_id": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    },
                    "content": {
                        "type": "text",
                        "analyzer": "ik_smart"
                    },
                    "forum_id": {
                        "type": "long"
                    },
                    "publish_time": {
                        "type": "date",
                        "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
                    },
                    "title": {
                        "type": "text",
                        "analyzer": "ik_smart"
                    },
                    "user_id": {
                        "type": "long"
                    },
                    "username": {
                        "type": "text"
                    }
                }
            }
        }
    }
}

属性的类型:

  • 基础类型text, keyword, date, long, double, boolean, ip.
  • 支持多层json格式的object, nested.
  • 其他一些特殊类型 geo_point, geo_shape, completion

text

文本格式,索引时,会根据设置的分析器,分词,处理字符串,分成适合于倒排索引的独立的词条

analyzer: 设置分析器,在索引时和搜索时(无设置search_analyzer时)会被用来处理字符串, 默认为standard
search_analyzer: 设置搜索分析器,在搜索时用来处理字符串
boost: 设置字段相关性权重,搜索时影响权重值
index:  设置字段是否可以搜索
    analyzed
        首先分析字符串,然后索引它。换句话说,以全文索引这个域。
    not_analyzed
        索引这个域,所以可以搜索到它,但索引指定的精确值。不对它进行分析。
    no
        Don’t index this field at all不索引这个域。这个域不会被搜索到。
norms: 设置字段查询相关度是否受字段长度影响 默认true

keyword

ignore_above: 索引的最长长度

date

format:格式("yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis")

猜你喜欢

转载自my.oschina.net/u/2299936/blog/1560571