ES数据结构
ES基本命令
索引相关的命令
查看es中有哪些索引 GET /_cat/indices?v
增加一个索引 索引名:movie_index PUT /movie_index
删除一个索引 DELETE /movie_index
文档相关的命令
新增文档 格式 PUT /index/type/id
PUT /movie_index/movie/2 { “id”:2, “name”:“operation meigong river”, “doubanScore”:8.0, “actorList”:[ {“id”:3,“name”:“zhang han yu”} ] }
直接用id查找 GET movie_index/movie/1
修改—整体替换 和新增没有区别 PUT /movie_index/movie/3 { “doubanScore”: “7.0” }
修改—某个字段 POST movie_index/movie/3/_update { “doc”: { “doubanScore”:“7.0” } }
删除一个document DELETE movie_index/movie/3
搜索type全部数据 GET movie_index/movie/_search
查询相关的命令
按条件查询(全部) GET movie_index/movie/_search { “query”:{ “match_all”: {} } }
按分词查询 GET movie_index/movie/_search { “query”:{ “match”: {“name”:“red”} } }
按分词子属性查询 GET movie_index/movie/_search { “query”:{ “match”: {“actorList.name”:“fei”} } }
match phrase(词组查询) GET movie_index/movie/_search { “query”:{ “match_phrase”: {“name”:“operation red”} } }
关键字查询(对查询条件不进行分词) GET movie_index/movie/_search { “query”:{ “match”: {“name.keyword”: “operation red sea”} } }
fuzzy查询(模糊查询) 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”: { “filter”: [ { “term”: { “actorList.name.keyword”: “zhang han yu” } }], “must”: { “match”: { “name”: “river” } } } } }
过滤–按范围过滤 GET movie_index/movie/_search { “query”: { “bool”: { “filter”: { “range”: { “doubanScore”: {“gte”: 8} } } } } }
排序 GET movie_index/movie/_search { “query”:{ “match”: {“name”:“red sea”} } , “sort”: [ { “doubanScore”: { “order”: “desc” } } ] }
分页查询 GET movie_index/movie/_search { “query”: { “match_all”: {} }, “from”: 1, “size”: 1 }
指定查询的字段 GET movie_index/movie/_search { “query”: { “match_all”: {} }, “_source”: [“name”, “doubanScore”] }
高亮 GET movie_index/movie/_search { “query”:{ “match”: {“name”:“red sea”} }, “highlight”: { “fields”: {“name”:{} } }
}
自定义标签 加粗 GET movie_index/movie/_search { “query”: { “match”: { “name”: “red sea” } }, “highlight”: { “pre_tags”: [ “” ], “post_tags”: [ “” ], “fields”: { “name”: {} } } }
聚合 取出每个演员共参演了多少部电影 GET movie_index/movie/_search { “aggs”: { “groupby_actor”: { “terms”: { “field”: “actorList.name.keyword” } } } }
每个演员参演电影的平均分是多少,并按评分排序 GET movie_index/movie/_search { “aggs”: { “groupby_actor_id”: { “terms”: { “field”: “actorList.name.keyword” , “order”: { “avg_score”: “desc” } }, “aggs”: { “avg_score”:{ “avg”: { “field”: “doubanScore” } } } } } }
映射mapping
查看es的mapping结构 GET movie_index/_mapping
[https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-field-mapping.html]
|