C/C++教程

ElasticSearch的REST接口操作(一)

本文主要是介绍ElasticSearch的REST接口操作(一),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
# 查看集群的健康状态
GET /_cat/health?v

# 查看节点状态
GET /_cat/nodes?v

# 查看索引的状态
GET /_cat/indices?v

# 创建一个索引
PUT /movie_index
# 删除索引
DELETE /movie_index

# 查看某一个索引的分片情况
GET /_cat/shards/movie_index?v

# 创建文档
PUT /movie_index/movie/1
{
    "id":100,
    "name":"operation red sea",
    "doubanScore":8.5,
    "actorList":[
        {
            "id":1,
            "name":"zhang yi"
        },
        {
            "id":2,
            "name":"hai qing"
        },
        {
            "id":3,
            "name":"zhang han yu"
        }
    ]
}

PUT /movie_index/movie/2
{
    "id":200,
    "name":"operation meigong river",
    "doubanScore":8,
    "actorList":[
        {
            "id":3,
            "name":"zhang han yu"
        }
    ]
}
# PUT(幂等性操作)
PUT /movie_index/movie/3
{
    "id":300,
    "name":"incident red sea",
    "doubanScore":5,
    "actorList":[
        {
            "id":4,
            "name":"zhang san feng"
        }
    ]
}

# 查看一个索引中所有的文档
GET /movie_index/_search

# 根据文档 id 查看文档
GET /movie_index/movie/3

# 根据文档 id 删除文档
DELETE /movie_index/movie/3

# 根据文档 id 更新文档
POST /movie_index/movie/3/_update?pretty
{
"doc": {"name":"wudang"}
}

# 根据条件更新文档(了解)
POST /movie_index/_update_by_query
{
"query": {
"match":{
"actorList.id":1
}
},
"script": {
"lang": "painless",
"source":"for(int
i=0;i<ctx._source.actorList.length;i++){if(ctx._source.actorList[i].id==3){ctx._source.
actorList[i].name='tttt'}}"
}
}


##################查询##############################
# 按条件查询(全部)
GET /movie_index/_search
{
    "query":{
        "match_all":{

        }
    }
}

# 按分词查询(必须使用分词 text 类型)
# text:分词
# keyword:不分词

GET movie_index/movie/_search
{
    "query":{
        "match":{
            "name":"operation red sea"
        }
    }
}
# 按分词子属性查询(分词匹配)
GET movie_index/movie/_search
{
  "query": {
    "match": {
      "actorList.name": "zhang han yu"
    }
  }
}

#按短语查询(相当于 like %短语%)
GET movie_index/movie/_search
{
    "query":{
        "match_phrase":{
            "actorList.name":"zhang han yu"
        }
    }
}

#  通过 term 精准搜索匹配(必须使用 keyword 类型)
GET movie_index/movie/_search
{
  "query": {
    "term": {
      "actorList.name.keyword": "zhang han yu"
    }
  }
}

# fuzzy 查询(容错匹配)
# 校正匹配分词,当一个单词都无法准确匹配, ES通过一种算法对非常接近的单词也给与一定的评分,能够查出来,但是消耗更多的性能,对中文来讲, 实现不是特别好。
GET movie_index/movie/_search
{
    "query":{
        "fuzzy":{
            "name":"rad"
        }
    }
}

# 过滤—先匹配,再过滤
GET movie_index/movie/_search
{
    "query":{
        "match":{
            "name":"red"
        }
    },
    "post_filter":{
        "term":{
            "actorList.id":3
        }
    }
}

# 过滤—匹配和过滤同时(推荐使用)
GET movie_index/movie/_search
{
    "query":{
        "bool":{
            "must":[
                {
                    "match":{
                        "name":"red"
                    }
                }
            ],
            "filter":[
                {
                    "term":{
                        "actorList.id":"1"
                    }
                },
                {
                    "term":{
                        "actorList.id":"3"
                    }
                }
            ]
        }
    }
}


# 过滤--按范围过滤
GET movie_index/movie/_search
{
    "query":{
        "range":{
            "doubanScore":{
                "gte":6,
                "lte":8.5
            }
        }
    }
}


# 排序
GET movie_index/movie/_search
{
    "query":{
        "match":{
            "name":"operation"
        }
    },
    "sort":{
        "doubanScore":{
            "order":"desc"
        }
    }
}

# 分页查询
GET movie_index/movie/_search
{
    "query":{
        "match_all":{

        }
    },
    "from":0,
    "size":10
}

# 指定查询的字段
GET movie_index/movie/_search
{
    "query":{
        "match_all":{

        }
    },
    "_source":[
        "name",
        "doubanScore"
    ]
}

# 高亮
GET movie_index/movie/_search
{
    "query":{
        "match":{
            "name":"red sea"
        }
    },
    "highlight":{
        "fields":{
            "name":{

            }
        }
    }
}

# 聚合
# 需求 1:取出每个演员共参演了多少部电影
GET movie_index/movie/_search
{
    "aggs":{
        "myAGG":{
            "terms":{
                "field":"actorList.name.keyword"
            }
        }
    }
}

# 需求 2:每个演员参演电影的平均分是多少,并按评分排序
GET movie_index/movie/_search
{
    "aggs":{
        "groupby_actor_id":{
            "terms":{
                "field":"actorList.name.keyword",
                "order":{
                    "avg_score":"desc"
                }
            },
            "aggs":{
                "avg_score":{
                    "avg":{
                        "field":"doubanScore"
                    }
                }
            }
        }
    }
}

# 查看英文单词默认分词情况
GET _analyze
{
    "text":"hello world"
}
# 按照空格对单词进行切分


# 按照每个汉字进行切分
GET _analyze
{
    "text":"小米手机"
}
# ik分词器
GET _analyze
{
    "analyzer":"ik_smart",
    "text":"小米手机"
}


# ik_max_word 分词方式
GET movie_index/_analyze
{
    "analyzer":"ik_max_word",
    "text":"我是中国人"
}
这篇关于ElasticSearch的REST接口操作(一)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!