ElasticSearch6.7 入门教程示例

相关依赖:

    <properties>
        <es.version>6.7.2</es.version>
        <httpcore.version>4.4.10</httpcore.version>
        <httpclient.version>4.5.6</httpclient.version>
    </properties>
<!-- elasticsearch -->
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-client</artifactId>
            <version>${es.version}</version>
        </dependency>
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>elasticsearch-rest-high-level-client</artifactId>
            <version>${es.version}</version>
        </dependency>
        <!-- http -->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>${httpcore.version}</version>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>${httpclient.version}</version>
        </dependency>

基于es6.7.2版本操作
(基于es6.7.2版本操作)[https://blog.csdn.net/qq_37873221/article/details/106986998]

一,索引操作

index相当于关系型数据库中database的概念
1,新增database索引,put

put: "http://localhost:9200/database"
body:{
  "settings":{
    "index":{
      "number_of_shards":2,
      "number_of_replicas":1
    }
  }
}

2,查询database索引,get

get: "http://localhost:9200/database"

3,删除database索引,delete

delete: "http://localhost:9200/database"

二,映射操作

mapping相当于关系型数据库中schema的概念

1,查询映射mapping

get: "http://localhost:9200/database/person/_mapping"

2,创建index和mapping
创建index1索引和student映射

put: "http://localhost:9200/index1"

body:
{
  "settings": {
    "index": {
      "number_of_shards": 2,
      "number_of_replicas": 1
    }
  },
  "mappings": {
    "student": {
      "properties": {
        "name": {
          "type": "text"
        },
        "age": {
          "type": "long"
        },
        "mail": {
          "type": "keyword"
        },
        "hobby": {
          "type": "text"
        }
      }
    }
  }
}

三,文档操作

1,新增和更新文档 post 或者 put
URL规则:POST http://172.16.55.185:9200/{索引}/{类型}/{id}
插入id为p101的person类型文档
插入时,自动生成person类型,id不指定,es自动生成默认id (32位长度)
文档数据是不为修改的,但是可以通过覆盖的方式进行更新

post: "http://localhost:9200/database/person/p101"
put: "http://localhost:9200/database/person/p101"

body:
{
  "id":101,
  "name":"zhangsan",
  "age":18
}

2,局部更新 post, put

post: "http://localhost:9200/database/person/p101"
put: "http://localhost:9200/database/person/p101"

body:
{
  "doc":{
    "name":"wangwu"
  }
}

3,查询文档 get
3.1查询person类型id为p101的文档

get: "http://localhost:9200/database/person/p101"

3.2查询person类型的所有文档

get: "http://localhost:9200/database/person/_search"

3.3查询person类型,age为30的文档

get: "http://localhost:9200/database/person/_search?q=age:30"

4,删除文档 delete
删除id为p101的文档

delete: "http://localhost:9200/database/person/p101"

四,DSL搜索

1,匹配查询 match,查询person类型,age为22的文档

post: http://localhost:9200/database/person/_search

body:
{
  "query":{
    "match":{
      "age":22
    }
  }
}

2,过滤查询 bool filter,查询person类型,age大于20而且名字为lisi的文档
_type 的名字可以是大写或小写,不能包含下划线或逗号。我们将使用 blog 做为类型名

post: http://localhost:9200/database/person/_search

body:
{
  "query":{
    "bool":{
      "filter":{
        "range":{
          "age":{
            "gt":20
          }
        }
      },
      "must":{
        "match":{
          "name": "lisi"
        }
      }
    }
  }
}

3,高亮查询,highlight

post: "http://localhost:9200/database/person/_search"

body:
{
    "query":{
        "bool":{
            "should":[
                {
                    "match":{
                        "name": "tianqi"
                    }
                }
            ]
        }
    },
    "highlight":{
        "fields":{
            "name":{}
        }
    }
}

4,聚合查询 aggregations
类似于mysql中的group by

{
  "aggs":{
    "all_interests":{
      "terms":{
        "field":"age"
      }
    }   
  }
}

五,进阶查询

1,查询响应
浏览器直接查询url后面添加pretty参数,使得返回的json更易查看。
在响应的数据中,如果我们不需要全部的字段,可以指定某些需要的字段进行返回。

get: "http://localhost:9200/database/person/p101?_source=name,age&pretty"

2,判断文档是否存在
状态码返回200或者404

HEAD: "http://localhost:9200/database/person/p101"

3,批量操作
3.1批量查询 post

post: "http://localhost:9200/database/person/_mget"
body:
{
  "ids":["p101","p102"]
}

3.2批量插入 post

{ action: { metadata }}
{ request body }
{ action: { metadata }}
{ request body }

post: "http://localhost:9200/database/person/_bulk"

body:
{"create":{"_index":"database","_type":"person","_id":2001}}
{"id":2001,"name":"name1","age": 20}
{"create":{"_index":"database","_type":"person","_id":2002}}
{"id":2002,"name":"liubei","age":31}
{"create":{"_index":"database","_type":"person","_id":2003}}
{"id":2003,"name":"guanyu","age":33}

3.3 批量删除 post

post: "http://localhost:9200/database/person/_bulk"

body:
{"delete":{"_index":"database","_type":"person","_id":2001}}
{"delete":{"_index":"database","_type":"person","_id":2002}}
{"delete":{"_index":"database","_type":"person","_id":2003}}

4,分页查询

get: "http://localhost:9200/database/person/_search?size=1&from=1"

六,结构化查询

1,term查询
主要用于精确匹配哪些值,比如数字,日期,布尔值或 not_analyzed 的字符串
(未经分析的文本数据类型)

post: "http://localhost:9200/database/person/_search"

body:
{
    "query": {
        "term": {
            "age": 22
        }
    }
}

2,terms查询
terms 跟 term 有点类似,但 terms 允许指定多个匹配条件。 如果某个字段指定了多个值,那么文档需要一起去做匹配

{
    "terms": {
        "tag": [ "search", "full_text", "nosql" ]
    }
}
post: "http://localhost:9200/database/person/_search"

body:
{
    "query": {
        "terms": {
            "age": [22,30]
        }
    }
}

3,range查询
过滤允许我们按照指定范围查找一批数据
gt :: 大于
gte :: 大于等于
lt :: 小于
lte :: 小于等于

post: "http://localhost:9200/database/person/_search"

body:
{
    "query": {
        "range": {
            "age": {
                "gte": 20,
                "lte": 30
            }
        }
    }
}

4,exists查询
exists 查询可以用于查找文档中是否包含指定字段或没有某个字段,类似于SQL语句中的 IS_NULL 条件

post: "http://localhost:9200/database/person/_search"

body:
{
    "query":{
        "exists": {
            "field": "age"
        }
    }
}

5,match查询
match 查询是一个标准查询,不管你需要全文本查询还是精确查询基本上都要用到它。

post: "http://localhost:9200/database/person/_search"

body:
{
    "query":{
        "match": {
            "age": "22"
        }
    }
}

6,bool查询 (条件合并逻辑)
bool 查询可以用来合并多个条件查询结果的布尔逻辑,它包含一下操作符:
must :: 多个查询条件的完全匹配,相当于 and 。
must_not :: 多个查询条件的相反匹配,相当于 not 。
should :: 至少有一个查询条件匹配, 相当于 or 。

post: "http://localhost:9200/database/person/_search"

body:
{
    "query":{
        "bool": {
            "must": { "term": { "name": "tianqi" }},
            "must_not": { "term": { "age": "99" }},
            "should": [
                { "term": { "starred": true }},
                { "term": { "unread": true }}
            ]
        }
    }
}

7,条件查询filter

前面讲过结构化查询,Elasticsearch也支持过滤查询,如term、range、match等。

post: "http://localhost:9200/database/person/_search"

body:
{
    "query":{
        "bool": {
            "filter": { 
                "term": { "age": 22 }
            }
        }
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,088评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,715评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,361评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,099评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 60,987评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,063评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,486评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,175评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,440评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,518评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,305评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,190评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,550评论 3 298
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,880评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,152评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,451评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,637评论 2 335

推荐阅读更多精彩内容