首页 > 其他 > 详细

elasticsearch

时间:2021-02-15 23:00:15      阅读:24      评论:0      收藏:0      [点我收藏+]

‘‘‘

GET _search
{
"query": {
"match_all": {}
}
}

GET s18/doc/1

DELETE s18

PUT s18/doc/1
{
"name":"ZYB",
"age":20,
"gender":"男",
"tags":"正人君子",
"b":"19941201"
}

PUT s18/doc/2
{
"name":"yuanyuan",
"age":18,
"gender":"女",
"tags":"活泼大方",
"b":"19900901"
}

PUT s18/doc/3
{
"name":"baobao",
"age":6,
"gender":"女",
"tags":"小孩子",
"b":"19970101"
}

PUT s18/doc/4
{
"name":"long",
"age":22,
"gender":"男",
"tags":"弟弟",
"b":"19930205"
}

PUT s18/doc/5
{
"name":"zhu",
"age":23,
"gender":"男",
"tags":"浪",
"b":"19980501"
}

GET s18/doc/2

查找字符串 query String

GET s18/doc/_search?q=age:23

修改指定字段

POST s18/doc/5/_update
{
"doc":{
"tag":"帅气"
}
}

结构化查询

GET s18/doc/_search
{
"query":{
"match":{
"age":18
}
}
}

GET s18/doc/_search
{
"query":{
"match":{
"tags":"浪 弟弟"
}
}
}

GET s18/doc/_search

GET s18/doc/_search
{
"query":{
"match_all":{}
}
}

排序

GET s18/doc/_search
{
"query":{
"match_all":{}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}

GET s18/doc/_search
{
"query":{
"match_all":{}
},
"sort": [
{
"age": {
"order": "asc"
}
}
]
}

GET s18/doc/_search
{
"query":{
"match_all":{}
},
"sort": [
{
"age": {
"order": "desc"
}
}
]
}

分页

GET s18/doc/_search
{
"query":{
"match_all":{}
},
"from":0,
"size":2
}

bool查询 bool:should(or) must(and) must_not(not)

GET s18/doc/_search
{
"query":{
"bool": {
"should": [
{
"match":{
"name":"zhu"
}
},
{
"match": {
"age": 18
}
}
]
}
}
}

查询 性别是男 并且年龄是23

GET s18/doc/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"gender": "男"
}
},
{
"match": {
"age": "23"
}
}
]
}
}
}

查询年龄大于20的男的 gt 大于 gte 大于等于 lt 小于 lte 小于等于 filter中尽量使用must,避免脏数据

GET s18/doc/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"gender": "男"
}
}
],
"filter": {
"range": {
"age": {
"gt": 20
}
}
}
}
}
}

高亮查询

GET s18/doc/_search
{
"query":{
"match": {
"name": "long"
}
},
"highlight": {
"fields": {
"name":{}
}
}
}

GET s18/doc/_search
{
"query":{
"match": {
"name": "yuanyuan"
}
},
"highlight": {
"pre_tags": "",
"post_tags": "
",
"fields": {
"name":{}
}
}
}

结果过滤

GET s18/doc/_search
{
"query": {
"match": {
"name": "yuanyuan"
}
},
"_source":["name","age"]
}

聚合查询sum

GET s18/doc/_search
{
"query": {
"match": {
"gender": "男"
}
},
"aggs": {
"my_sum": {
"sum": {
"field": "age"
}
}
}
}

查询年龄最大的男性 最小 min

GET s18/doc/_search
{
"query": {
"match": {
"gender": "男"
}
},
"aggs": {
"my_max": {
"max": {
"field": "age"
}
}
}
}

查询年龄最小的

GET s18/doc/_search
{
"aggs": {
"my_min": {
"min": {
"field": "age"
}
}
}
}

求平均值 avg

GET s18/doc/_search
{
"aggs": {
"my_avg": {
"avg": {
"field": "age"
}
}
}
}

分组查询 根据年龄,0-10,10-20,20-30

GET s18/doc/_search
{
"query": {
"match": {
"gender": "男"
}
},
"aggs": {
"my_group": {
"range": {
"field": "age",
"ranges": [
{
"from": 0,
"to": 10
},
{
"from": 10,
"to":20
},{
"from": 20,
"to":30
}
]
}
}
}
}

分组,根据年龄,0-10,10-20,20-30,对每组年龄求和

GET s18/doc/_search
{
"query": {
"match": {
"gender": "男"
}
},
"aggs": {
"group": {
"range": {
"field": "age",
"ranges": [
{
"from": 0,
"to": 10
},
{
"from": 10,
"to":20
},
{
"from":20,
"to":30
}
]
},
"aggs": {
"my_sum": {
"sum": {
"field": "age"
}
}
}
}
}
}

mappings

PUT s2
{
"mappings": {
"doc":{
"properties":{
"name":{
"type":"text"
},
"age":{
"type":"long"
},
"desc":{
"type":"text"
}
}
}
}
}

PUT s2/doc/1
{
"name":"zhang",
"age":21,
"desc":"低调"
}

GET s2/doc/_search

GET s18/_mapping
GET s2/_mapping

mapping的dynamic的三种状态 dynamic为flase时,新增字段不会自动添加映射关系,dynamic为strict时,为严格模式,不能新增字段

PUT s4
{
"mappings": {
"doc":{
"dynamic":true,
"properties":{
"name":{
"type":"text"
}
}
}
}
}

PUT s5
{
"mappings": {
"doc":{
"dynamic":false,
"properties":{
"name":{
"type":"text"
}
}
}
}
}

mapping的ignore_above 限定字段长度,不会被索引

PUT s7
{
"mappings": {
"doc":{
"properties":{
"title":{
"type":"keyword",
"ignore_above":10
}
}
}
}
}

PUT s7/doc/1
{
"title":"阿达大方阿达大就说了句就方阿达大方"
}

PUT s7/doc/2
{
"title":"123456"
}

PUT s7/doc/3
{
"title":"1234567"
}

GET s7/doc/_search
{
"query": {
"match": {
"title": "1234567"
}
}
}

GET s7/doc/_search
{
"query": {
"match": {
"title": "阿达大方阿达大就说了句就方阿达大方"
}
}
}

GET s7/_mapping

mappings参数 index 默认true,如果为false,则不会创建索引

PUT s8
{
"mappings": {
"doc":{
"properties":{
"t1":{
"type":"text",
"index":true
},
"t2":{
"type":"text",
"index":false
}
}
}
}
}

PUT s8/doc/1
{
"t1":"论文的书写方法",
"t2":"论技术安吉技术"
}
GET s8/doc/_search
{
"query": {
"match": {
"t1": "论文"
}
}
}

GET s8/doc/_search
{
"query": {
"match": {
"t2": "论文"
}
}
}

copy_to

PUT s9
{
"mappings": {
"doc":{
"properties":{
"t1":{
"type":"text",
"copy_to":"full_name"
},
"t2":{
"type":"text",
"copy_to":"full_name"
},
"full_name":{
"type":"text"
}
}
}
}
}

PUT s9/doc/1
{
"t1":"xxx",
"t2":"ooo"
}

GET s9/doc/_search
{
"query": {
"match": {
"t1": "xxx"
}
}
}

GET s9/doc/_search
{
"query": {
"match": {
"full_name": "ooo"
}
}
}

PUT s10
{
"mappings": {
"doc":{
"properties":{
"t1":{
"type":"text",
"copy_to":["f1","f2"]
},
"t2":{
"type":"text",
"copy_to":["f1","f2"]
},
"f1":{
"type":"text"
},
"f2":{
"type":"keyword"
}
}
}
}
}

嵌套

PUT w1
{
"mappings": {
"doc":{
"properties":{
"name":{
"type":"text"
},
"age":{
"type":"long"
},
"info":{
"properties":{
"addr":{
"type":"text"
},
"tel":{
"type":"long"
}
}
}
}
}
}
}

PUT w1/doc/1
{
"name":"tom",
"age":18,
"info":{
"addr":"北京",
"tel":"010"
}
}

GET w1/doc/_search
{
"query": {
"match": {
"name": "tom"
}
}
}

GET w1/doc/_search
{
"query": {
"match": {
"info.tel": "010"
}
}
}

分片 number_of_shards

GET w1

PUT w2
{
"settings": {
"mappings":{
"doc":{
"properties":{
"title":{
"type":"text"
}
}
}
},
"number_of_shards": 3,
"number_of_replicas": 2
}
}

GET s3/_mapping

PUT s3
{
"mappings" : {
"doc" : {
"properties" : {
"age" : {
"type" : "long"
},
"b" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"gender" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"tags" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}

match查询

查看分词

GET _analyze
{
"analyzer": "standard",
"text":"中国"
}

PUT t1/doc/1
{
"title":"中国是世界上人口最多的国家"
}

PUT t1/doc/2
{
"title":"美国是世界上军事实力最强大的国家"
}

PUT t1/doc/3
{
"title":"北京是中国的首都"
}

检索

GET t1/doc/_search
{
"query": {
"match": {
"title": "中国"
}
}
}

短语查询

GET t1/doc/_search
{
"query": {
"match_phrase": {
"title": "中国"
}
}
}

拼词查询 slop 允许中间有一个分割 中国是世界

GET t1/doc/_search
{
"query": {
"match_phrase": {
"title": {
"query": "中国世界",
"slop":1
}
}
}
}

最左前缀查询

PUT t2/doc/1
{
"title":"beautiful girl"
}

PUT t2/doc/2
{
"title":"beautiful so"
}

GET t2/doc/_search
{
"query": {
"match_phrase_prefix": {
"title": "bea"
}
}
}

多字段联查

PUT t3/doc/1
{
"t1":"beautiful girl",
"t2":"beautiful so"
}

GET t3/doc/_search
{
"query": {
"multi_match": {
"query": "beautiful",
"fields": ["t1","t2"]
}
}
}

GET t3/doc/_search
{
"query": {
"multi_match": {
"query": "beautiful",
"fields": ["t1","t2"],
"type":"phrase"
}
}
}

GET t3/doc/_search
{
"query": {
"multi_match": {
"query": "beautiful",
"fields": ["t1","t2"],
"type":"phrase_prefix"
}
}
}

路径分词器

POST _analyze
{
"tokenizer": "path_hierarchy",
"text":"/usr/local/python/python2.7"
}

邮箱分词器

POST _analyze
{
"tokenizer": "uax_url_email",
"text":"作者:zyb:位置原文:https://www.baidu.com/articles/123.html邮箱:zybyekai@icloud.com版权声明:我的东东"
}

GET _analyze
{
"analyzer": "standard",
"text":"上海自来水来自海上"
}

GET _analyze
{
"analyzer": "ik_smart",
"text":"上海自来水来自海上"
}

GET _analyze
{
"analyzer": "ik_max_word",
"text":"上海自来水来自海上"
}

‘‘‘

elasticsearch

原文:https://www.cnblogs.com/zybyekai/p/14403737.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!