{
“text”:“我爱学习,最爱学Java”,
“analyzer”:“ik_max_word”
}
2.5.4 测试分词_粗粒度拆分
{
“text”:“我爱学习,最爱学Java”,
“analyzer”:“ik_smart”
}
2.5.5 自定义分词库
<?xml version="1.0" encoding="UTF-8"?>首先在IKAnalyzer.cfg.xml里面定义自定义的字典,我这里是main.dic(自己随意命名);
IK Analyzer 扩展配置
main.dic
=====================================================================================
PUT /kak
{
“settings”: {
“index”:{
“number_of_shards”:5,
“number_of_replicas”:3
}
}
}
#查看单个
GET /kak/_settings
#查看所有
GET /_all/_settings
PUT /kak/student/1
{
“name”:“kaka”,
“sex”:“man”,
“age”:23,
“score”:100
}
POST /kak/student/2
{
“name”:“taotao”,
“sex”:“woman”,
“age”:23,
“score”:100
}
GET /kak/student/1
GET /kak/student/_search
PUT方法:存在就修改,不存在就新增;
PUT /kak/student/1
{
“name”:“kaka”,
“sex”:“man”,
“age”:23,
“score”:150
}
#删除单个
DELETE /kak/student/2
#删除全部
DELETE /kak
===============================================================================
创建索引、添加文档
PUT kak_01
PUT kak_01/student/1
{
“name”:“kaka”,
“age”:23,
“sex”:“man”
}
PUT kak_01/student/2
{
“name”:“zhuangzhuang”,
“age”:23,
“sex”:“woman”
}
PUT kak_02
PUT kak_02/student/1
{
“name”:“zhangsan”,
“age”:23,
“sex”:“man”
}
PUT kak_02/student/2
{
“name”:“lisi”,
“age”:23,
“sex”:“woman”
}
GET /_mget
{
“docs”:[
{
“_index”: “kak_01”,
“_type”: “student”,
“_id”: 1
},
{
“_index”: “kak_01”,
“_type”: “student”,
“_id”: 2
}
]
}
GET /_mget
{
“docs”:[
{
“_index”:“kak_01”,
“_type”:“student”,
“_id”:1,
“_source”:[“name”,“age”]
},
{
“_index”:“kak_01”,
“_type”:“student”,
“_id”:2,
“_source”:[“name”,“age”,“sex”]
}
]
}
GET /kak_01/student/_mget
{
“docs”:[
{
“_id”:1
},
{
“_id”:2
}
]
}
=======================================================================================
语法格式:
- {action:{metadata}}
- {requstbody}
- action:(行为)
* create:文档不存在时创建
* update:更新文档
* index:创建新文档或替换已有文档
* delete:删除一个文档
create 和index的区别:
- 如果数据存在,使用create操作失败,会提示文档已经存在;
- 如果数据存在,使用index则可以成功执行;
POST kak_01/student/_bulk
{“index”:{“_id”:3}}
{“name”:“kak_03”,“age”:23,“sex”:“woman”}
{“index”:{“_id”:4}}
{“name”:“kak_04”,“age”:23,“sex”:“woman”}
{“index”:{“_id”:5}}
{“name”:“kak_05”,“age”:23,“sex”:“man”}
GET kak_01/student/_mget
{
“ids”:[1,2,3]
}
POST kak_01/student/_bulk
{“delete”:{“_index”:“kak_01”,“_type”:“student”,“_id”:4}}
{“delete”:{“_index”:“kak_01”,“_type”:“student”,“_id”:5}}
操作的大小建议:
Bulk会把将要处理的数据载入内存中,所以数据量是有限制的,最佳的数据量不是一个确定的数值取决于硬件;
- 一般建议是1000-5000个文档,大小建议是5-15MB,默认不能超过100M,可以在ES的配置文件($ES_HOME下的config下的elasticsearch.yml)中设置;
====================================================================================
ElasticSearch采用了乐观锁来保证数据的一致性,当用户对document进行操作时,并不需要对该document作加锁和解锁的操作,只需要指定要操作的版本;当版本号一致时,ElasticSearch会允许该操作顺利执行,当版本号存在冲突时,ElasticSearch会提示冲突并抛出异常(VersionConflictEngineException异常);
-
ElasticSearch的版本号的取值范围为1到2^63-1。
-
使用的是_version(每修改一个版本加一)
Elasticsearch在处理外部版本号时会与对内部版本号的处理有些不同。它不再是检查version是否与请求中指定的数值相同,而是检查当前的version是否比指定的数值小。如果请求成功,那么外部的版本号就会被存储到文档中的version中。为了保持version与外部版本控制的数据一致使用version_type=external ;
==============================================================================
查看自动生成的映射情况
GET kak_01/_mapping
核心数据类型:
字符型:string
- string类型包括,text 和 keyword
* text类型被用来索引长文本,在建立索引前会将这些文本进行分词,转化为词的组合,建立索引。允许es来检索这些词语。text类型不能用来排序和聚合。
* Keyword类型不需要进行分词,可以被用来检索过滤、排序和聚合。
* keyword 类型字段只能用本身来进行检索
- 数字型:long, integer, short, byte, double, float
- 日期型:date
- 布尔型:boolean
- 二进制型:binary
关键的属性:
- “index”: true分词,false不分词,设置成false,字段将不会被索引;
- “analyzer”:"ik"指定分词器,默认分词器为standard analyzer
PUT /lib
{
“settings”:{
“number_of_shards” : 3,
“number_of_replicas” : 0
},
“mappings”:{
“properties”:{
“title”:{“type”:“text”},
“name”:{“type”:“text”,“analyzer”:“standard”},
“publish_date”:{“type”:“date”,“index”:false},
“price”:{“type”:“double”}
}
}
}
![在这里插入图片描述](https://img-blog.csdnimg.cn/20200930001346233.png?x-
oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MjYwMTEzNg==,size_16,color_FFFFFF,t_70#pic_center)
- 定义Mapping没有类型,之前能写进去是因为通过PUT,放文档放进去的;
===========================================================================
PUT /lib2
{
“settings”:{
“number_of_shards” : 3,
“number_of_replicas” : 0
},
“mappings”:{
“properties”:{
“name”: {“type”:“text”},
“address”: {“type”:“text”},
“age”: {“type”:“integer”},
“interests”: {“type”:“text”},
“birthday”: {“type”:“date”}
}
}
}
PUT /lib2/_doc/1
{
“name” : “kak”,
“address” : “sx”,
“age” : 32,
“interests” : “java,music”,
“birthday”: “1988-08-08”
}
PUT /lib2/_doc/2
{
“name” : “taotao”,
“address” : “sz”,
“age” : 33,
“interests” : “java,music,money”,
“birthday”: “1983-08-08”
}
PUT /lib2/_doc/2
{
“name” : “taotao”,
“address” : “sz”,
“age” : 33,
“interests” : “java,music,money”,
“birthday”: “1983-08-08”
}
term查询
8.2.1 查询某个字段里含有某个关键词的文档
GET /lib2/_search/
{
“query”: {
“term”: {
“name”: {
“value”: “kak”
}
}
}
}
8.2.2 查询某个字段里含有多个关键词的文档
GET /lib2/_search/
{
“query”: {
“terms”: {
“interests”: [
“java”,
“music”
]
}
}
}
GET /lib2/_search
{
“from”: 0,
“size”: 2,
“query”: {
“terms”: {
“interests”: [
“java”,
“music”
]
}
}
}
GET /lib2/_search
{
“version”: true,
“from”:0,
“size”: 2,
“query”: {
“terms”: {
“interests”: [
“java”,
“money”
]
}
}
}
match query知道分词器的存在,会对filed进行分词操作,然后再查询
GET /lib2/_search
{
“query”: {
“match”: {
“name”: “kak helloworld”
}
}
}
GET /lib2/_search
{
“query”: {
“multi_match”: {
“query”: “java”,
“fields”: [“interests”,“name”]
}
}
}
ElasticSearch引擎首先分析查询字符串,从分析后的文本中构建短语查询,这意味着必须匹配短语中的所有分词,并且保证各个分词的相对位置不变
GET /lib2/_search
{
“query”: {
“match_phrase”: {
“interests”: “java,music”
}
}
}
GET /lib2/_search
{
“_source”: [“address”,“name”],
“query”: {
“match”: {
“interests”: “java”
}
}
}
GET /lib2/_search
{
“query”: {
“match_all”: {}
},
“sort”: [
{
“age”: {
“order”: “desc”
}
}
]
}
GET /lib2/_search
{
“query”: {
“match_phrase_prefix”: {
“name”: {
“query”: “kak”
}
}
}
}
GET /lib2/_search
{
“query”: {
“range”: {
“birthday”: {
“from”: “1988-08-08”,
“to”: “2020-08-08”,
“include_lower”: true,
“include_upper”: false
}
}
}
}
允许使用通配符* 和 ?来进行查询:
- *代表0个或多个字符
- ?代表任意一个字符
GET /lib2/_search
{
“query”: {
“wildcard”: {
“name”: “kak*”
}
}
}
GET /lib2/_search
{
“query”: {
“match”: {
“interests”: “java”
}
},
“highlight”: {
“fields”: {
“interests”: {}
}
}
}
===============================================================================
- 可以实现组合过滤查询
- must:必须满足的条件(and)
- should:可以满足也可以不满足的条件(or)
- must_not:不需要满足的条件(not )
GET /lib2/_search
{
“post_filter”: {
“bool”: {
“should”:[
{“term”:{“age”:32}},
{“term”:{“interests”:“java”}}
],
“must_not”:{
“term”:{“age”:90}
}
}
}
}
GET /lib2/_search
{
“post_filter”: {
“bool”: {
“must”:[
{“term”:{“age”:32}},
{“term”:{“interests”:“java”}}
]
}
}
}
============================================================================
- gt:>
- lt: <
- gte: >=
- lte: <=
GET /lib2/_search
{
“post_filter”: {
“range”: {
“age”: {
“gte”: 10,
“lte”: 32
}
}
}
}
============================================================================
sum:求和
GET /lib2/_search
{
“size”: 0,
“aggs”: {
“agesum”: {
“sum”: {
“field”: “age”
}
}
}
}
min:最小值
GET /lib2/_search
{
“size”: 0,
“aggs”: {
“agemin”: {
“min”: {
“field”: “age”
}
}
}
}
- max:最大值
GET /lib2/_search
{
“size”: 0,
“aggs”: {
“agemax”: {
“max”: {
“field”: “age”
}
}
}
}