es索引与数据结构 ,以及其他知识使用

本文深入探讨Elasticsearch的索引模板设置,包括如何创建名为'my_template'的模板,该模板针对'my-*'开头的索引,设定10个分片和0个副本。同时,我们也会讨论Elasticsearch的数据结构,帮助理解其存储和检索机制。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1:es   索引与数据结构映射
----es索引说明: 
索引结构: index/type/id   
对应的关系型数据库结构  index = database , type = table , id = id 


----默认
--插入新的索引
PUT test-index/default/1
{
  "test":"one",
  "other":1
}
--查询索引与数据结构
GET test-index/_mapping?pretty
-结果
{
  "test-index": {
    "mappings": {
      "default": {
        "properties": {
          "other": {
            "type": "long"
          },
          "test": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    }
  }
}


--解释
es会对字段类型进行分析和模式匹配,test的字段会映射为string,other字段映射为long


mapping - 映射和定义数据结构 
mapping由一个或多个analyzer(分词器)组成, analyzer又由一个或多个filter组成的。当ES索引文档的时候,字段中的内容传递给analyzer并通过子集的filters进行数据转换(分词,索引等操作)。


--自定义mapping
PUT newindex
{
  "mappings": {
      "default": {
        "properties": {
          "name": {
            "type": "text"
          },
          "age": {
            "type": "long"
          }
        }
      }
    }
}
--说明
索引newIndex - 指定 name为text ,age指定long


--查看
GET newIndex/_mapping?pretty


--测试
PUT newindex/default/1
{
  "name":"xxx",
  "age":"xxx"
}
--结果
mapper_parsing_exception
failed to parse [age]


--测试2
PUT newindex/default/1
{
  "name":"xxx",
  "age":11
}
--结果
"created": true




2:es  对binary数据类型的支持


--原理
该binary类型接受一个二进制值作为 Base64编码的字符串。该字段默认情况下不存储,不可搜索

--使用
PUT my_index
{
  "mappings": {
    "doc": {
      "properties": {
        "name": {
          "type": "text"
        },
        "blob": {
          "type": "binary" ,
          "doc_values" : true
        }
      }
    }
  }
}

数据插入:
PUT my_index/_doc/1
{
  "name": "Some binary blob",
  "blob": "U29tZSBiaW5hcnkgYmxvYg==" 
}

----在插件中获取
final ScriptDocValues.BytesRefs bytesRefs = (ScriptDocValues.BytesRefs)doc().get(" blob");
byte[] binary = bytesRefs.getValue().bytes;

3:es 中文分词器安装与使用   
--这里搬运自github  原github地址https://github.com/medcl/elasticsearch-analysis-ik
--安装
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.2.2/elasticsearch-analysis-ik-6.2.2.zip


--使用
1.create a index


curl -XPUT http://localhost:9200/index


2.create a mapping
curl -XPOST http://localhost:9200/index/fulltext/_mapping -H 'Content-Type:application/json' -d'
{
        "properties": {
            "content": {
                "type": "text",
                "analyzer": "ik_max_word",
                "search_analyzer": "ik_max_word"
            }
        }
    
}'
3.index some docs


curl -XPOST http://localhost:9200/index/fulltext/1 -H 'Content-Type:application/json' -d'
{"content":"美国留给伊拉克的是个烂摊子吗"}
'
curl -XPOST http://localhost:9200/index/fulltext/2 -H 'Content-Type:application/json' -d'
{"content":"公安部:各地校车将享最高路权"}
'
curl -XPOST http://localhost:9200/index/fulltext/3 -H 'Content-Type:application/json' -d'
{"content":"中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"}
'
curl -XPOST http://localhost:9200/index/fulltext/4 -H 'Content-Type:application/json' -d'
{"content":"中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"}
'
4.query with highlighting


curl -XPOST http://localhost:9200/index/fulltext/_search  -H 'Content-Type:application/json' -d'
{
    "query" : { "match" : { "content" : "中国" }},
    "highlight" : {
        "pre_tags" : ["<tag1>", "<tag2>"],
        "post_tags" : ["</tag1>", "</tag2>"],
        "fields" : {
            "content" : {}
        }
    }
}
'
Result


{
    "took": 14,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 2,
        "max_score": 2,
        "hits": [
            {
                "_index": "index",
                "_type": "fulltext",
                "_id": "4",
                "_score": 2,
                "_source": {
                    "content": "中国驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首"
                },
                "highlight": {
                    "content": [
                        "<tag1>中国</tag1>驻洛杉矶领事馆遭亚裔男子枪击 嫌犯已自首 "
                    ]
                }
            },
            {
                "_index": "index",
                "_type": "fulltext",
                "_id": "3",
                "_score": 2,
                "_source": {
                    "content": "中韩渔警冲突调查:韩警平均每天扣1艘中国渔船"
                },
                "highlight": {
                    "content": [
                        "均每天扣1艘<tag1>中国</tag1>渔船 "
                    ]
                }
            }
        ]
    }
}


4:es  索引模板
--场景
当定义多个类似结构的索引数据时使用
索引分段存储
命名my_* 会对所有以my_为前缀的索引进行结构映射

--使用

PUT _template/my_template
{
    "template" : "my-*",
    "order" : 0,
    "settings" : {
         "number_of_shards" : 10,
 "number_of_replicas" : 0
    },
    "mappings": {

      "default": {

  "_all": {
        "enabled": false
      }
        "properties": {
          "name": {
            "type": "text"
          },
          "age": {
            "type": "long"
          }
        }
    }
  }

}


5:es   查询缓存
--原理


--使用


6:es  分片与查询
--原理与意义


--合理使用


7: es系统监控工具 安装x-pack 
可看这篇文章:http://blog.youkuaiyun.com/ailice001/article/details/79474290






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值