
由于es搜索主要依赖于内存,如果内存大小可以容纳总的数据量,性能会非常高,但由于硬件条件受限通常采取优化索引结构的方式,减少内存消耗,节省存储容量,提高查询响应速度。如es中只存放需要查询的数据,源数据放入hbase中,es的id可以使用hbase的rowKey,这样就大大节省了存储空间。另外将不需要分词的字段使用keyword类型或避免使用wildcard、join或关闭默认的排序属性等方式优化索引结构。
es的应用非常广泛,腾讯文档、腾讯云监控基于es做全文检索、拼多多商品搜索、京东、淘宝、去哪儿都有es的身影。现将基础用法分享,本次分享主要围绕es基础用法,用于为es的优化提供数据支撑。
创建索引
PUT /test_2020_index
{
"settings": {
"number_of_shards": 2,
"number_of_replicas": 1
}
}
查看索引结构
GET /test_2020_index/
设置索引settting
PUT /test_2020_index/_settings
{
"analysis" : {
"analyzer" : {
"default" : {
"tokenizer" : "ik_max_word"
},
"my_path_analyzer" : {
"tokenizer" : "path_hierarchy"
},
"my_douhao_analyzer":{
"tokenizer" : "douhao"
}
},
"tokenizer":{
"douhao":{
"type":"pattern",
"pattern":","
}
}
}
}
这里用到了ik分词器、路径分词器、逗号分词器
设置索引字段的mapping即字段信息
POST /test_2020_index/_doc/_mappings
{
"_doc":{
"properties": {
"name": {
"type": "text",
"analyzer": "my_path_analyzer"
},
"storeId":{
"type": "keyword"
},
"keyWord": {
"type": "text",
"analyzer": "my_douhao_analyzer"
}
}
}
}
数据的插入与修改
POST /test_2020_index/_doc/1
{
"name":"001/002/003",
"storeId":86,
"keyWord":"ios,苹果,手机"
}
数据检索
GET /test_2020_index/_search
{
"query":{
"bool":{
"must":[
{
"match_all":{
}
}
]
}
}
}
删除指定索此的数据
POST /test_2020_index/_doc/_delete_by_query
{
"query":{
"term":{
"_id":"5a"
}
}
}