【官网】https://www.elastic.co/guide/cn/elasticsearch/guide/current/_creating_an_index.html
【api信息】https://elasticsearch-py.readthedocs.io/en/master/api.html
一个 Elasticsearch 集群可以 包含多个 索引 ,相应的每个索引可以包含多个 类型 。 这些不同的类型存储着多个 文档 ,每个文档又有 多个 属性 。
如果你对index还没有概念,建议你去看下官网文档:https://www.elastic.co/guide/cn/elasticsearch/guide/current/_indexing_employee_documents.html
在shell 中输入下面的信息,就可以创建一个简单的索引了:
curl -X PUT "localhost:9200/megacorp/employee/1" -H 'Content-Type: application/json' -d'
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
'
这个索引采用的是默认的配置,新的字段通过动态映射的方式被添加到类型映射。现在我们需要对这个建立索引的过程做更多的控制:我们想要确保这个索引有数量适中的主分片,并且在我们索引任何数据 之前 ,分析器和映射已经被建立好。
为了达到这个目的,我们需要手动创建索引,在请求体里面传入设置或类型映射,如下所示:
curl -X PUT "localhost:9200/my_index" -H 'Content-Type: application/json' -d'
{
"settings": { ... any settings ... },
"mappings": {
"type_one": { ... any mappings ... },
"type_two": { ... any mappings ... },
...
}
}
举一个建立索引的例子,在官网中,字符串排序与多字段说的不是很清楚:https://www.elastic.co/guide/cn/elasticsearch/guide/current/multi-fields.html
比如说一个用空格分开的字段date日期信息,
注意在elasticSearch里,日期信息不要用2019-07-01
的形式,可能会导致搜索无效,也许是因为包含了特殊字符!!!!!
2019.07.01 08:26
2019.07.01 09:26
2019.07.01 10:26
2019.07.01 09:20
2019.07.02 08:28
我们想对它进行排序,虽然es有定义date类型,但是太长了,我们也不需要那么多信息,
于是需要自定义索引,
普通的索引是不够用的,es为了能够进行文档检索,空格之间的字段是单独的,分开的,
也就是,对上面这个date字段排序,只能对2019.07.xx排序,后面的时间信息依旧是无序的,我们需要把它按照一个字段来排序,但是一旦索引指定了全文,就会导致对时间如 10:26 的查找结果变成了找不到这个信息。所以为了解决这个矛盾, 引入一个简单的方法是用两种方式对同一个字符串进行索引,这将在文档中包括两个字段: analyzed 用于搜索, not_analyzed 用于排序
但是保存相同的字符串两次在 _source 字段是浪费空间的。 我们真正想要做的是传递一个 单字段
但是却用两种方式索引它。
所以用下面的形式对数据库test建立索引,特别注意"properties" 里面的 “cdate” 类型,
"cdate" : {
"type" : "text",
"analyzer": "whitespace",
"fields" : {
"raw": {
"type": "text",
"index": "not_analyzed"
},
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
},
},
key | Value |
---|---|
analyzer | whitespace 按照空格分割 |
type | text:文本,keyword:键值 |
fields | 内容区域 ,这里定义了两个索引:raw和keyword,默认键值是keyword,用cdate.raw来引用raw索引 |
raw | 自定义的索引名,可以 任意 |
keyword | 官方定义的默认键值 |
index | not_analyzed 不解析 |
ignore_above | 忽略256以上的值 |
curl -X PUT 'xxx.xxx.168.xxx:9200/test' -H 'Content-Type: application/json' -d '
{
"mappings" : {
"cluster_feature" : {
"properties" : {
"cdate" : {
"type" : "text",
"analyzer": "whitespace",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"feature_data" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"feature_type" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"set_id" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"set_name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}'
number_of_shards
每个索引的主分片数,默认值是 5 。这个配置在索引创建后不能修改。
number_of_replicas
每个主分片的副本数,默认值是 1 。对于活动的索引库,这个配置可以随时修改。
Elasticsearch 提供了优化好的默认配置。 除非你理解这些配置的作用并且知道为什么要去修改,否则不要随意修改。
curl -X PUT 'xxx.xxx.168.xxx:9200/test' -H 'Content-Type: application/json' -d '
里面xxx.xxx.168.xxx为你的网址,然后9200根据你的配置端口修改
而且,已经建立了索引的index,似乎是不可以重建或者修改index的。
所以你要么重新创建index,要么删掉已有index,创建index
参考网址:https://blog.csdn.net/zx711166/article/details/82390925
尽管可以增加新的类型到索引中,或者增加新的字段到类型中,但是不能添加新的分析器或者对现有的字段做改动。 如果你那么做的话,结果就是那些已经被索引的数据就不正确, 搜索也不能正常工作。
对现有数据的这类改变最简单的办法就是重新索引:用新的设置创建新的索引并把文档从旧的索引复制到新的索引。字段 _source 的一个优点是在Elasticsearch中已经有整个文档。你不必从源数据中重建索引,而且那样通常比较慢。
为了有效的重新索引所有在旧的索引中的文档,用 scroll 从旧的索引检索批量文档 , 然后用 bulk API 把文档推送到新的索引中。从Elasticsearch v2.3.0开始, Reindex API 被引入。它能够对文档重建索引而不需要任何插件或外部工具。
参考网址:https://www.elastic.co/guide/cn/elasticsearch/guide/current/reindex.html