首页 > 其他分享 >Elastic搜索引擎8.4+最新记录

Elastic搜索引擎8.4+最新记录

时间:2022-10-05 00:33:24浏览次数:78  
标签:search name Elastic GET 搜索引擎 test query match 8.4

******** 一、新增 *******

添加数据 /索引(index数据库)/类型(type表)/文档(document记录)

POST /test/_doc/1
{
"name": "刘备",
"age": 27,
"desc": "有志者事竟成"
}
POST /test/_doc/2
{
"name":"张飞",
"age":28,
"desc":"张飞在此"
}
POST /test/_doc/3
{
"name":"关羽",
"age":30,
"desc":"管管相扣"
}
POST /test/_doc/4
{
"name":"刘苏",
"age":30,
"desc":"零零零零"
}

POST /test1/_doc/1
{
"title":"重庆是雾都之城"
}
POST /test1/_doc/2
{
"title":"火锅繁荣城市之一重庆"
}
POST /test1/_doc/3
{
"title":"大庆都嫫阿缑"
}

******* 二、查询 ********

获取索引(库)信息

GET /test

获取单条记录

GET /test/_doc/1

查询该库下全部信息

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

条件查询

GET /test/_search?q=name:"刘备"

GET /test/_search
{
"query": {
"match": {
"name": "刘备"
}
}
}

test1

GET /test1/_search

匹配短语查询

GET /test1/_search
{
"query":{
"match_phrase":{
"title":"重庆"
}
}
}

查询title字段值短语为重庆的记录

GET /test1/doc/_search
{
"query": {
"multi_match": {
"query": "重庆",
"fields": ["title"],
"type": "phrase"
}
}
}

排序

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

分页

GET /test/_search
{
"query": {
"match_all": {}
},
"from": 0,
"size": 1
}

bool 查询 多条件组合查询(相当于sql中and条件)

GET /test/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"name": "刘备"
}
},{
"match": {
"age": 27
}
}
]
}
}
}

should 查询 多条件组合查询(相当于sql中or条件)

GET /test/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name": "刘备"
}
},{
"match": {
"age": 27
}
}
]
}
}
}

查询指定字段

GET /test/_search
{
"query": {
"match": {
"name": "刘备"
}
},
"_source": ["name"]
}

高亮查询

GET /test/_search
{
"query": {
"match": {
"name": "刘"
}
},
"highlight": {
"pre_tags": "<b class='key' style='color:red'>",
"post_tags": "</b>",
"fields": {
"name": {}
}
}
}

聚合查询avg、max、min、sum

GET /test/_search
{
"query": {
"match": {
"name": "刘"
}
},
"aggs": {
"my_avg": {
"avg": {
"field": "age"
}
}
},
"_source": ["name", "age"]
}

总结

#match 查询相关总结

match:返回所有匹配的分词

match_all:查询全部

match_phrase:短语查询,在match的基础上进一步查询词组,可以指定slop分词间隔

multi_match:多字段查询,使用相当的灵活,可以完成match_phrase和match_phrase_prefix的工作

match相当于模糊查询,term相当于精准查询

bool查询总结

must:与关系,相当于关系型数据库中的 and。

should:或关系,相当于关系型数据库中的 or。

must_not:非关系,相当于关系型数据库中的 not。

filter:过滤条件。

range:条件筛选范围。

gt:大于,相当于sql的 >

gte:大于等于,相当于sql的 >=

lt:小于,相当于sql的 <

lte:小于等于,相当于sql的 <=

***** 三、修改 *********

修改指定记录 修改时,不指定的属性会自动覆盖,只保留指定的属性

PUT test/_doc/1
{
"name":"赵云"
}

修改name为asvv(推荐使用)

POST /test/_update/2
{
"doc": {
"name": "赵云"
}
}

条件修改

POST /test/_update_by_query
{
"script": {
"source": "ctx._source.name='赵云'",
"lang": "painless"
},
"query": {
"match": {
"name": "张飞"
}
}
}

******** 四、删除 *******

根据id删除

DELETE /test/_doc/1

条件删除

POST /test/_delete_by_query
{
"query": {
"match": {
"name": "asvv"
}
}
}

删除整个索引记录

DELETE /test

官网: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-replication.html

标签:search,name,Elastic,GET,搜索引擎,test,query,match,8.4
From: https://www.cnblogs.com/hushaoz/p/16754893.html

相关文章