一、索引的增删改查
rest 肯定要大写哦哈哈
减少索引 : customer 索引名,_doc type类型 ,1 文档id 大括号中的为 申请体(及数据)
1、创立索引
PUT /索引名/类型名(会删除)/文档id
{
申请体
}
阐明:PUT手动指定索引
例:创立索引
PUT /customer/_doc/1
{
"name": "John Doe"
}
2、查问索引
GET /customer/_doc/1
3、删除索引
DELETE /customer
4、批改
PUT /customer/_doc/1 {
"name": "大帅逼"
}
阐明:这种形式会将version版本号减少,然而增加时须要与后面文档中数据数目统一不然会失落数据
POST /test20210304/_doc/1/_update
{
"doc":{
"name": "大帅逼"
}
}
二、索引的属性批改
创立索引规定,就像设置数据库的一些参数一样,
PUT /test1
{
"mappings": {
"properties": {
"name": {
"type":"text"
}
}
}
}
阐明:设置文档中对应词组的类型
GET /test1 查看索引的信息
三、查看elasticsearch状态
GET _cat/ 获取es的信息状态
四、文档的操作
1、基本操作
1)增加数据
PUT /testbasedoc/_doc/1
{
"name":"大帅比大水笔",
"age":"23",
"desc":"喜之郎果冻,盼盼我爱你",
"likes":["java 爪哇","可恶","刘月半"]
}
PUT /testbasedoc/_doc/2
{
"name":"鸡蛋",
"age":"23",
"desc":"喜之冻,牙医媒体",
"likes":["电影ae,ar","胖胖","倩妹子"]
}
PUT /testbasedoc/_doc/3
{
"name":"鸭蛋",
"age":"23",
"desc":"趣味使然的理发师",
"likes":["打游戏","剪头发","妹子"]
}
2)查问GET testbasedoc/_doc/1
3)更新
POST /test20210304/_doc/1/_update
{
"doc":{
"name": "大帅比2"
}
}
4)删除文档
DELETE /tehero_index/_doc/1
5)批量操作文档
a、查问多个_mget
POST /_mget
{
"docs":[
{
"_index": "testbasedoc", "_id": "1"
},
{
"_index":"testbasedoc", "_id": "2"
}
]
}
b、批量执行多个操作
POST _bulk
## 替换指定文档
{ "index" : { "_index" : "testbasedoc", "_type" : "_doc", "_id" : "1" } }
{ "this_is_field1" : "this_is_index_value" }
##删除指定文档
{ "delete" : { "_index" : "testbasedoc", "_type" : "_doc", "_id" : "1" } }
##创立文档
{ "create" : { "_index" : "testbasedoc", "_type" : "_doc", "_id" : "1" } }
{ "name":"大帅比大水笔","age":"23","desc":"喜之郎果冻,盼盼我爱你","likes":["java 爪哇","可恶","刘月半"]}
##更新文档
{ "update" : {"_id" : "1", "_type" : "_doc", "_index" : "testbasedoc"} }
{ "doc" : {"age" : "24"} }
在7.X中也能够去掉"_type" : "_doc"
2、简单操作 (排序,分页,高亮,含糊查问,精准查问)
1)简略搜寻_search?q=name:大帅比java
a、GET testbasedoc/_doc/_search?q=name:大帅比jav
b、GET /testbasedoc,customer/_search?q=name:”wenye” 多索引查问对立条件
GET testbasedoc/_doc/_search
{
"query": {
"match": {
"name":"大帅比"
}
}
}
查问后果
2)聚合查问 metric,bucket
metric:avf 、min、max、cardinality、sum、stats等
Bucket:想当与group by
1)平均值
POST /testbasedoc/_search
{
"aggs":{
"avg_age":{"avg":{"field":"age"}}
}
}
2)计数
POST /schools*/_search
{
"aggs":{
"distinct_name_count":{"cardinality":{"field":"name"}}
}
}
3)统计
POST /schools/_search
{
"aggs" : {
"fees_stats" : { "stats" : { "field" : "fees" } }
}
}
4) 最大聚合
POST /schools*/_search
{
"aggs" : {
"max_fees" : { "max" : { "field" : "fees" } }
}
}
5) 最小聚合
POST /schools*/_search
{
"aggs" : {
"min_fees" : { "min" : { "field" : "fees" } }
}
}
6)特定字段的总和
POST /schools*/_search
{
"aggs" : {
"total_fees" : { "sum" : { "field" : "fees" } }
}
}
7)terms 桶 准确查问 依据倒排索引进行准确索引
GET /testbasedoc1/_search
{
"aggs": { # 应用聚合的必须带如设置_mapping 必须以properties结尾
"boyfirl_term": {
"terms": {
"field": "age"
},
"aggs" :{
"avg_age":{
"avg":{"field":"age"}
}
}
}
}
}
3、搜寻相干
#搜寻
#简略搜寻 match_all
POST /testbasedoc1/_search
{
"query": {
"match_all": {}
}
, "_source": ["name","age"] # 指定显示的字段
}
#分页查问
POST /testbasedoc1/_search
{
"from":0, #哪里开始
"size": 2, #查问出的总个数
"query": {
"match_all": {}
}
}
#排序
POST /testbasedoc1/_search
{
"sort":[{"age":"asc"}],
"from":0,
"size": 20,
"query": {
"match_all": {}
}
}
#match 匹配查问
POST /testbasedoc1/_search
{
"query": {
"match": {
"age": 23
}
}
}
#(multi_mathc)多种匹配查问
POST /testbasedoc1/_search
{
"query": {
"multi_match": {
"query": "23", #value
"fields": ["name","age"] #column 数组
}
}
}
#布尔查问
POST /testbasedoc1/_search
{
"query": {
"bool":{ #返回的会是boolean值
"must":[ #must 必须相等 must_not 不相等 should (or)
{
"match":{
"name":"大帅比"
}
},
{
"match":
{
"age":"23"
}
}
]
}
}
}
#过滤
POST /testbasedoc1/_search
{
"query": {
"bool":{
"must":[
{
"match":{
"name":"鸭蛋"
}
},
{
"match":
{
"age":"24"
}
}
],
"filter": [ #过滤
{
"range": {
"FIELD": {
"gte": 10,
"lte": 20
}
}
}
]
}
}
}
#query :查问蕴含str的文档
POST /testbasedoc1/_search
{
"query":{
"query_string":{
"query":"爱"
}
}
}
#短语搜寻 - Match Phrase 搞不清
POST testbasedoc1/_search
{
"query": {
"match_phrase": {
"desc":{
"query": "趣味使,然理发师"
, "slop": 1
}
}
}
}
#term 查问 次要用于数字日期相干
POST /testbasedoc/_search
{
"query":{
"term":{"age":"23"}
}
}
#range 查问范畴查问
POST /testbasedoc*/_search
{
"query":{
"range":{
"age":{
"gte":3.5 #gte:大于等于 gt:大于 lte:小于等于 lt:小于
}
}
}
}
4、对于分词
- term : 准确查问,不会对词组进行解析
- match: 先进行文档解析,而后进行匹配
- 类型为keyword的不会被分词器解析
5、高亮显示
默认:
POST /testbasedoc/_search
{
"query":{
"match":{"name":"大帅比"}
},
"highlight": {
"fields": {
"name": {}
}
}
}
自定义 高亮条件 pre_tags post_tages:
POST /testbasedoc/_search
{
"query":{
"match":{"name":"大帅比"}
},
"highlight": {
"pre_tags":"<p class='key' style='color:red'>",
"post_tags":"</p>",
"fields": {
"name": {}
}
}
}
匹配的后果
"hits" : [
{
"_index" : "testbasedoc",
"_type" : "_doc",
"_id" : "1",
"_score" : 2.0487494,
"_source" : {
"name" : "大帅比大水笔",
"age" : 23,
"desc" : "喜之郎果冻,盼盼我爱你",
"likes" : [
"java 爪哇",
"可恶",
"刘月半"
]
},
"highlight" : {
"name" : [
"<em>文</em><em>烨</em>大水笔" 高亮
]
发表回复