ElasticSearch索引操作总结归纳 便于后期使用查找
Index索引操作
文章目录
一、使用kibana工具操作索引
1.索引类型:
- text/keyword、
- 整数类型、
- 浮点类型、
- 日期类型、
- boolean 、
- array类型(其实就是josn对象中数组写法”[{},{}]”)、
- Object类型(其实就是一个json对象)
2.创建索引和映射对象
PUT /索引库名
{
"mappings": {
"类型名称":{
//类似于数据库中的不同表字段名:任意填写 ,相当于表的名称。
"properties": {
"字段名": {
"type": "类型",
"index": true,
"store": true,
"analyzer": "分词器"//比如ik_max_word
}
}
}
}
先分析,那些需要建立索引放入到索引库中,比如
#创建索引结构
PUT sku {
"mappings": {
"doc":{
"properties":{
"name":{
"type":"text",
"analyzer":"ik_smart"
},
"price":{
"type":"integer"
},
"image":{
"type":"text",
"index":false
},
"createTime":{
"type":"date"
},
"spuId":{
"type":"text"
},
"categoryName":{
"type":"keyword"
},
"brandName":{
"type":"keyword"
},
"spec":{
"type":"object"
},
"saleNum":{
"type":"integer"
},
"commentNum":{
"type":"integer"
}
}
}
}
}
3.添加文档
分为指定id,和不指定id。如果不指定id,系统会自动生成。
POST 索引库名/ 类型名 //POST 索引库名/ 类型名/id值
{
"key":"value"
}
### 插入文档 手动指定指定id写法:POST sku/doc/3
POST sku/doc
{
"name":"小米手机",
"price":200000,
"spuId":101,
"createTime":"2019-05-01",
"brandName":"小米",
"categerName":"手机",
"saleNum":1203,
"commentNum":2356,
"spec":{
"网络制式":"移动4G",
"屏幕尺寸":"4.5"
}
}
在java中如何体现添加添加spec??
Map skuMap = new HashMap();
Map specMap = new HashMap();
specMap.put("网络制式","移动4G");
specMap.put("屏幕尺寸","4.5");
skuMap.put("spec",specMap);
4.查询索引
查询的基本格式:
Get 索引名称/_search
{
"query":{
"查询类型":{
"查询条件":"查询条件值"
}
}
}
query
代表一个查询对象,里面可以有不同的查询属性
- 查询类型
match_all
、match
、term(根据字段)
、range(根据区间)
、等。
- 查询条件:查询条件会根据类型的不同,写法也有差异。比如
bool
、过滤查询/分组查询
4.1 查询所有-match_all
查询类型为match_all
相当于_search
Get sku/_search
{
"query":{
"match_all":{
}
}
}
4.2 匹配查询-match
查询类型match
GET sku/_search
{
"query":{
"match":{
"name":"手机"
}
}
}
只要name
中包含手机、手、机都会被查询出来.会对手机进行分词,多个词语之间的关系默认为or
。如果要精准查询,需要将操作改为and
关系.
GET sku/_search
{
"query":{
"match":{
"name":{
"query":"小米手机",
"operator":"and"
}
}
}
}
4.3 多字段查询-multi_match
查询类型名称为:“multi_match”
Get sku/_search
{
"query":{
"multi_match":{
"query":"小米",
"fields":["name","brandName","categoryName"]
}
}
}
"fields":["name","brandName","categoryName"]
从这些字段查询小米
4.4 词条匹配-term
查询类型名称为:“term”,精确值匹配,可能是数字、时间、布尔或者未分词的字符串。
###单词条匹配
GET sku/_search
{
"query":{
"term":{
"price":20000
}
}
}
###多词条匹配
GET sku/_search
{
"query":{
"term":{
"price":[200000,300000]
}
}
}
4.5 布尔组合查询bool
查询类型为:“bool”。把”match”/“match_all”/“term”/“terms”等查询方式通过”must(与)”、“must_not(非)”、”should(或)”的方式进行组合。
#查询名称包含手机,并且品牌名称为小米的数据
Get sku/_search
{
"query":{
"bool":{
"must":[
"match":{
"name":"手机"
},
"term":{
"brandName":"小米"
}
]
}
}
}
4.6 过滤查询-bool-filter
过滤器查询的效率比查询效率高,过滤查询不会计算判断文档匹配度的得分.推荐尽量使用过滤
器去实现查询或者过滤器和查询共同使用。
过滤查询场景:我们希望查询出来的文档不需要对他进行匹配度评分、简单的进行筛选一下
查询类型为:“bool”
GET sku/_search
{
"query":{
"bool":{
"filter":{
"match":{
"brandName":"小米"}
}
}
}
}
4.7 分组查询-aggs
#分组查询
GET sku/_search
{
### aggs 表示分组
"size": 0, ###如果size:0 表示不用展示hits:[] 里面的数据了
"aggs": {
"sku_category": {
//分组展示的名称
"terms": {
"field": "price"
}
}
}
}
#多个分组条件
GET sku/_search
{
"size": 0, ###如果size:0 表示不用展示hits:[] 里面的数据了
"aggs": {
"sku_category": {
//分组展示的名称
"terms": {
"field": "price" //分组字段
}
},
"sku_brand": {
"terms": {
"field": "brandName"
}
}
}
}
###Mysql中多个字段的分组
####对Subject_Selection表中的数据进行分组,
####将具有相同Subject和Semester字段值的记录放到同一个分组里去,
####然后对每个分组中的数据应用聚合函数(COUNT,SUM, AVG,etc)。
####分组条件使用having
SELECT Subject, Semester, Count(*)FROM Subject_Selection GROUP BY Subject, Semester
二、使用java操作索引
使用java high level rest client。elasticsearch 版本需要在ES5以上。
创建索引和映射对象,可以使用kibana工具进行创建,然后添加、查询、修改索引使用java代码进行操作。
1.新增修改数据
HttpHost : url地址封装
RestClientBuilder: rest客户端构建器
RestHighLevelClient: rest高级客户端
HttpHost、RestClientBuild、RestHighLevelClient 这三个类用于连接ElasticSearch。
IndexRequest: 新增或修改请求
IndexResponse:新增或修改的响应结果
BulkRequest: 批量请求(用于增删改操作) ;BulkRequest里面可以装入多个IndexRequest。
BulkResponse:批量请求(用于增删改操作)
1.1 新增修改单条数据
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import java.util.HashMap;
import java.util.Map;
public class Demo1 {
public static void main(String[] args) {
//1.链接rest接口
HttpHost http = new HttpHost("127.0.0.1", 9200, "http");
RestClientBuilder restClientBuilder = RestClient.builder(http);
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);
//2.封装请求对象 (指定id)
IndexRequest indexRequest = new IndexRequest("sku", "doc", "3");
Map skuMap = new HashMap();
skuMap.put("name", "华为p30");
skuMap.put("name", "华为p30pro");
skuMap.put("brandName", "华为");
skuMap.put("categoryName", "手机");
skuMap.put("price", 1010221);
skuMap.put("createTime", "2019-05-01");
skuMap.put("saleNum", 101021);
skuMap.put("commentNum", 10102321);
//spec在索引库的类型为Object ,在java代码中我们使用map进行封装spec,然后在封装到外面一层的map。
Map spec = new HashMap();
spec.put("网络制式", "移动4G");
spec.put("屏幕尺寸", "5");
skuMap.put("spec", spec);
//除了使用Map,也可以使用实体类进行封装。
indexRequest.source(skuMap);
//3.获取请求结果
try{
IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
//执行状态
int status = indexResponse.status().getStatus();
System.out.println(status);
}catch (Exception e){
e.printStackTrace();
}
//关闭资源流
}
}
1.2 批量插入
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import java.util.HashMap;
import java.util.Map;
public class Demo2 {
public static void main(String[] args) {
//1.连接rest接口
HttpHost http = new HttpHost("127.0.0.1", 9200, "http");
RestClientBuilder builder = RestClient.builder(http);
//rest构建器
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);
//高级客户端对象
// 2.封装请求对象
BulkRequest bulkRequest = new BulkRequest();
//----------------------
IndexRequest indexRequest = new IndexRequest("sku", "doc", "4");
Map skuMap = new HashMap();
skuMap.put("name", "华为p30pro 火爆上市");
skuMap.put("brandName", "华为");
skuMap.put("categoryName", "手机");
skuMap.put("price", 1010221);
skuMap.put("createTime", "2019‐05‐01");
skuMap.put("saleNum", 101021);
skuMap.put("commentNum", 10102321);
Map spec = new HashMap();
spec.put("网络制式", "移动4G");
spec.put("屏幕尺寸", "5");
skuMap.put("spec", spec);
indexRequest.source(skuMap);
bulkRequest.add(indexRequest); //可以循环加入的
//--------------------
//可以多次添加
// 3.获取响应结果
try {
BulkResponse response = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
int status = response.status().getStatus();
System.out.println(status);
String message = response.buildFailureMessage();
System.out.println(message);
restHighLevelClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
2.匹配查询
SearchRequest: 查询请求对象
SearchResponse:查询响应对象
SearchSourceBuilder:查询源构建器
MatchQueryBuilder:匹配查询构建器
查询商品名称包含手机的数据
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
public class Demo3 {
public static void main(String[] args) throws Exception{
//1.连接rest接口
HttpHost http = new HttpHost("127.0.0.1", 9200, "http");
RestClientBuilder builder = RestClient.builder(http);
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);
//2.封装查询请求
//对应GET sku 这部分。
SearchRequest searchRequest = new SearchRequest("sku");
searchRequest.types("doc");//如果不写,可以查询所有类型. GET sku/doc/_search
//查询源构造器
/* { "query": { ---->new SearchSourceBuilder(); "match": { ----> "name": "手机" ---->MatchQueryBuilder } } } */
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name", "手机");
searchSourceBuilder.query(matchQueryBuilder);
//相当于最外面的大括号.
searchRequest.source(searchSourceBuilder);
//3.获取查询结果
//相当于 _search
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHits searchHits = searchResponse.getHits();
long totalHits = searchHits.getTotalHits();
System.out.println("返回记录数:"+totalHits);
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {
String source = hit.getSourceAsString();//json字符串。也可以获取map类型
System.out.println(source);
}
restHighLevelClient.close();
}
}
3.布尔和词条查询
SearchRequest: 查询请求对象
SearchResponse:查询响应对象
SearchSourceBuilder:查询源构建器
BoolQueryBuilder:布尔查询构建器
TermQueryBuilder:词条查询构建器
QueryBuilders:查询构建器工厂
查询名称包含手机的,并且品牌为小米的记录
#查询名称包含手机的,并且品牌为小米的。
Get sku/_search
{
"query":{
"bool":{
"must":[
{
"match":{
"name":"手机"}},
{
"term":{
"brandName":"小米"}}
]
}
}
}
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.*;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
public class Demo4 {
/* #查询名称包含手机的,并且品牌为小米的。 Get sku/_search { "query":{ ---- new SearchSourceBuilder(); "bool":{ ---> BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); "must":[ {"match":{"name":"手机"}}, ----->MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name","手机"); {"term":{"brandName":"小米"}} ----> TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("brandName", "小米"); ] } } } */
public static void main(String[] args) throws Exception {
//1.连接rest接口
HttpHost http = new HttpHost("127.0.0.1", 9200, "http");
RestClientBuilder builder = RestClient.builder(http);
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);
//2.封装查询对象
SearchRequest searchRequest = new SearchRequest("sku");
searchRequest.types("doc");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//布尔构建查询器
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name","手机");
boolQueryBuilder.must(matchQueryBuilder);
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("brandName", "小米");
boolQueryBuilder.must(termQueryBuilder);
//相当于最外面的大括号.
searchRequest.source(searchSourceBuilder);
//3.获取查询结果
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHits searchHits = searchResponse.getHits();
long totalHits = searchHits.getTotalHits();
System.out.println("返回记录数:"+totalHits);
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {
String source = hit.getSourceAsString();//json字符串。也可以获取map类型
System.out.println(source);
}
restHighLevelClient.close();
}
}
4.过滤查询
过滤查询依赖bool,filter是一个链接关键字,相当于must
筛选品牌为小米记录
package com.qingcheng;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
public class Demo5 {
/* #查询名称包含手机的,并且品牌为小米的。 Get sku/_search { "query":{ ---- new SearchSourceBuilder(); "bool":{ ---> BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery(); "filter":[ {"match":{"name":"手机"}}, ----->MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name","手机"); ] } } } */
public static void main(String[] args) throws Exception {
//1.连接rest接口
HttpHost http = new HttpHost("127.0.0.1", 9200, "http");
RestClientBuilder builder = RestClient.builder(http);
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);
//2.封装查询对象
SearchRequest searchRequest = new SearchRequest("sku");
searchRequest.types("doc");
//查询源构造器
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//布尔构建查询器
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("brandName", "小米");
boolQueryBuilder.filter(termQueryBuilder);
//相当于最外面的大括号.
searchRequest.source(searchSourceBuilder);
//3.获取查询结果
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
SearchHits searchHits = searchResponse.getHits();
long totalHits = searchHits.getTotalHits();
System.out.println("返回记录数:"+totalHits);
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits) {
String source = hit.getSourceAsString();//json字符串。也可以获取map类型
System.out.println(source);
}
restHighLevelClient.close();
}
}
5.分组查询(聚合查询)
AggregationBuilders:聚合构建器工厂
TermsAggregationBuilder:词条聚合构建器
Aggregations:分组结果封装
Terms.Bucket: 桶
package com.qingcheng;
import org.apache.http.HttpHost;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.Aggregations;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import java.util.List;
import java.util.Map;
public class Demo6 {
public static void main(String[] args) throws Exception {
//1.连接rest接口
HttpHost http = new HttpHost("127.0.0.1", 9200, "http");
RestClientBuilder builder = RestClient.builder(http);
RestHighLevelClient restHighLevelClient = new RestHighLevelClient(builder);
//2.封装查询对象
SearchRequest searchRequest = new SearchRequest("sku");
searchRequest.types("doc");
//查询员构造器
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
/* #分组查询 GET sku/_search { ### aggs 表示分组 size:0 "aggs": { "sku_category": { //分组展示的名称 "terms": { "field": "categoryName" } } } } */
//聚合构造器工程
TermsAggregationBuilder sku_category = AggregationBuilders.terms("sku_category");
//词条聚合构建器
TermsAggregationBuilder termsAggregationBuilder = sku_category.field("categoryName");//指定按照什么分组
searchSourceBuilder.aggregation(termsAggregationBuilder);
searchSourceBuilder.size(0);
//相当于最外面的大括号.
searchRequest.source(searchSourceBuilder);
//3.获取查询结果
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
/* SearchHits searchHits = searchResponse.getHits(); long totalHits = searchHits.getTotalHits(); System.out.println("返回记录数:"+totalHits); SearchHit[] hits = searchHits.getHits(); for (SearchHit hit : hits) { String source = hit.getSourceAsString();//json字符串。也可以获取map类型 System.out.println(source); }*/
Aggregations aggregations = searchResponse.getAggregations();
Map<String, Aggregation> aggregationMap = aggregations.getAsMap();
// Aggregation aggregation = aggregationMap.get("sku_category");
//Terms 是Aggregation的子类
Terms terms = (Terms)aggregationMap.get("sku_category");
List<? extends Terms.Bucket> buckets = terms.getBuckets();
for (Terms.Bucket bucket : buckets) {
System.out.println(bucket.getKeyAsString()+"___"+bucket.getDocCount());
}
restHighLevelClient.close();
}
}
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/100761.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...