首页 技术 正文
技术 2022年11月15日
0 收藏 806 点赞 3,609 浏览 6511 个字

ElasticSearch入门2: 基本用法

ElasticSearch入门2: 基本用法

ElasticSearch入门2: 基本用法

基本用法:

ElasticSearch入门2: 基本用法

 一、索引创建

ElasticSearch入门2: 基本用法

(启动集群和索引请看上一篇文章:http://www.cnblogs.com/liuxiaoming123/p/8081883.html)

1.打开浏览器,输入请求:http://localhost:9100

ElasticSearch入门2: 基本用法

2.点击后搜索book

ElasticSearch入门2: 基本用法

3.点击OK 显示创建成功

ElasticSearch入门2: 基本用法

ElasticSearch入门2: 基本用法

4.在概览中查看

ElasticSearch入门2: 基本用法

5.点击索引信息,查看结构化和非结构化索引信息

ElasticSearch入门2: 基本用法

ElasticSearch入门2: 基本用法

ElasticSearch入门2: 基本用法

1.点击 复合查询

  1.1 加入:book/novel/_mappers

  1.2加入:

{
"novel": {
"properties": {
"title": {
"type": "text"
}
}
}
}

1.3 勾选易读

  1.4点击 验证JSON

  1.5点击 提交请求

ElasticSearch入门2: 基本用法

2.点击概览,刷新页面

2.1 点击索引信息

ElasticSearch入门2: 基本用法

2.2 mappings 中 不为空则 表示 结构化索引创建成功

ElasticSearch入门2: 基本用法

注:上面是结构化索引的创建在head插件中实现的,其中json编写较为繁琐,下面采用postman方式进行创建

1.   在postman中编写json字符串(采取put提交方式提交)

2.   请求路径:127.0.0.1:9200/people

3.  json字符串

{
"settings":{
"number_of_shards":3,
"number_of_replicas":1
},
"mappings":{
"man":{
"properties":{
"name":{
"type":"text"
},
"country":{
"type":"keyword"
},
"age":{
"type":"integer"
},
"date":{
"type":"date",
"format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
}
}

4.json字符串的各字段介绍:

settings: 设置

  number_of_shards: 分片数

  number_of_replicas:备份数

  mappings: 索引的映射

  man: 映射名称,此处的映射名称只能有一个不能出现多个映射(这是一个设计失误,后面的版本将不再支持。官方给出解释是:https://www.elastic.co/guide/en/elasticsearch/reference/6.0/removal-of-types.html )

  properties:属性的集合 :(它下面的为各个属性,都是”属性名”:{“(类型)type”:”对应的类型”})注:其中Data属性有format设置日期格式

5.编写完毕,postman发送请求

ElasticSearch入门2: 基本用法

6.浏览器刷新请求:http://localhost:9100

ElasticSearch入门2: 基本用法

7.点击 索引信息

ElasticSearch入门2: 基本用法

8.查看结构化索引:

ElasticSearch入门2: 基本用法

{
"state": "open",
"settings": {
"index": {
"creation_date": "1513925050386",
"number_of_shards": "3",
"number_of_replicas": "1",
"uuid": "vGaF5hq4Te21atouyphn_Q",
"version": {
"created": "6010199"
},
"provided_name": "people"
}
},
"mappings": {
"man": {
"properties": {
"date": {
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis",
"type": "date"
},
"country": {
"type": "keyword"
},
"name": {
"type": "text"
},
"age": {
"type": "integer"
}
}
}
},
"aliases": [],
"primary_terms": {
"0": 1,
"1": 1,
"2": 1
},
"in_sync_allocations": {
"0": [
"DkW46MGNQi-fpBkd9Odpbw",
"EzcKH6oSSOCWMPIrwZVu0Q"
],
"1": [
"YbF3JOriS6iYJ-57Dc2eNA",
"h1Zkz1kaS0iAkoA_XekMIQ"
],
"2": [
"0AtuJLLJQEe_0HeF6CMoRw",
"oQukwljIS4K7gZ2ZXKJc5g"
]
}
}

 

二、插入

ElasticSearch入门2: 基本用法

ElasticSearch入门2: 基本用法

1.在postman中输入请求:127.0.0.1:9200/people/man/1 (PUT请求)

2.插入json 数据:

{
"name":"晓明",
"country":"china",
"age":26,
"date":"1992-08-08"
}

注:people 是索引, man是类型, 1是文档ID

3.点击send发送

ElasticSearch入门2: 基本用法

4.查看浏览器 http://localhost:9100

未刷新之前:

ElasticSearch入门2: 基本用法

刷新之后

ElasticSearch入门2: 基本用法

5.点击数据浏览查看所插入的数据:此时ID是手动指定的ID

ElasticSearch入门2: 基本用法

ElasticSearch入门2: 基本用法

1.浏览器输入请求:127.0.0.1:9200/people/man/ (post请求) 点击send发送

json字符串:

{
"name":"Auto晓明",
"country":"Autochina",
"age":26,
"date":"1992-08-08"
}

ElasticSearch入门2: 基本用法

2.查看浏览器 :http://localhost:9100

ElasticSearch入门2: 基本用法

3.点击数据浏览:此时新生成的为自动生成的ID

ElasticSearch入门2: 基本用法

三、修改

ElasticSearch入门2: 基本用法

ElasticSearch入门2: 基本用法

1.postman提交请求:127.0.0.1:9200/people/man/1/_update

2.请求json串为:

{
"doc":{
"name":"修改晓明"
}
}

ElasticSearch入门2: 基本用法

4.查看浏览器:http://localhost:9100

ElasticSearch入门2: 基本用法

ElasticSearch入门2: 基本用法

第一种方式:

1. postman输入请求:127.0.0.1:9200/people/man/1/_update(post请求)

2.json字符串:

{
    "script":{
        "lang":"painless",
        "inline":"ctx._source.age += 10"
    }
}

注:script 是脚本标识

  lang 是脚本语言

  painless 是内置脚本语言

  inline 是指定脚本内容

  ctx 是脚本上下文

  _source 为当前文档

ElasticSearch入门2: 基本用法

3.浏览器查看:http://localhost:9100

ElasticSearch入门2: 基本用法

第二种方式:

1. postman输入请求:127.0.0.1:9200/people/man/1/_update(post请求)

2.json字符串:

{
"script":{
"lang":"painless",
"inline":"ctx._source.age = params.age",
"params":{
"age":100
}
}
}

ElasticSearch入门2: 基本用法

3.查看浏览器:http://localhost:9100

ElasticSearch入门2: 基本用法

、删除

ElasticSearch入门2: 基本用法

ElasticSearch入门2: 基本用法

1.postman输入请求: 127.0.0.1:9200/people/man/1 (DELETE方式)

ElasticSearch入门2: 基本用法

2.查看浏览器:http://localhost:9200/ :ID等于1的已经成功删除

ElasticSearch入门2: 基本用法

ElasticSearch入门2: 基本用法

第一种:通过head插件直接删除索引

1.查看浏览器 :http://localhost:9100 删除book索引

ElasticSearch入门2: 基本用法

2.输入 :删除 点击 “好”ElasticSearch入门2: 基本用法

3.显示删除成功

ElasticSearch入门2: 基本用法

4.点击关闭

ElasticSearch入门2: 基本用法

第二种:通过postman删除索引 删除people

1.postman输入请求:127.0.0.1:9200/people(DELETE方式) 点击send发送请求

ElasticSearch入门2: 基本用法

2.查看浏览器:http://localhost:9100

ElasticSearch入门2: 基本用法

、查询

ElasticSearch入门2: 基本用法

1.准备:

   1.1 post创建机构化索引:

   1.1.1 请求:127.0.0.1:9200/people (PUT方式)

   1.1.2 json字符串:

{
"settings":{
"number_of_shards":3,
"number_of_replicas":1
},
"novel": {
"properties": {
"title": {
"type": "text"
},
"author": {
"type": "keyword"
},
"tittle": {
"type": "text"
},
"publish_date": {
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis",
"type": "date"
}
}
}
}

ElasticSearch入门2: 基本用法

1.2 postman插入数据:

  1.2.1 请求:127.0.0.1:9200/book/novel/1 (post方式)

  1.2.2 json 数据

{
"name":"晓明1",
"country":"china1",
"age":26,
"date":"1992-08-08"
}

ElasticSearch入门2: 基本用法

1.postman请求:127.0.0.1:9200/book/novel/1(get方式)

ElasticSearch入门2: 基本用法

ElasticSearch入门2: 基本用法

1.postman请求:127.0.0.1:9200/book/_search(post请求)

2.请求json串:

2.1 全部查询

{
"query":{
"match_all":{}
},
"from":1,
"size":1
}

2.2 按关键字查询(含有按指定字段排序)

{
"query":{
"match":{
"name":"晓明"
}
},
"sort":[
{"_id":"desc"}
]
}

3.解释请求json串:

match_all 是全部查询

query 是查询关键字

from 是从哪里查

size 是显示多少条数据

sort 是排序设置

ElasticSearch入门2: 基本用法

ElasticSearch入门2: 基本用法

ElasticSearch入门2: 基本用法(多组聚合)

1.postman请求:127.0.0.1:9200/book/_search(post请求)

2.请求json字符串:

{
   "aggs":{
           "group_by_name":{
               "terms":{
                   "field":"name"
               }
           },
           "group_by_country":{
               "terms":{
                   "field":"country"
               }
           },
           "group_by_date":{
               "terms":{
                   "field":"date"
               }
           }
           
   }
}

3.解析请求json字符串:

aggs:聚合查询关键字

group_by_word_count :聚合条件的名字(名字可自定义)

terms 关键词

field 是指定字段

4.点击send发送得到如下查询信息:

{
"took": 10,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 1,
"hits": [
{
"_index": "book",
"_type": "novel",
"_id": "5",
"_score": 1,
"_source": {
"name": "晓明",
"country": "china",
"age": 26,
"date": "1992-08-08"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "8",
"_score": 1,
"_source": {
"name": "晓明",
"country": "china",
"age": 26,
"date": "1992-08-08"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "9",
"_score": 1,
"_source": {
"name": "晓明9",
"country": "china9",
"age": 26,
"date": "1992-08-08"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "1",
"_score": 1,
"_source": {
"name": "晓明1",
"country": "china1",
"age": 26,
"date": "1992-08-08"
}
}
]
},
"aggregations": {
"group_by_date": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 713232000000,
"key_as_string": "1992-08-08T00:00:00.000Z",
"doc_count": 4
}
]
},
"group_by_country": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "china",
"doc_count": 2
},
{
"key": "china1",
"doc_count": 1
},
{
"key": "china9",
"doc_count": 1
}
]
},
"group_by_name": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "明",
"doc_count": 4
},
{
"key": "晓",
"doc_count": 4
},
{
"key": "1",
"doc_count": 1
},
{
"key": "9",
"doc_count": 1
}
]
}
}
}

注: 若聚合查询报错 请参考:http://www.cnblogs.com/liuxiaoming123/p/8117786.html

PUT         127.0.0.1:9200/book/_mapping/novel/ {
"properties": {
"name": {
"type": "text",
"fielddata": true
},
"country": {
"type": "text",
"fielddata": true
} }
}

ElasticSearch入门2: 基本用法

注:聚合查询其他用法:

计算年龄的统计  总条数、最大值、最小值、平均值、总和   若:stats改成min则只显示最小值请求URL:POST  127.0.0.1:9200/book/_search请求json:{
"aggs":{
"group_by_age":{
"stats":{
"field":"age"
}
} }
}返回结果:{
"took": 33,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 1,
"hits": [
{
"_index": "book",
"_type": "novel",
"_id": "5",
"_score": 1,
"_source": {
"name": "晓明",
"country": "china",
"age": 26,
"date": "1992-08-08"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "8",
"_score": 1,
"_source": {
"name": "晓明",
"country": "china",
"age": 26,
"date": "1992-08-08"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "9",
"_score": 1,
"_source": {
"name": "晓明9",
"country": "china9",
"age": 26,
"date": "1992-08-08"
}
},
{
"_index": "book",
"_type": "novel",
"_id": "1",
"_score": 1,
"_source": {
"name": "晓明1",
"country": "china1",
"age": 26,
"date": "1992-08-08"
}
}
]
},
"aggregations": {
"group_by_age": {
"count": 4,
"min": 26,
"max": 26,
"avg": 26,
"sum": 104
}
}
}

ElasticSearch入门2: 基本用法

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849