相关依赖:
<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 }
}
}
}
}