目录
创建索引
post:http://192.168.35.132:9200/my_doc/_doc/1
body中的数据
{
"id":1001,
"name":"imooc-1",
"desc":"imooc is very good, 慕课网非常牛",
"crate_time":"2019-12-24"
}
result:
{
"_index": "my_doc",
"_type": "_doc",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
创建mappping
put http://192.168.35.111:9200/index_mapping/
{
"mappings":{
"properties":{
"realname":{
"type":"text",
"index":true
},
"username":{
"type":"keyword",
"index":false
}
}
}
}
映射创建之后可以添加属性但不能修改,删除
POST http://192.168.35.111:9200/index_mapping/_mapping
{
"properties":{
"moneyy":{
"type":"double"
},
"sex":{
"type":"byte"
}
}
}
添加数据
可以直接添加数据,不需要创建mapping
POST: http://192.168.35.111:9200/my_douc/_doc/1
{
"id": 1001,
"name" :"imooc-1",
"desc": "imooc is very good,慕课网非常牛!",
"create_ date": "2019-12-24"
}
删除数据
只是逻辑删除,但数据还在磁盘中,等数据量大了清理
{
"_index": "my_doc",
"_type": "_doc",
"_id": "5",
"_version": 2,
"result": "deleted",
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"_seq_no": 3,
"_primary_term": 1
}
修改数据
post:http://192.168.35.132:9200/my_doc/_doc/1/_update
body:
{
"doc":{
"name":"我是xxx"
}
}
查询数据
term query:输入的查询内容是什么,就会按照什么去查询,并不会解析查询内容,对它分词
boolQueryBuilder.must(QueryBuilders.termQuery(key, value));查询key中包含value的数据
match query:首先会解析查询字符串,进行分词,然后查询
get: http://192.168.35.132:9200/my_doc/_doc/_search
http://192.168.35.111:9200/my_douc/_doc/_search?_source=id,name(只返回指定数据)
get: http://192.168.35.132:9200/my_doc/_doc/1
head:http://192.168.35.132:9200/my_doc/_doc/1 有数据就返回1
索引版本号(乐观锁)
{
"_index": "my_douc",
"_type": "_doc",
"_id": "3000",
"_version": 2,
"_seq_no": 17,
"_primary_term": 2,
"found": true,
"_source": {
"id": 22,
"name": "我是xxx",
"desc": "imooc is fashion, 慕课网非常时尚!",
"create_ date": "2019-12-25"
}
正确 POST :http://192.168.35.111:9200/my_douc/_doc/3000/_update?if_seq_no=17&if_primary_term=2
错误:POST : http://192.168.35.111:9200/my_douc/_doc/3000?version=2
新版本报错:
{
"error": {
"root_cause": [
{
"type": "action_request_validation_exception",
"reason": "Validation Failed: 1: internal versioning can not be used for optimistic concurrency control. Please use `if_seq_no` and `if_primary_term` instead;"
}
],
"type": "action_request_validation_exception",
"reason": "Validation Failed: 1: internal versioning can not be used for optimistic concurrency control. Please use `if_seq_no` and `if_primary_term` instead;"
},
"status": 400
}
分词
内置分词器 :
standard:
simple:按照非字母进行拆分
whitespace:根据空格拆分
stop:根据the ,is, a这些没有意义的单词
keyword:根据关键字
分词:
POST:http://192.168.35.111:9200/_analyze
{
"analyzer":"standard",
"text":"I study in imooc.com"
}
result:》》》》》》》》》》》》》》》》》》》》》》》
{
"tokens": [
{
"token": "i",
"start_offset": 0,
"end_offset": 1,
"type": "<ALPHANUM>",
"position": 0
},
{
"token": "study",
"start_offset": 2,
"end_offset": 7,
"type": "<ALPHANUM>",
"position": 1
},
{
"token": "in",
"start_offset": 8,
"end_offset": 10,
"type": "<ALPHANUM>",
"position": 2
},
{
"token": "imooc.com",
"start_offset": 11,
"end_offset": 20,
"type": "<ALPHANUM>",
"position": 3
}
]
}
中文分词器:需要添加插件
ik_max_word:细粒度很大
ik_smart:细粒度比较粗
http://192.168.35.111:9200/_analyze
{
"analyzer":"ik_max_word",
"text":"上下班车流量很大"
}
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
{
"tokens": [
{
"token": "上下班",
"start_offset": 0,
"end_offset": 3,
"type": "CN_WORD",
"position": 0
},
{
"token": "上下",
"start_offset": 0,
"end_offset": 2,
"type": "CN_WORD",
"position": 1
},
{
"token": "下班",
"start_offset": 1,
"end_offset": 3,
"type": "CN_WORD",
"position": 2
},
{
"token": "班车",
"start_offset": 2,
"end_offset": 4,
"type": "CN_WORD",
"position": 3
},
{
"token": "车流量",
"start_offset": 3,
"end_offset": 6,
"type": "CN_WORD",
"position": 4
},
{
"token": "车流",
"start_offset": 3,
"end_offset": 5,
"type": "CN_WORD",
"position": 5
},
{
"token": "流量",
"start_offset": 4,
"end_offset": 6,
"type": "CN_WORD",
"position": 6
},
{
"token": "很大",
"start_offset": 6,
"end_offset": 8,
"type": "CN_WORD",
"position": 7
}
]
}
定义分词:添加一个custom.dic文档,将自己希望组合的词语组合到一起