ES索引操作
1、创建测试索引
PUT /test_index
{
"mappings": {
"test_type":{
"properties": {
"code":{
"type": "keyword"
},
"name":{
"type": "text",
"analyzer": "ik"
},
"count":{
"type": "integer"
}
}
}
},
"settings": {
"number_of_shards": 5,
"number_of_replicas": 1,
"max_result_window":10000,
"analysis": {
"analyzer": {
"ik":{
"tokenizer":"ik_max_word",
"stopwords":["的","是"]
}
}
}
}
}
2、插入数据
PUT test_index/test_type/1 #4代表索引记录的唯一标识,类似数据库表中主键
{
"code":"001",
"name":"万科企业股份有限公司"
}
PUT test_index/test_type/2
{
"code":"002",
"name":"万达集团股份有限公司"
}
PUT test_index/test_type/3
{
"code":"003",
"name":"阿里巴巴(中国)有限公司"
}
PUT test_index/test_type/4
{
"code":"004",
"name":"中国平安"
}
3、删除数据
DELETE test_index/test_type/4 #4代表索引记录的唯一标识,类似数据库表中主键
4、更新数据
回复删除的4数据
PUT test_index/test_type/4
{
"code":"004",
"name":"中国平安"
}
①覆盖更新
PUT test_index/test_type/4
{
"name":"中国平安"
}
②更新字段值
POST test_index/test_type/4/_update
{
"doc": {
"name":"中国平安保险(集团)股份有限公司"
}
}
5、查询数据
在kibana中默认只显示10条记录
①查询所有
方式1:
GET test_index/test_type/_search
方式2:
GET test_index/test_type/_search
{
"query":{
"match_all": {}
}
}
②根据指定分词字段查询
name:为索引中的中文分词字段
GET test_index/test_type/_search
{
"query":{
"match": {
"name": "中国"
}
}
}
③、根据指定不分词字段查询
GET test_index/test_type/_search
{
"query":{
"term": {
"code": "004"
}
}
}
④、根据文档id查询
查询描述:pXrY0GsBN9ZpEwHZ14it:文档id
方式1:GET 索引/类型/文档id
GET test_index/test_type/
方式2:_id文档唯一的id
GET test_index/test_type/_search
{
"query":{
"match": {
"_id": ""
}
}
}
⑤多条件查询
多条件and关系:
#逻辑:and关系
GET test_index/test_type/_search
{
"query": {
"bool": {
"must": [
{"match": { "question": "淘宝"}},
{"match": { "question": "软件"}}
]
}
}
}
GET test_index/test_type/_search
{
"query":{
"bool": {
"must": [
{
"match": {
"name":"中国平安保险(集团)股份有限公司"
}
},
{
"term": {
"code": "004"
}
}
]
}
}
}
多条件or关系:
GET test_index/test_type/_search
{
"query":{
"bool": {
"should": [
{
"match": {
"name":"中国平安保险(集团)股份有限公司"
}
},
{
"term": {
"code": "004"
}
}
]
}
}
}
多条件and和or关系混合使用:
GET test_index/test_type/_search
{
"query":{
"bool": {
"should": [
{
"match": {
"name":"中国平安保险(集团)股份有限公司"
}
},
{
"term": {
"code": "004"
}
}
],
"must": [
{
"match": {
"count": 2
}
}
]
}
}
}
自定义排序:
GET test_index/test_type/_search
{
"query": {
"bool": {
"must" : [
{
"match" : {
"name" : {
"query" : "中国平安"
}
}
}
],
"adjust_pure_negative": true,
"boost": 1.0
}
},
"_source": {
"includes": ["code", "name"],
"excludes": []
},
"sort": [{
"count": {
"order": "desc"
}
}]
}
⑥指定查询条数
GET test_index/test_type/_search
{
"query": {
"match": {
"question": "淘宝"
}
},
"from": 1, #指定位移
"size": 5 #指定查询条数
}
⑦指定查询返回的字段
GET test_index/test_type/_search
{
"_source": ["question","nlp"], #返回字段数组
"query": {
"match": {
"question": "淘宝"
}
}
}
⑧控制加载的字段
GET test_index/test_type/_search
{
"_source": {"includes": ["question","nlp"],"excludes": ["isSatisfied"]},
"query": {
"match": {
"question": "淘宝"
}
}
}
⑨通配符
GET test_index/test_type/_search
{
"_source": {"includes": ["quest*"],"excludes": ["*Date"]},
"query": {
"match": {
"question": "淘宝"
}
}
}
排序
GET test_index/test_type/_search
{
"sort": [
{
"askTimes": {
"order": "desc"
}
}
],
"query": {
"match_all": {}
}
}
前缀匹配
GET test_index/test_type/_search
{
"query": {
"match_phrase_prefix": {
"question": "万科"
}
}
}
返回查询
GET test_index/test_type/_search
{
"query": {
"range": {
"askTimes": {
"gte": 10, #
"lte": 20
}
}
}
}
wildcard查询
*代表0个或多个字符
?代表任意一个字符
GET test_index/test_type/_search
{
"query": {
"wildcard": {
"question": "万科*"
}
}
}
模糊查询
GET test_index/test_type/_search
{
"query": {
"fuzzy": {"question": "万科"}
}
}
高亮搜索结果
注:在Kibana中没有看到效果
GET test_index/test_type/_search
{
"query": {
"match": {
"question": "淘宝"
}
},
"highlight": {
"fields": {"question":{}}
}
}
过滤查询
GET test_index/test_type/_search
{
"post_filter": {
"term": {
"askTimes": 10
}
}
}
过滤非空
GET test_index/test_type/_search
{
"query": {
"bool": {
"filter": {
"exists": {
"field": "nlp"
}
}
}
}
}
聚合查询
sum,min,max,avg,cardinality:求基数,terms:分组
GET /test_index/test_type/_search
{
"size": 0,
"aggs": {
"askTimes_of_max": { #自定义名称
"max": { #最大值
"field": "askTimes"
}
}
}
}
符合查询:待整理
7、检查noop更新
默认情况下,不更改任何内容的更新会返回“result”:“noop”;
可以通过设置“detect_noop”来禁用此行为:false
PUT test_index/test_type/4
{
"code":"004",
"name":"中国平安保险(集团)股份有限公司",
"count":2,
"tags":["aa","bb"]
}
POST test_index/test_type/4/_update
{
"doc": {
"name":"中国平安保险(集团)股份有限公司"
},
"detect_noop":false
}
POST test_index/test_type/5
{
"script" : {
"source": "ctx._source.count += params.count",
"lang": "painless",
"params" : {
"count" : 4
}
},
"upsert" : {
"count" : 1
}
}
如果无论文档是否存在您都希望脚本运行,即脚本处理初始化文档而不是upsert元素,设置scripted_upsert为true:
POST test_index/test_type/5
{
"scripted_upsert":true,
"script" : {
"source": "ctx._source.count += params.count",
"lang": "painless",
"params" : {
"count" : 4
}
},
"upsert" : {
"count" : 1
}
}
同scripted_upsert,如果无论文档是否存在都希望脚本运行,即脚本处理初始化文档而不是upsert元素,设置doc_as_upsert为true(文档5不存在):
POST test_index/test_type/5/_update
{
"doc":{
"count":12
},
"doc_as_upsert":true,
"upsert":{
"count":16
}
}
查询结果:GET test_index/test_type/5
{
"_index": "test_index",
"_type": "test_type",
"_id": "5",
"_version": 11,
"found": true,
"_source": {
"count": 12
}
}
9、其他
①、统计索引文档数量
描述:GET 索引/类型/_count
GET test_index/test_type/_count
②检测分词效果
POST test_index/_analyze
{
"analyzer": "ik_max_word",
"text": "我是中国人"
}
ES索引操作的更多相关文章
- Elasticsearch必知必会的干货知识二:ES索引操作技巧
该系列上一篇文章<Elasticsearch必知必会的干货知识一:ES索引文档的CRUD> 讲了如何进行index的增删改查,本篇则侧重讲解说明如何对index进行创建.更改.迁移.查询配 ...
- 008-elasticsearch5.4.3【二】ES使用、ES客户端、索引操作【增加、删除】、文档操作【crud】
一.ES使用,以及客户端 1.pom引用 <dependency> <groupId>org.elasticsearch.client</groupId> < ...
- ES入门三部曲:索引操作,映射操作,文档操作
ES入门三部曲:索引操作,映射操作,文档操作 一.索引操作 1.创建索引库 #语法 PUT /索引名称 { "settings": { "属性名": " ...
- ElasticSearch+Kibana 索引操作
ElasticSearch+Kibana 索引操作 一 前言 ElasticiSearch 简介 ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文搜索引 ...
- 数据源、数据集、同步任务、数据仓库、元数据、数据目录、主题、来源系统、标签、增量识别字段、修改同步、ES索引、HBase列族、元数据同步、
数据源.数据集.同步任务.数据仓库.元数据.数据目录.主题.来源系统.标签. 增量识别字段.修改同步.ES索引.HBase列族.元数据同步.DS.ODS.DW.DM.zk集群地址 == 数据源 数据源 ...
- elasticsearch的索引操作和文档操作总结
参考文档:https://es.xiaoleilu.com/010_Intro/00_README.html 一.索引操作 1.查看当前节点的所有的index 查看当前节点的所有的index [roo ...
- Kibana自动关联ES索引
原因: Kibana中关联ES索引需要手动操作,如果ES中索引较多(如每天生成),则工作量会比较大. 方法: 考虑使用Linux的cron定时器自动关联ES索引,原理是调用Kibana API接口自动 ...
- es常用操作
1.查看所有索引 _cat/indices?v 2.删除索引 DELETE my_index 3.查询缓存 curl /my_index/_search?request_cache=true' -d' ...
- Elasticsearch ES索引
ES是一个基于RESTful web接口并且构建在Apache Lucene之上的开源分布式搜索引擎. 同时ES还是一个分布式文档数据库,其中每个字段均可被索引,而且每个字段的数据均可被搜索,能够横向 ...
随机推荐
- 到手的DEM不会用?教你6个常用强大功能
一.概述 DEM是数字高程模型(Digital Elevation Model)的简称,接触GIS,规划,设计类的多多少少会接触到DEM,可是这个直接查看黑溜溜一片DEM到底可以用来做什么呢? 二.背 ...
- vue与其他框架对比
https://cn.vuejs.org/v2/guide/comparison.html 1. vue 框架的特点? MVVM框架模式 轻量级,灵活,容易上手 数据驱动 组件化(单文件组件) 插件化 ...
- lonic常用组件之五------按钮
一.Ionic常用组件之五------按钮 <ion-button color="主题色" size="small/large" expand=& ...
- Django路由之url分组(命名)匹配
分组(命名)匹配 urls.py路由配置文件中: urlspatterns中想捕获正则表达式匹配的结果用来出传递给views.py视图函数文件使用,需要用到分组匹配,或者使用第三个参数python字典 ...
- LaunchScreen作为启动图设置,修改无效的解决方案
原有的推流APP用launchScreen做的启动图,现在要修改一张,发现修改无效. 当前测试的方法有 1,重启Xcode 卸载app 清楚xcode缓存 2,修改launchScreen.stor ...
- VueRouter小手册
目录 一. 了解router 二. 工作流程 三. 简单的Demo 四. 理解template和route的组合 五. Vue-Router-GoBack记录返回 六. Router-Link 七. ...
- Poj2965 冰箱的开关
#include<iostream> using namespace std; int flag; int step; ][]; ] = { }; ] = { }; void turn(i ...
- CSS3新子代选择器
:nth-child(n) 选择器匹配属于其父元素的第 N 个子元素,不论元素的类型,除了<h>标签. n 可以是数字.关键词或公式 例子一 <!DOCTYPE html> & ...
- Altera的Cyclone系列器件命名规则
Altera的Cyclone系列器件命名规则如下 器件系列 + 器件类型(是否含有高速串行收发器) + LE逻辑单元数量 + 封装类型 + 高速串行收发器的数量(没有则不写) + 引脚数目 + 器件 ...
- python中几个双下划线用法的含义
_ _ init() _ _(self[,...]) 我们有时在类定义写__init()__方法,但是有时又没有.__init()__方法相当于其他面向对象的编程语言中的构造方法,也就是类在实例化成对 ...