全文检索大体分两个过程,索引创建和搜索索引
9300端口为es集群间组件的通信端口,9200端口为浏览器访问的http协议Restful端口
- text:文本是可以分词的
- keyword:不能分词,必须完整匹配
- index:false,不能被索引查询
ES接入的几种方式:
1.node接入-node client
2.transport接入-transport client
3.http接入- rest client
1.创建(order_main)索引
#put请求
127.0.0.1:9200/order_main
2.查看(order_main)索引
#get 请求
127.0.0.1:9200/order_main
3.查看全部索引信息
#get请求查看所有索引信息
127.0.0.1:9200/_cat/indices?v
4.删除指定索引
#delete 方法
127.0.0.1:9200/order_main
5.创建文档(post)
#方式一
127.0.0.1:9200/order_main/_doc
#方式二 指定id
127.0.0.1:9200/order_main/_doc/1920
#方式三 指定id
127.0.0.1:9200/order_main/_create/2208
{
"order_id": "1657440571452",
"address": "北京市海淀区五道口金融大厦",
"name": "liufeifei",
"phone": "180",
"price": 4599
}
6.根据_id指定查询
127.0.0.1:9200/order_main/_doc/2208
7.查询所有
127.0.0.1:9200/order_main/_search
8.更新文档
#全量新增(先删除后创建)
127.0.0.1:9200/order_main/_doc/1920
{
"order_id": "1657440571498",
"address": "北京市海淀区五道口金融大厦",
"name": "wuzhaoyang2",
"phone": "111"
}
#局部更新
127.0.0.1:9200/order_main/_update/1920
{
"doc": {
"price": 4599
}
}
9.条件查询
#根据姓名查询
127.0.0.1:9200/order_main/_search?q=name:lixiuyang
#分页查询
127.0.0.1:9200/order_main/_search?from=1&size=10
#根据body请求体查询
127.0.0.1:9200/order_main/_search
{
"query": {
"match": {
"name": "lixiuyang"
}
}
}
#查询所有
{
"query": {
"match_all": {
}
}
}
#分页查询
{
"query": {
"match_all": {}
},
"from": 0,
"size": 1
}
#指定字段
{
"query": {
"match_all": {
}
},
"from":0,
"size":1,
"_source":["price"]
}
#根据价格排序
{
"query": {
"match_all": {
}
},
"from":0,
"size":10,
"_source":["price"],
"sort":{
"price":{
"order":"desc"
}
}
}
10.多条件查询
#多个条件查询(and)
127.0.0.1:9200/order_main/_search
{
"query": {
"bool": {
"must":[ {
"match": {
"name": "lixiuyang"
}
},
{
"match": {
"phone": "110"
}
}]
}
}
}
#多个条件查询(or)
127.0.0.1:9200/order_main/_search
{
"query": {
"bool": {
"should":[ {
"match": {
"name": "lixiuyang"
}
},
{
"match": {
"name": "liufeifei"
}
}]
}
}
}
#多个条件查询+过滤
127.0.0.1:9200/product/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"name": "lixiuyang"
}
},
{
"match": {
"name": "liufeifei"
}
}
],
"filter": {
"range": {
"price": {
"gt": 2000
}
}
}
}
}
}
11.完全匹配查询(不分词)没有生效
127.0.0.1:9200/product/_search
{
"query": {
"match_phrase": {
"title":"华为mate40e"
}
}
}
12.高亮显示
127.0.0.1:9200/product/_search
{
"query": {
"match_phrase": {
"title":"华为mate40e"
}
},
"highlight":{
"fields":{
"title":{}
}
}
}
13.聚合操作
127.0.0.1:9200/product/_search
{
"aggs":{ //聚合操作
"price_group":{ //任意取名
"terms":{ //分组
"field":"price" //分组字段
}
}
},
"size":0
}
14.查询mapping信息
127.0.0.1:9200/people/_mapping
15.维护mapping信息
127.0.0.1:9200/people/_mapping
{
"properties": {
"name": {
"type": "text",
"index": true
},
"sex": {
"type": "keyword",
"index": true
},
"tel": {
"type": "keyword",
"index": false
}
}
}
- 不需要被索引(查询)的字段就设置 index:false
根据_id查询
GET /eshop_order/order_product/z0tYmHkByZh-k7RqRC3r
搜索全部数据 响应:(默认返回10条数据)
GET /eshop_order/order_product/_search
关键字搜素数据
GET /eshop_order/order_product/_search?q=customer:速递
DSL搜索
Elasticsearch提供丰富且灵活的查询语言叫做DSL查询(Query DSL),它允许你构建更加复杂、强大的查询。
DSL(Domain Specific Language特定领域语言)以JSON请求体的形式出现。
GET /eshop_order/order_product/_search
{
"query":{
"match": {
"customer": "速递"
}
}
}
GET /eshop_order/order_product/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"customer": "的"
}
}
],
"filter": [
{
"range": {
"id": {
"gte": 10,
"lt": 1012
}
}
}
]
}
}
}
全文搜索(类似模糊搜索)
GET /eshop_order/order_product/_search
{
"query":{
"match": {
"customer": "王者 王富"
}
}
}
高亮显示
GET /eshop_order/order_product/_search
{
"query": {
"match": {
"customer": "王者 王富"
}
},
"highlight": {
"fields": {
"customer": {}
}
}
}
聚合
在Elasticsearch中,支持聚合操作,类似SQL中的group by操作。
GET /eshop_order/order_product/_search
{
"aggs": {
"group_id": {
"terms": {
"field": "id"
}
}
}
}
可以在查询url后面添加pretty参数,使得返回的json更易查看。
GET /eshop_order/order_product/z0tYmHkByZh-k7RqRC3r?pretty
在响应的数据中,如果我们不需要全部的字段,可以指定某些需要的字段进行返回。
GET /eshop_order/order_product/z0tYmHkByZh-k7RqRC3r?_source=id,customer
如果我们只需要判断文档是否存在,而不是查询文档内容,那么可以这样:
HEAD /eshop_order/order_product/z0tYmHkByZh-k7RqRC3r
批量操作
有些情况下可以通过批量操作以减少网络请求。如:批量查询、批量插入数据。
GET /eshop_order/order_product/_mget
{
"ids":["1001","1002"]
}
批量新增
POST /eshop_order/order_product/_bulk
{"create":{"_index":"eshop_order","_type":"order_product","_id":2001}}
{"id":2001,"customer":"name1","phone": 20,"orderCode": "202105231008525632"}
{"create":{"_index":"eshop_order","_type":"order_product","_id":2002}}
{"id":2002,"customer":"name2","phone": 20,"orderCode": "202105231008525632"}
{"create":{"_index":"eshop_order","_type":"order_product","_id":2003}}
{"id":2003,"customer":"name3","phone": 20,"orderCode": "202105231008525632"}
创建明确类型的索引:
PUT /order
{
"settings": {
"index": {
"number_of_shards": "2",
"number_of_replicas": "0"
}
},
"mappings": {
"properties": {
"name": {
"type": "text"
},
"age": {
"type": "integer"
},
"mail": {
"type": "keyword"
},
"hobby": {
"type": "text"
}
}
}
}
查看映射:
GET /order/_mapping
分页查询
GET /order/_search
{
"size": 2,
"from": 2,#跳过开始的结果数,默认0
"query": {
"match": {
"hobby": "乒乓球 音乐"
}
}
}
cardinality(相当于对工作岗位进行分组并count)
GET employee/_search
{
"aggs": {
"job_category_count": {
"cardinality": {
"field": "job"
}
}
}
}
kibana安装
1.修改 config/kibana.yml 文件
# 默认端口
server.port: 5601
# ES 服务器的地址
elasticsearch.hosts: ["http://localhost:9200"]
# 索引名
kibana.index: ".kibana"
# 支持中文
i18n.locale: "zh-CN"
2. http://localhost:5601 查看效果