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
}

区别:
 
禁用此行为后,不更改任何内容的更新也会返回updated并且文档版本号加1;
 
不禁用此行为,不更改任何内容的更新会返回noop并且文档版本号不变。
 
8、Upserts更新
如果文档不存在,则upsert元素的内容将作为新文档插入。如果文档存在,那么script将执行:

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": "我是中国人"
}

最新文章

  1. WebService -- Java 实现之 CXF ( 使用CXF工具生成client 程序)
  2. Mysql 5.7 Linux安装详细步骤
  3. 连接WCF报EntityFramework.SqlServer 错误的解决方法
  4. 腾讯云Centos下Nginx反向代理Apache+Tomcat
  5. 由一个订单推送想到了ObservableCollection的神奇用法
  6. windows 注册表编程
  7. CodeForce---Educational Codeforces Round 3 D. Gadgets for dollars and pounds 正题
  8. zabbix数据存储
  9. 基于jsp+servlet图书管理系统之后台用户信息查询操作
  10. hdu Counting Sheepsuanga
  11. Eclipse在点击运行后不能自动保存的解决
  12. Java IO流之转换流
  13. 对 List 、Set、Map 的理解
  14. cookie记住浏览位置
  15. spring中集成hibernate
  16. iPhone / iPad L2TP Client Setup
  17. 【TCP/IP详解 卷一:协议】第一章概论 学习笔记
  18. 如何成为一名合格的Android工程师?
  19. 持续交付的Mesos与Docker导入篇
  20. 哈尔滨理工大学软件与微电子学院第八届程序设计竞赛同步赛(高年级) Solution

热门文章

  1. C#如何给WinForm的button等控件添加快捷键
  2. .net core 3.1 使用nlog记录日志 NLog.Web.AspNetCore
  3. Unsafe类初探
  4. POJ1321棋盘问题(DFS)
  5. 51Nod栈
  6. 【Mac】屏蔽系统升级更新
  7. GO 使用Webhook 实现github 自动化部署
  8. Python面向对象的特征跟私有属性
  9. C#中方法的静态和非静态
  10. Java中的集合(十二) 实现Map接口的WeakHashMap