mirror of
https://gitee.com/dromara/easy-es.git
synced 2025-12-06 09:09:13 +08:00
feat: Springboot测试工具. 优化mix查询
This commit is contained in:
parent
9d34f1ad3a
commit
6a7c18e55d
@ -68,5 +68,5 @@ public class Param extends Tree {
|
||||
/**
|
||||
* 混合查询 原生查询条件
|
||||
*/
|
||||
private Query.Builder queryBuilder;
|
||||
private Query query;
|
||||
}
|
||||
|
||||
@ -236,10 +236,10 @@ public interface Func<Children, R> extends Serializable {
|
||||
/**
|
||||
* 地理位置坐标点由近及远排序
|
||||
*
|
||||
* @param column 列
|
||||
* @param column 列
|
||||
* @param geoDistanceType 距离计算方式
|
||||
* @param lat 纬度
|
||||
* @param lon 经度
|
||||
* @param lat 纬度
|
||||
* @param lon 经度
|
||||
* @return wrapper`
|
||||
*/
|
||||
default Children orderByDistanceAsc(R column, GeoDistanceType geoDistanceType, double lat, double lon) {
|
||||
@ -249,11 +249,11 @@ public interface Func<Children, R> extends Serializable {
|
||||
/**
|
||||
* 地理位置坐标点由近及远排序
|
||||
*
|
||||
* @param column 列
|
||||
* @param unit 距离单位
|
||||
* @param column 列
|
||||
* @param unit 距离单位
|
||||
* @param geoDistanceType 距离计算方式
|
||||
* @param lat 纬度
|
||||
* @param lon 经度
|
||||
* @param lat 纬度
|
||||
* @param lon 经度
|
||||
* @return wrapper
|
||||
*/
|
||||
default Children orderByDistanceAsc(R column, DistanceUnit unit, GeoDistanceType geoDistanceType, double lat, double lon) {
|
||||
@ -286,9 +286,9 @@ public interface Func<Children, R> extends Serializable {
|
||||
/**
|
||||
* 地理位置坐标点由近及远排序
|
||||
*
|
||||
* @param column 列
|
||||
* @param column 列
|
||||
* @param geoDistanceType 距离计算方式
|
||||
* @param geoPoints 多边形坐标点数组
|
||||
* @param geoPoints 多边形坐标点数组
|
||||
* @return wrapper
|
||||
*/
|
||||
default Children orderByDistanceAsc(R column, GeoDistanceType geoDistanceType, GeoLocation... geoPoints) {
|
||||
@ -298,10 +298,10 @@ public interface Func<Children, R> extends Serializable {
|
||||
/**
|
||||
* 地理位置坐标点由近及远排序
|
||||
*
|
||||
* @param column 列
|
||||
* @param unit 距离单位
|
||||
* @param column 列
|
||||
* @param unit 距离单位
|
||||
* @param geoDistanceType 距离计算方式
|
||||
* @param geoPoints 多边形坐标点数组
|
||||
* @param geoPoints 多边形坐标点数组
|
||||
* @return wrapper
|
||||
*/
|
||||
default Children orderByDistanceAsc(R column, DistanceUnit unit, GeoDistanceType geoDistanceType, GeoLocation... geoPoints) {
|
||||
@ -336,10 +336,10 @@ public interface Func<Children, R> extends Serializable {
|
||||
/**
|
||||
* 地理位置坐标点由近及远排序
|
||||
*
|
||||
* @param column 列名 字符串
|
||||
* @param column 列名 字符串
|
||||
* @param geoDistanceType 距离计算方式
|
||||
* @param lat 纬度
|
||||
* @param lon 经度
|
||||
* @param lat 纬度
|
||||
* @param lon 经度
|
||||
* @return wrapper
|
||||
*/
|
||||
default Children orderByDistanceAsc(String column, GeoDistanceType geoDistanceType, double lat, double lon) {
|
||||
@ -349,11 +349,11 @@ public interface Func<Children, R> extends Serializable {
|
||||
/**
|
||||
* 地理位置坐标点由近及远排序
|
||||
*
|
||||
* @param column 列名 字符串
|
||||
* @param unit 距离单位
|
||||
* @param column 列名 字符串
|
||||
* @param unit 距离单位
|
||||
* @param geoDistanceType 距离计算方式
|
||||
* @param lat 纬度
|
||||
* @param lon 经度
|
||||
* @param lat 纬度
|
||||
* @param lon 经度
|
||||
* @return wrapper
|
||||
*/
|
||||
default Children orderByDistanceAsc(String column, DistanceUnit unit, GeoDistanceType geoDistanceType, double lat, double lon) {
|
||||
@ -386,9 +386,9 @@ public interface Func<Children, R> extends Serializable {
|
||||
/**
|
||||
* 地理位置坐标点由近及远排序
|
||||
*
|
||||
* @param column 列名 字符串
|
||||
* @param column 列名 字符串
|
||||
* @param geoDistanceType 距离计算方式
|
||||
* @param geoPoints 多边形坐标点数组
|
||||
* @param geoPoints 多边形坐标点数组
|
||||
* @return wrapper
|
||||
*/
|
||||
default Children orderByDistanceAsc(String column, GeoDistanceType geoDistanceType, GeoLocation... geoPoints) {
|
||||
@ -398,10 +398,10 @@ public interface Func<Children, R> extends Serializable {
|
||||
/**
|
||||
* 地理位置坐标点由近及远排序
|
||||
*
|
||||
* @param column 列名 字符串
|
||||
* @param unit 距离单位
|
||||
* @param column 列名 字符串
|
||||
* @param unit 距离单位
|
||||
* @param geoDistanceType 距离计算方式
|
||||
* @param geoPoints 多边形坐标点数组
|
||||
* @param geoPoints 多边形坐标点数组
|
||||
* @return wrapper
|
||||
*/
|
||||
default Children orderByDistanceAsc(String column, DistanceUnit unit, GeoDistanceType geoDistanceType, GeoLocation... geoPoints) {
|
||||
@ -411,11 +411,11 @@ public interface Func<Children, R> extends Serializable {
|
||||
/**
|
||||
* 地理位置坐标点由近及远排序
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param column 列名 字符串
|
||||
* @param unit 距离单位 重载方法默认为km
|
||||
* @param condition 执行条件
|
||||
* @param column 列名 字符串
|
||||
* @param unit 距离单位 重载方法默认为km
|
||||
* @param geoDistanceType 距离计算方式,重载方法默认为GeoDistanceType.Plane
|
||||
* @param geoPoints 多边形坐标点数组
|
||||
* @param geoPoints 多边形坐标点数组
|
||||
* @return wrapper
|
||||
*/
|
||||
Children orderByDistanceAsc(boolean condition, String column, DistanceUnit unit, GeoDistanceType geoDistanceType, GeoLocation... geoPoints);
|
||||
@ -448,10 +448,10 @@ public interface Func<Children, R> extends Serializable {
|
||||
/**
|
||||
* 地理位置坐标点由近及远排序
|
||||
*
|
||||
* @param column 列
|
||||
* @param column 列
|
||||
* @param geoDistanceType 距离计算方式,重载方法默认为GeoDistanceType.Plane
|
||||
* @param lat 纬度
|
||||
* @param lon 经度
|
||||
* @param lat 纬度
|
||||
* @param lon 经度
|
||||
* @return wrapper
|
||||
*/
|
||||
default Children orderByDistanceDesc(R column, GeoDistanceType geoDistanceType, double lat, double lon) {
|
||||
@ -461,11 +461,11 @@ public interface Func<Children, R> extends Serializable {
|
||||
/**
|
||||
* 地理位置坐标点由近及远排序
|
||||
*
|
||||
* @param column 列名
|
||||
* @param unit 距离单位
|
||||
* @param column 列名
|
||||
* @param unit 距离单位
|
||||
* @param geoDistanceType 距离计算方式,重载方法默认为GeoDistanceType.Plane
|
||||
* @param lat 纬度
|
||||
* @param lon 经度
|
||||
* @param lat 纬度
|
||||
* @param lon 经度
|
||||
* @return wrapper
|
||||
*/
|
||||
default Children orderByDistanceDesc(R column, DistanceUnit unit, GeoDistanceType geoDistanceType, double lat, double lon) {
|
||||
@ -498,9 +498,9 @@ public interface Func<Children, R> extends Serializable {
|
||||
/**
|
||||
* 地理位置坐标点由近及远排序
|
||||
*
|
||||
* @param column 列
|
||||
* @param column 列
|
||||
* @param geoDistanceType 距离计算方式,重载方法默认为GeoDistanceType.Plane
|
||||
* @param geoPoints 多边形坐标点数组
|
||||
* @param geoPoints 多边形坐标点数组
|
||||
* @return wrapper
|
||||
*/
|
||||
default Children orderByDistanceDesc(R column, GeoDistanceType geoDistanceType, GeoLocation... geoPoints) {
|
||||
@ -510,10 +510,10 @@ public interface Func<Children, R> extends Serializable {
|
||||
/**
|
||||
* 地理位置坐标点由近及远排序
|
||||
*
|
||||
* @param column 列
|
||||
* @param unit 距离单位
|
||||
* @param column 列
|
||||
* @param unit 距离单位
|
||||
* @param geoDistanceType 距离计算方式,重载方法默认为GeoDistanceType.Plane
|
||||
* @param geoPoints 多边形坐标点数组
|
||||
* @param geoPoints 多边形坐标点数组
|
||||
* @return wrapper
|
||||
*/
|
||||
default Children orderByDistanceDesc(R column, DistanceUnit unit, GeoDistanceType geoDistanceType, GeoLocation... geoPoints) {
|
||||
@ -548,10 +548,10 @@ public interface Func<Children, R> extends Serializable {
|
||||
/**
|
||||
* 地理位置坐标点由近及远排序
|
||||
*
|
||||
* @param column 列名 字符串
|
||||
* @param column 列名 字符串
|
||||
* @param geoDistanceType 距离计算方式,重载方法默认为GeoDistanceType.Plane
|
||||
* @param lat 纬度
|
||||
* @param lon 经度
|
||||
* @param lat 纬度
|
||||
* @param lon 经度
|
||||
* @return wrapper
|
||||
*/
|
||||
default Children orderByDistanceDesc(String column, GeoDistanceType geoDistanceType, double lat, double lon) {
|
||||
@ -561,11 +561,11 @@ public interface Func<Children, R> extends Serializable {
|
||||
/**
|
||||
* 地理位置坐标点由近及远排序
|
||||
*
|
||||
* @param column 列名 字符串
|
||||
* @param unit 距离单位
|
||||
* @param column 列名 字符串
|
||||
* @param unit 距离单位
|
||||
* @param geoDistanceType 距离计算方式,重载方法默认为GeoDistanceType.Plane
|
||||
* @param lat 纬度
|
||||
* @param lon 经度
|
||||
* @param lat 纬度
|
||||
* @param lon 经度
|
||||
* @return wrapper
|
||||
*/
|
||||
default Children orderByDistanceDesc(String column, DistanceUnit unit, GeoDistanceType geoDistanceType, double lat, double lon) {
|
||||
@ -598,9 +598,9 @@ public interface Func<Children, R> extends Serializable {
|
||||
/**
|
||||
* 地理位置坐标点由近及远排序
|
||||
*
|
||||
* @param column 列名 字符串
|
||||
* @param column 列名 字符串
|
||||
* @param geoDistanceType 距离计算方式,重载方法默认为GeoDistanceType.Plane
|
||||
* @param geoPoints 多边形坐标点数组
|
||||
* @param geoPoints 多边形坐标点数组
|
||||
* @return wrapper
|
||||
*/
|
||||
default Children orderByDistanceDesc(String column, GeoDistanceType geoDistanceType, GeoLocation... geoPoints) {
|
||||
@ -610,10 +610,10 @@ public interface Func<Children, R> extends Serializable {
|
||||
/**
|
||||
* 地理位置坐标点由近及远排序
|
||||
*
|
||||
* @param column 列名 字符串
|
||||
* @param unit 距离单位
|
||||
* @param column 列名 字符串
|
||||
* @param unit 距离单位
|
||||
* @param geoDistanceType 距离计算方式,重载方法默认为GeoDistanceType.Plane
|
||||
* @param geoPoints 多边形坐标点数组
|
||||
* @param geoPoints 多边形坐标点数组
|
||||
* @return wrapper
|
||||
*/
|
||||
default Children orderByDistanceDesc(String column, DistanceUnit unit, GeoDistanceType geoDistanceType, GeoLocation... geoPoints) {
|
||||
@ -623,11 +623,11 @@ public interface Func<Children, R> extends Serializable {
|
||||
/**
|
||||
* 地理位置坐标点由远及近排序
|
||||
*
|
||||
* @param condition 条件
|
||||
* @param column 列名 字符串
|
||||
* @param unit 距离单位 重载方法默认为km
|
||||
* @param condition 条件
|
||||
* @param column 列名 字符串
|
||||
* @param unit 距离单位 重载方法默认为km
|
||||
* @param geoDistanceType 距离计算方式,重载方法默认为GeoDistanceType.Plane
|
||||
* @param geoPoints 多边形坐标点数组
|
||||
* @param geoPoints 多边形坐标点数组
|
||||
* @return wrapper
|
||||
*/
|
||||
Children orderByDistanceDesc(boolean condition, String column, DistanceUnit unit, GeoDistanceType geoDistanceType, GeoLocation... geoPoints);
|
||||
@ -1685,22 +1685,22 @@ public interface Func<Children, R> extends Serializable {
|
||||
/**
|
||||
* 混合查询
|
||||
*
|
||||
* @param queryBuilder 原生查询条件
|
||||
* @param query 原生查询条件
|
||||
* @return wrapper
|
||||
*/
|
||||
default Children mix(co.elastic.clients.elasticsearch._types.query_dsl.Query.Builder queryBuilder) {
|
||||
return mix(true, queryBuilder);
|
||||
default Children mix(co.elastic.clients.elasticsearch._types.query_dsl.Query query) {
|
||||
return mix(true, query);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 混合查询
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param queryBuilder 原生查询条件
|
||||
* @param condition 执行条件
|
||||
* @param query 原生查询条件
|
||||
* @return wrapper
|
||||
*/
|
||||
Children mix(boolean condition, co.elastic.clients.elasticsearch._types.query_dsl.Query.Builder queryBuilder);
|
||||
Children mix(boolean condition, co.elastic.clients.elasticsearch._types.query_dsl.Query query);
|
||||
|
||||
|
||||
/**
|
||||
|
||||
@ -663,8 +663,8 @@ public abstract class AbstractChainWrapper<T, R, Children extends AbstractChainW
|
||||
}
|
||||
|
||||
@Override
|
||||
public Children mix(boolean condition, co.elastic.clients.elasticsearch._types.query_dsl.Query.Builder queryBuilder) {
|
||||
getWrapper().mix(condition, queryBuilder);
|
||||
public Children mix(boolean condition, co.elastic.clients.elasticsearch._types.query_dsl.Query query) {
|
||||
getWrapper().mix(condition, query);
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
|
||||
@ -601,8 +601,8 @@ public abstract class AbstractWrapper<T, R, Children extends AbstractWrapper<T,
|
||||
}
|
||||
|
||||
@Override
|
||||
public Children mix(boolean condition, co.elastic.clients.elasticsearch._types.query_dsl.Query.Builder queryBuilder) {
|
||||
return addParam(condition, queryBuilder);
|
||||
public Children mix(boolean condition, co.elastic.clients.elasticsearch._types.query_dsl.Query query) {
|
||||
return addParam(condition, query);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -823,14 +823,14 @@ public abstract class AbstractWrapper<T, R, Children extends AbstractWrapper<T,
|
||||
/**
|
||||
* 追加查询参数
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param queryBuilder 原生查询参数
|
||||
* @param condition 执行条件
|
||||
* @param query 原生查询参数
|
||||
* @return wrapper
|
||||
*/
|
||||
private Children addParam(boolean condition, co.elastic.clients.elasticsearch._types.query_dsl.Query.Builder queryBuilder) {
|
||||
private Children addParam(boolean condition, co.elastic.clients.elasticsearch._types.query_dsl.Query query) {
|
||||
if (condition) {
|
||||
Param param = new Param();
|
||||
param.setQueryBuilder(queryBuilder);
|
||||
param.setQuery(query);
|
||||
addBaseParam(param, MIX, null, null, null);
|
||||
}
|
||||
return typedThis;
|
||||
|
||||
@ -94,7 +94,7 @@ public class WrapperProcessor {
|
||||
// 渣男行为,*完就不认人了,因为拼接类型在AbstractWrapper中已处理过了 直接跳过
|
||||
break;
|
||||
case MIX:
|
||||
setBool(bool, param.getQueryBuilder().build(), param.getPrevQueryType());
|
||||
setBool(bool, param.getQuery(), param.getPrevQueryType());
|
||||
break;
|
||||
case TERM:
|
||||
realField = getRealFieldAndSuffix(param.getColumn(), fieldTypeMap, mappingColumnMap, entityInfo);
|
||||
|
||||
@ -32,7 +32,7 @@ public class Document {
|
||||
* es中的唯一id,字段名随便起,我这里演示用esId,你也可以用id(推荐),bizId等.
|
||||
* 如果你想自定义es中的id为你提供的id,比如MySQL中的id,请将注解中的type指定为customize或直接在全局配置文件中指定,如此id便支持任意数据类型)
|
||||
*/
|
||||
@IndexId(type = IdType.CUSTOMIZE)
|
||||
@IndexId(type = IdType.CUSTOMIZE, writeToSource = true)
|
||||
private String esId;
|
||||
/**
|
||||
* 文档标题,不指定类型默认被创建为keyword类型,可进行精确查询
|
||||
@ -118,7 +118,7 @@ public class Document {
|
||||
* es返回的得分字段,字段名字随便取,只要加了@Score注解即可
|
||||
*/
|
||||
@Score(decimalPlaces = 2)
|
||||
private Float score;
|
||||
private Double score;
|
||||
/**
|
||||
* es返回的距离,字段名字随便取,距离单位以用户在序器中指定的为准,不指定es默认为:米
|
||||
*/
|
||||
|
||||
@ -1,11 +1,11 @@
|
||||
package org.dromara.easyes.test.agg;
|
||||
|
||||
import co.elastic.clients.elasticsearch.core.SearchResponse;
|
||||
import org.dromara.easyes.core.biz.EsPageInfo;
|
||||
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
|
||||
import org.dromara.easyes.test.TestEasyEsApplication;
|
||||
import org.dromara.easyes.test.entity.Document;
|
||||
import org.dromara.easyes.test.mapper.DocumentMapper;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
@ -41,7 +41,7 @@ public class AggTest {
|
||||
wrapper.eq(Document::getTitle, "老汉")
|
||||
.groupBy(Document::getSubTitle)
|
||||
.max(Document::getStarNum);
|
||||
SearchResponse response = documentMapper.search(wrapper);
|
||||
SearchResponse<Document> response = documentMapper.search(wrapper);
|
||||
System.out.println(response);
|
||||
}
|
||||
|
||||
@ -55,7 +55,7 @@ public class AggTest {
|
||||
.groupBy(Document::getTitle)
|
||||
.max(Document::getStarNum)
|
||||
.min(Document::getStarNum);
|
||||
SearchResponse response = documentMapper.search(wrapper);
|
||||
SearchResponse<Document> response = documentMapper.search(wrapper);
|
||||
System.out.println(response);
|
||||
}
|
||||
|
||||
@ -71,7 +71,7 @@ public class AggTest {
|
||||
.min(true, Document::getStarNum, Document::getScore)
|
||||
.max(false, Document::getStarNum, Document::getScore)
|
||||
.sum(false, false, Document::getStarNum, Document::getScore);
|
||||
SearchResponse response = documentMapper.search(wrapper);
|
||||
SearchResponse<Document> response = documentMapper.search(wrapper);
|
||||
System.out.println(response);
|
||||
}
|
||||
|
||||
@ -86,7 +86,7 @@ public class AggTest {
|
||||
.min(true, "starNum", "score")
|
||||
.max(false, "starNum", "score")
|
||||
.sum(false, false, "starNum", "score");
|
||||
SearchResponse response = documentMapper.search(wrapper);
|
||||
SearchResponse<Document> response = documentMapper.search(wrapper);
|
||||
System.out.println(response);
|
||||
}
|
||||
|
||||
@ -97,7 +97,7 @@ public class AggTest {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
// 指定启用管道聚合为false
|
||||
wrapper.groupBy(false, Document::getTitle, Document::getSubTitle);
|
||||
SearchResponse response = documentMapper.search(wrapper);
|
||||
SearchResponse<Document> response = documentMapper.search(wrapper);
|
||||
System.out.println(response);
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,45 +1,33 @@
|
||||
package org.dromara.easyes.test.all;
|
||||
|
||||
|
||||
import co.elastic.clients.elasticsearch._types.FieldValue;
|
||||
import co.elastic.clients.elasticsearch._types.GeoLocation;
|
||||
import co.elastic.clients.elasticsearch._types.*;
|
||||
import co.elastic.clients.elasticsearch._types.aggregations.Aggregate;
|
||||
import co.elastic.clients.elasticsearch._types.aggregations.LongTermsBucket;
|
||||
import co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery;
|
||||
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
|
||||
import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
|
||||
import co.elastic.clients.elasticsearch.core.SearchRequest;
|
||||
import org.dromara.easyes.common.constants.BaseEsConstants;
|
||||
import co.elastic.clients.elasticsearch.core.SearchResponse;
|
||||
import co.elastic.clients.json.JsonData;
|
||||
import co.elastic.clients.transport.rest_client.RestClientOptions;
|
||||
import org.dromara.easyes.core.biz.EntityInfo;
|
||||
import org.dromara.easyes.core.biz.EsPageInfo;
|
||||
import org.dromara.easyes.core.biz.OrderByParam;
|
||||
import org.dromara.easyes.core.biz.SAPageInfo;
|
||||
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
|
||||
import org.dromara.easyes.core.conditions.update.LambdaEsUpdateWrapper;
|
||||
import org.dromara.easyes.core.kernel.EsWrappers;
|
||||
import org.dromara.easyes.core.kernel.WrapperProcessor;
|
||||
import org.dromara.easyes.core.toolkit.EntityInfoHelper;
|
||||
import org.dromara.easyes.core.toolkit.FieldUtils;
|
||||
import org.dromara.easyes.test.TestEasyEsApplication;
|
||||
import org.dromara.easyes.test.entity.Document;
|
||||
import org.dromara.easyes.test.mapper.DocumentMapper;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.client.HttpAsyncResponseConsumerFactory;
|
||||
import org.elasticsearch.client.RequestOptions;
|
||||
import org.elasticsearch.common.geo.GeoDistance;
|
||||
import org.elasticsearch.common.geo.GeoShapeRelation;
|
||||
import org.elasticsearch.common.unit.DistanceUnit;
|
||||
import org.elasticsearch.geometry.Circle;
|
||||
import org.elasticsearch.geometry.Point;
|
||||
import org.elasticsearch.geometry.Rectangle;
|
||||
import org.elasticsearch.index.query.BoolQueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.script.ScriptType;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.ParsedLongTerms;
|
||||
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
|
||||
import org.elasticsearch.search.aggregations.metrics.ParsedAvg;
|
||||
import org.elasticsearch.search.aggregations.metrics.ParsedMax;
|
||||
import org.elasticsearch.search.aggregations.metrics.ParsedMin;
|
||||
import org.elasticsearch.search.aggregations.metrics.ParsedSum;
|
||||
import org.elasticsearch.search.sort.FieldSortBuilder;
|
||||
import org.elasticsearch.search.sort.GeoDistanceSortBuilder;
|
||||
import org.elasticsearch.search.sort.SortBuilders;
|
||||
import org.elasticsearch.search.sort.SortOrder;
|
||||
import org.junit.jupiter.api.*;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@ -97,6 +85,7 @@ public class AllTest {
|
||||
document.setEnglish("Calcium Gluconate");
|
||||
document.setBigNum(new BigDecimal("66.66"));
|
||||
document.setVector(new double[]{0.39684247970581666, 0.768707156181666, 0.5145490765571666});
|
||||
// System.out.println(JsonUtils.toJsonPrettyStr(document));
|
||||
int successCount = documentMapper.insert(document);
|
||||
Assertions.assertEquals(successCount, 1);
|
||||
}
|
||||
@ -117,7 +106,6 @@ public class AllTest {
|
||||
document.setGeoLocation(point.toString());
|
||||
document.setStarNum(i);
|
||||
document.setVector(new double[]{35.89684247970581666, 86.268707156181666, 133.1145490765571666});
|
||||
|
||||
// 针对个别数据 造一些差异项 方便测试不同场景
|
||||
if (i == 2) {
|
||||
document.setLocation("40.17836693398477,116.64002551005981");
|
||||
@ -151,12 +139,12 @@ public class AllTest {
|
||||
wrapper.eq(Document::getTitle, "测试文档2");
|
||||
wrapper.set(Document::getContent, "测试文档内容2的内容被更新了");
|
||||
|
||||
int count = documentMapper.update(null, wrapper);
|
||||
int count = documentMapper.update(wrapper);
|
||||
Assertions.assertEquals(1, count);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
@Order(5)
|
||||
public void testUpdateByChainWrapper() {
|
||||
int count = EsWrappers.lambdaChainUpdate(documentMapper)
|
||||
.eq(Document::getTitle, "测试文档3")
|
||||
@ -166,12 +154,16 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(4)
|
||||
@Order(6)
|
||||
public void testUpdateBySetSearchSourceBuilder() {
|
||||
LambdaEsUpdateWrapper<Document> wrapper = new LambdaEsUpdateWrapper<>();
|
||||
SearchRequest.Builder searchSourceBuilder = new SearchRequest.Builder();
|
||||
searchSourceBuilder.query(QueryBuilders.termQuery(FieldUtils.val(Document::getTitle) + KEYWORD_SUFFIX, "测试文档2"));
|
||||
wrapper.setSearchBuilder(searchSourceBuilder);
|
||||
SearchRequest.Builder searchBuilder = new SearchRequest.Builder();
|
||||
searchBuilder.query(QueryBuilders.term()
|
||||
.field(FieldUtils.val(Document::getTitle) + KEYWORD_SUFFIX)
|
||||
.value("测试文档2")
|
||||
.build()._toQuery()
|
||||
);
|
||||
wrapper.setSearchBuilder(searchBuilder);
|
||||
|
||||
Document document = new Document();
|
||||
document.setContent("测试文档内容2的内容再次被更新了");
|
||||
@ -180,7 +172,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(5)
|
||||
@Order(7)
|
||||
public void testUpdateByWrapperAndEntity() {
|
||||
LambdaEsUpdateWrapper<Document> wrapper = new LambdaEsUpdateWrapper<>();
|
||||
wrapper.match(Document::getCreator, "老汉");
|
||||
@ -193,7 +185,7 @@ public class AllTest {
|
||||
// 3.查询
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(8)
|
||||
public void testSQL() {
|
||||
// 注意 sql中的from后面跟的是要被查询的索引名,也可以是索引别名(效果一样) 由于索引名可能会变,所以此处我采用别名ee_default_alias进行查询
|
||||
String sql = "select count(*) from ee_default_alias where star_num > 0";
|
||||
@ -203,7 +195,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(9)
|
||||
public void testDSL() {
|
||||
String dsl = "{\"query\":{\"bool\":{\"must\":[{\"term\":{\"title.keyword\":{\"value\":\"测试文档3\",\"boost\":1.0}}}],\"adjust_pure_negative\":true,\"boost\":1.0}},\"track_total_hits\":2147483647,\"highlight\":{\"pre_tags\":[\"<em>\"],\"post_tags\":[\"</em>\"],\"fragment_size\":2,\"fields\":{\"content\":{\"type\":\"unified\"}}}}";
|
||||
String jsonResult = documentMapper.executeDSL(dsl);
|
||||
@ -212,7 +204,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(10)
|
||||
public void testSelectOne() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.match(Document::getContent, "内容")
|
||||
@ -223,7 +215,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(11)
|
||||
public void testOne() {
|
||||
// 链式调用
|
||||
Document document = EsWrappers.lambdaChainQuery(documentMapper).eq(Document::getTitle, "测试文档3").one();
|
||||
@ -231,14 +223,14 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
public void testDefaultMethod(){
|
||||
@Order(12)
|
||||
public void testDefaultMethod() {
|
||||
List<Document> documents = documentMapper.testDefaultMethod();
|
||||
Assertions.assertEquals(documents.size(),1);
|
||||
Assertions.assertEquals(documents.size(), 1);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(13)
|
||||
public void testSelectById() {
|
||||
Document document = documentMapper.selectById(1);
|
||||
Assertions.assertEquals("1", document.getEsId());
|
||||
@ -246,14 +238,14 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(14)
|
||||
public void testSelectBatchIds() {
|
||||
List<Document> documents = documentMapper.selectBatchIds(Arrays.asList("1", "2"));
|
||||
Assertions.assertEquals(2, documents.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(15)
|
||||
public void testSelectList() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.match(Document::getCustomField, "字段");
|
||||
@ -262,7 +254,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(16)
|
||||
public void testIgnoreCase() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.eq(Document::getCaseTest, "test");
|
||||
@ -271,7 +263,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(17)
|
||||
public void testIp() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.eq(Document::getIpAddress, "192.168.0.0/16");
|
||||
@ -280,7 +272,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(18)
|
||||
public void testSelectCount() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.match(Document::getCustomField, "字段");
|
||||
@ -289,7 +281,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(19)
|
||||
public void testSelectCountDistinct() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.match(Document::getCustomField, "字段");
|
||||
@ -299,7 +291,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(20)
|
||||
public void testConditionAllEq() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
@ -313,7 +305,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(21)
|
||||
public void testConditionEq() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.eq(Document::getTitle, "测试文档10");
|
||||
@ -323,7 +315,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(22)
|
||||
public void testConditionGt() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.gt(Document::getStarNum, 20);
|
||||
@ -332,7 +324,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(23)
|
||||
public void testConditionGe() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.ge(Document::getStarNum, 20);
|
||||
@ -341,7 +333,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(24)
|
||||
public void testConditionLt() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.lt(Document::getStarNum, 3);
|
||||
@ -350,7 +342,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(25)
|
||||
public void testConditionLe() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.le(Document::getStarNum, 3);
|
||||
@ -359,7 +351,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(26)
|
||||
public void testConditionBetween() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.between(Document::getStarNum, 1, 10);
|
||||
@ -368,7 +360,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(27)
|
||||
public void testConditionLike() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.like(Document::getTitle, "试文档");
|
||||
@ -377,7 +369,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(28)
|
||||
public void testConditionLikeLeft() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.likeLeft(Document::getTitle, "文档10");
|
||||
@ -386,7 +378,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(29)
|
||||
public void testConditionLikeRight() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.likeRight(Document::getTitle, "测试文");
|
||||
@ -395,7 +387,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(30)
|
||||
public void testConditionIsNotNull() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.isNotNull(Document::getNullField);
|
||||
@ -404,7 +396,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(31)
|
||||
public void testConditionExists() {
|
||||
// exists等价于isNotNull 在es中更推荐此种语法
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
@ -413,9 +405,8 @@ public class AllTest {
|
||||
Assertions.assertEquals(1, documents.size());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(32)
|
||||
public void testConditionIn() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.in(Document::getEsId, "1", "2", "3");
|
||||
@ -429,70 +420,63 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(33)
|
||||
public void testConditionGroupBy() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.match(Document::getContent, "测试")
|
||||
.groupBy(Document::getStarNum);
|
||||
SearchResponse response = documentMapper.search(wrapper);
|
||||
ParsedLongTerms parsedLongTerms = response.getAggregations()
|
||||
.get("starNumTerms");
|
||||
Terms.Bucket bucket = parsedLongTerms.getBuckets().get(0);
|
||||
Assertions.assertTrue(bucket.getKey().equals(1L) && bucket.getDocCount() == 2L);
|
||||
SearchResponse<Document> response = documentMapper.search(wrapper);
|
||||
Aggregate aggregate = response.aggregations().get("starNumTerms");
|
||||
LongTermsBucket bucket = aggregate.lterms().buckets().array().get(0);
|
||||
Assertions.assertTrue(bucket.key() == 1L && bucket.docCount() == 2L);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(34)
|
||||
public void testConditionMax() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.match(Document::getContent, "测试")
|
||||
.max(Document::getStarNum);
|
||||
SearchResponse response = documentMapper.search(wrapper);
|
||||
ParsedMax parsedMax = response.getAggregations()
|
||||
.get("starNumMax");
|
||||
Assertions.assertTrue(parsedMax.getValue() > 21);
|
||||
SearchResponse<Document> response = documentMapper.search(wrapper);
|
||||
Aggregate agg = response.aggregations().get("starNumMax");
|
||||
Assertions.assertTrue(agg.max().value() > 21);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(35)
|
||||
public void testConditionMin() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.match(Document::getContent, "测试")
|
||||
.min(Document::getStarNum);
|
||||
SearchResponse response = documentMapper.search(wrapper);
|
||||
ParsedMin parsedMin = response.getAggregations()
|
||||
.get("starNumMin");
|
||||
Assertions.assertTrue(parsedMin.getValue() > 0 && parsedMin.getValue() < 2);
|
||||
SearchResponse<Document> response = documentMapper.search(wrapper);
|
||||
double parsedMin = response.aggregations().get("starNumMin").min().value();
|
||||
Assertions.assertTrue(parsedMin > 0 && parsedMin < 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(36)
|
||||
public void testConditionSum() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.match(Document::getContent, "测试")
|
||||
.sum(Document::getStarNum);
|
||||
SearchResponse response = documentMapper.search(wrapper);
|
||||
ParsedSum parsedSum = response.getAggregations()
|
||||
.get("starNumSum");
|
||||
Assertions.assertTrue(parsedSum.getValue() >= 252);
|
||||
SearchResponse<Document> response = documentMapper.search(wrapper);
|
||||
double parsedSum = response.aggregations().get("starNumSum").sum().value();
|
||||
Assertions.assertTrue(parsedSum >= 252);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(37)
|
||||
public void testConditionAvg() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.match(Document::getContent, "测试")
|
||||
.avg(Document::getStarNum);
|
||||
SearchResponse response = documentMapper.search(wrapper);
|
||||
ParsedAvg parsedAvg = response.getAggregations()
|
||||
.get("starNumAvg");
|
||||
Assertions.assertTrue(parsedAvg.getValue() > 11 && parsedAvg.getValue() < 12);
|
||||
SearchResponse<Document> response = documentMapper.search(wrapper);
|
||||
double parsedAvg = response.aggregations().get("starNumAvg").avg().value();
|
||||
Assertions.assertTrue(parsedAvg > 11 && parsedAvg < 12);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(38)
|
||||
public void testConditionDistinct() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.match(Document::getContent, "测试")
|
||||
@ -502,7 +486,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(39)
|
||||
public void testConditionLimit() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.match(Document::getCreator, "老汉")
|
||||
@ -512,7 +496,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(40)
|
||||
public void testConditionFromAndSize() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.match(Document::getCreator, "老汉")
|
||||
@ -523,7 +507,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(41)
|
||||
public void testConditionIndex() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.match(Document::getCreator, "老汉")
|
||||
@ -533,14 +517,17 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(42)
|
||||
public void testSetSearchSourceBuilder() {
|
||||
EntityInfo e = EntityInfoHelper.getEntityInfo(Document.class);
|
||||
// 测试混合查询的另一种方式
|
||||
SearchRequest.Builder searchSourceBuilder = new SearchRequest.Builder();
|
||||
searchSourceBuilder.query(QueryBuilders.matchQuery(FieldUtils.val(Document::getCreator), "老汉"));
|
||||
Optional.ofNullable(EntityInfoHelper.getEntityInfo(Document.class))
|
||||
.flatMap(i -> Optional.ofNullable(i.getMaxResultWindow()))
|
||||
.ifPresent(searchSourceBuilder::size);
|
||||
SearchRequest.Builder searchSourceBuilder = new SearchRequest.Builder()
|
||||
.query(QueryBuilders.match()
|
||||
.field(FieldUtils.val(Document::getCreator))
|
||||
.query("老汉")
|
||||
.build()._toQuery()
|
||||
)
|
||||
.size(e.getMaxResultWindow().intValue());
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.setSearchBuilder(searchSourceBuilder);
|
||||
List<Document> documents = documentMapper.selectList(wrapper);
|
||||
@ -548,7 +535,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(43)
|
||||
public void testConditionAnd() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.in(Document::getStarNum, 1, 2, 3, 4, 10, 11)
|
||||
@ -558,7 +545,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(44)
|
||||
public void testConditionOr() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.in(Document::getStarNum, 1, 10, 12, 13)
|
||||
@ -568,7 +555,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(45)
|
||||
public void testConditionOrInner() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.eq(Document::getTitle, "测试文档10")
|
||||
@ -579,7 +566,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(46)
|
||||
public void testConditionFilter() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.eq(Document::getStarNum, 10)
|
||||
@ -589,7 +576,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(47)
|
||||
public void testConditionNot() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.in(Document::getStarNum, 10, 11, 12, 13)
|
||||
@ -598,9 +585,8 @@ public class AllTest {
|
||||
Assertions.assertEquals(2, documents.size());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(48)
|
||||
public void testPageQuery() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.match(Document::getCreator, "老汉");
|
||||
@ -610,7 +596,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(49)
|
||||
public void testChainPage() {
|
||||
// 链式
|
||||
EsPageInfo<Document> pageInfo = EsWrappers.lambdaChainQuery(documentMapper)
|
||||
@ -620,9 +606,8 @@ public class AllTest {
|
||||
Assertions.assertEquals(22, pageInfo.getTotal());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(50)
|
||||
public void testSearchAfter() {
|
||||
LambdaEsQueryWrapper<Document> lambdaEsQueryWrapper = EsWrappers.lambdaQuery(Document.class);
|
||||
lambdaEsQueryWrapper.size(10);
|
||||
@ -639,7 +624,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(51)
|
||||
public void testFilterField() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.eq(Document::getTitle, "测试文档10")
|
||||
@ -651,7 +636,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(52)
|
||||
public void testNotFilterField() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.eq(Document::getTitle, "测试文档10")
|
||||
@ -663,7 +648,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(53)
|
||||
public void testOrderByDesc() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.match(Document::getCreator, "老汉");
|
||||
@ -674,7 +659,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(54)
|
||||
public void testOrderByAsc() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.match(Document::getCreator, "老汉");
|
||||
@ -685,13 +670,13 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(55)
|
||||
public void testOrderBy() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.match(Document::getCreator, "老汉");
|
||||
List<OrderByParam> orderByParams = new ArrayList<>();
|
||||
OrderByParam orderByParam = new OrderByParam();
|
||||
orderByParam.setOrder("star_num");
|
||||
orderByParam.setOrder("starNum");
|
||||
orderByParam.setSort("DESC");
|
||||
orderByParams.add(orderByParam);
|
||||
wrapper.orderBy(orderByParams);
|
||||
@ -701,10 +686,10 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(56)
|
||||
public void testOrderByDistanceAsc() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
GeoLocation centerPoint = new GeoLocation(41.0, 116.0);
|
||||
GeoLocation centerPoint = GeoLocation.of(a -> a.latlon(a1 -> a1.lat(41.0).lon(116.0)));
|
||||
wrapper.match(Document::getCreator, "老汉")
|
||||
.geoDistance(Document::getLocation, 168.8, centerPoint)
|
||||
.orderByDistanceAsc(Document::getLocation, centerPoint);
|
||||
@ -714,10 +699,10 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(57)
|
||||
public void testOrderByDistanceDesc() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
GeoLocation centerPoint = new GeoLocation(41.0, 116.0);
|
||||
GeoLocation centerPoint = GeoLocation.of(a -> a.latlon(a1 -> a1.lat(41.0).lon(116.0)));
|
||||
wrapper.match(Document::getCreator, "老汉")
|
||||
.geoDistance(Document::getLocation, 168.8, centerPoint)
|
||||
.orderByDistanceDesc(Document::getLocation, centerPoint);
|
||||
@ -727,11 +712,11 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(58)
|
||||
public void testOrderByDistanceMulti() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
GeoLocation centerPoint = new GeoLocation(41.0, 116.0);
|
||||
GeoLocation centerPoint1 = new GeoLocation(42.0, 118.0);
|
||||
GeoLocation centerPoint = GeoLocation.of(a -> a.latlon(a1 -> a1.lat(41.0).lon(116.0)));
|
||||
GeoLocation centerPoint1 = GeoLocation.of(a -> a.latlon(a1 -> a1.lat(42.0).lon(118.0)));
|
||||
wrapper.match(Document::getCreator, "老汉")
|
||||
.geoDistance(Document::getLocation, 168.8, centerPoint)
|
||||
.orderByDistanceDesc(Document::getLocation, centerPoint)
|
||||
@ -742,7 +727,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(59)
|
||||
public void testSortByScore() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.match(Document::getCreator, "老汉11");
|
||||
@ -752,23 +737,27 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(60)
|
||||
public void testSort() {
|
||||
EntityInfo entityInfo = EntityInfoHelper.getEntityInfo(Document.class);
|
||||
String realField = FieldUtils.getRealField(
|
||||
FieldUtils.val(Document::getStarNum),
|
||||
entityInfo.getMappingColumnMap(),
|
||||
entityInfo);
|
||||
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.match(Document::getCreator, "老汉");
|
||||
FieldSortBuilder fieldSortBuilder = SortBuilders.
|
||||
fieldSort(FieldUtils.getRealField(
|
||||
FieldUtils.val(Document::getStarNum),
|
||||
EntityInfoHelper.getEntityInfo(Document.class).getMappingColumnMap(), entityInfo.getKeyProperty()));
|
||||
fieldSortBuilder.order(SortOrder.Desc);
|
||||
wrapper.sort(fieldSortBuilder);
|
||||
wrapper.sort(SortOptions.of(a -> a.field(b -> b
|
||||
.field(realField)
|
||||
.order(SortOrder.Desc)
|
||||
)));
|
||||
List<Document> documents = documentMapper.selectList(wrapper);
|
||||
Assertions.assertEquals("22", documents.get(0).getEsId());
|
||||
Assertions.assertEquals("21", documents.get(1).getEsId());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(61)
|
||||
public void testMatch() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.match(Document::getCreator, "老汉");
|
||||
@ -777,12 +766,12 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(62)
|
||||
public void testMatchPhrase() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.matchPhrase(Document::getContent, "测试");
|
||||
List<Document> documents = documentMapper.selectList(wrapper);
|
||||
Assertions.assertTrue(documents.size() > 0);
|
||||
Assertions.assertFalse(documents.isEmpty());
|
||||
|
||||
LambdaEsQueryWrapper<Document> wrapper1 = new LambdaEsQueryWrapper<>();
|
||||
wrapper1.matchPhrase(Document::getContent, "内容测试");
|
||||
@ -791,7 +780,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(63)
|
||||
public void testMatchAllQuery() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.matchAllQuery();
|
||||
@ -800,7 +789,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(64)
|
||||
public void testMatchPhrasePrefixQuery() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.matchPhrasePrefixQuery(Document::getContent, "测试");
|
||||
@ -809,7 +798,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(65)
|
||||
public void testMultiMatchQuery() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.multiMatchQuery("老汉", Document::getContent, Document::getCreator, Document::getCustomField);
|
||||
@ -824,7 +813,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(66)
|
||||
public void testQueryStringQuery() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.queryStringQuery("老汉");
|
||||
@ -833,7 +822,7 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(67)
|
||||
public void testPrefixQuery() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.prefixQuery(Document::getContent, "测试");
|
||||
@ -842,51 +831,51 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(68)
|
||||
public void testHighLight() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.match(Document::getContent, "测试")
|
||||
.match(Document::getCustomField,"字段");
|
||||
.match(Document::getCustomField, "字段");
|
||||
List<Document> documents = documentMapper.selectList(wrapper);
|
||||
Assertions.assertTrue(documents.get(0).getHighlightContent().contains("测试"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(69)
|
||||
public void testGeoBoundingBox() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
GeoLocation leftTop = new GeoLocation(41.187328D, 115.498353D);
|
||||
GeoLocation bottomRight = new GeoLocation(39.084509D, 117.610461D);
|
||||
GeoLocation leftTop = GeoLocation.of(a -> a.latlon(a1 -> a1.lat(41.187328D).lon(115.498353D)));
|
||||
GeoLocation bottomRight = GeoLocation.of(a -> a.latlon(a1 -> a1.lat(39.084509D).lon(117.610461D)));
|
||||
wrapper.geoBoundingBox(Document::getLocation, leftTop, bottomRight);
|
||||
List<Document> documents = documentMapper.selectList(wrapper);
|
||||
Assertions.assertEquals(4, documents.size());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(70)
|
||||
public void testGeoDistance() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
GeoLocation geoPoint = new GeoLocation(41.0, 116.0);
|
||||
wrapper.geoDistance(Document::getLocation, 168.8, DistanceUnit.KILOMETERS, geoPoint);
|
||||
GeoDistanceSortBuilder geoDistanceSortBuilder = SortBuilders.geoDistanceSort(FieldUtils.val(Document::getLocation), geoPoint)
|
||||
.unit(DistanceUnit.KILOMETERS)
|
||||
.geoDistance(GeoDistance.ARC)
|
||||
.order(SortOrder.Desc);
|
||||
|
||||
wrapper.sort(geoDistanceSortBuilder);
|
||||
GeoLocation geoPoint = GeoLocation.of(a -> a.latlon(a1 -> a1.lat(41.0).lon(116.0)));
|
||||
wrapper.geoDistance(Document::getLocation, 168.8, DistanceUnit.Kilometers, geoPoint);
|
||||
wrapper.sort(SortOptions.of(a -> a.geoDistance(b -> b
|
||||
.field(FieldUtils.val(Document::getLocation))
|
||||
.location(geoPoint)
|
||||
.unit(DistanceUnit.Kilometers)
|
||||
.distanceType(GeoDistanceType.Arc)
|
||||
.order(SortOrder.Desc)
|
||||
)));
|
||||
List<Document> documents = documentMapper.selectList(wrapper);
|
||||
Assertions.assertEquals(4, documents.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(71)
|
||||
public void testGeoPolygon() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
List<GeoLocation> geoPoints = new ArrayList<>();
|
||||
GeoLocation geoPoint = new GeoLocation(40.178012, 116.577188);
|
||||
GeoLocation geoPoint1 = new GeoLocation(40.169329, 116.586315);
|
||||
GeoLocation geoPoint2 = new GeoLocation(40.178288, 116.591813);
|
||||
GeoLocation geoPoint = GeoLocation.of(a -> a.latlon(a1 -> a1.lat(40.178012).lon(116.577188)));
|
||||
GeoLocation geoPoint1 = GeoLocation.of(a -> a.latlon(a1 -> a1.lat(40.169329).lon(116.586315)));
|
||||
GeoLocation geoPoint2 = GeoLocation.of(a -> a.latlon(a1 -> a1.lat(40.178288).lon(116.591813)));
|
||||
geoPoints.add(geoPoint);
|
||||
geoPoints.add(geoPoint1);
|
||||
geoPoints.add(geoPoint2);
|
||||
@ -896,17 +885,17 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(72)
|
||||
public void testGeoShape() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
Circle circle = new Circle(13, 14, 100);
|
||||
wrapper.geoShape(Document::getGeoLocation, circle, GeoShapeRelation.DISJOINT);
|
||||
wrapper.geoShape(Document::getGeoLocation, circle, GeoShapeRelation.Disjoint);
|
||||
List<Document> documents = documentMapper.selectList(wrapper);
|
||||
Assertions.assertEquals(22, documents.size());
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(73)
|
||||
public void testMultiFieldSelect() {
|
||||
// 药品 中文名叫葡萄糖酸钙口服溶液 英文名叫 Calcium Gluconate 汉语拼音为 putaotangsuangaikoufurongye
|
||||
// 用户可以通过模糊检索,例如输入 Calcium 或 葡萄糖 或 putaotang时对应药品均可以被检索到
|
||||
@ -921,16 +910,19 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(74)
|
||||
public void testVector() {
|
||||
// 向量查询, 查询条件构造
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("vector", new double[]{0.39684247970581055, 0.7687071561813354, 0.5145490765571594});
|
||||
String scriptCode = "cosineSimilarity(params.vector, 'vector') + 1.0";
|
||||
QueryBuilder queryBuilder = QueryBuilders.scriptScoreQuery(QueryBuilders.matchAllQuery(), new Script(ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, scriptCode, params));
|
||||
|
||||
Query query = Query.of(a -> a.scriptScore(b -> b
|
||||
.query(QueryBuilders.matchAll().build()._toQuery())
|
||||
.script(d -> d
|
||||
.lang(ScriptLanguage.Painless)
|
||||
.params("vector", JsonData.of(new double[]{0.39684247970581055, 0.7687071561813354, 0.5145490765571594}))
|
||||
.source("cosineSimilarity(params.vector, 'vector') + 1.0")
|
||||
)
|
||||
));
|
||||
SearchRequest.Builder searchSourceBuilder = new SearchRequest.Builder();
|
||||
searchSourceBuilder.query(queryBuilder);
|
||||
searchSourceBuilder.query(query);
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.setSearchBuilder(searchSourceBuilder);
|
||||
|
||||
@ -939,67 +931,75 @@ public class AllTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(6)
|
||||
@Order(75)
|
||||
public void testSetRequestOptions() {
|
||||
// 可设置自定义请求参数,覆盖默认配置, 解决报错 entity content is too long [168583249] for the configured buffer limit [104857600]
|
||||
RequestOptions.Builder options = RequestOptions.DEFAULT.toBuilder();
|
||||
options.setHttpAsyncResponseConsumerFactory(
|
||||
RequestOptions.Builder builder = RequestOptions.DEFAULT.toBuilder();
|
||||
builder.setHttpAsyncResponseConsumerFactory(
|
||||
new HttpAsyncResponseConsumerFactory.HeapBufferedResponseConsumerFactory(4 * 104857600));
|
||||
final RequestOptions requestOptions = options.build();
|
||||
Boolean success = documentMapper.setRequestOptions(requestOptions);
|
||||
Boolean success = documentMapper.setRequestOptions(new RestClientOptions(builder.build(), true));
|
||||
Assertions.assertTrue(success);
|
||||
}
|
||||
|
||||
// 4.删除
|
||||
@Test
|
||||
@Order(7)
|
||||
public void testDeleteById() {
|
||||
int count = documentMapper.deleteById("1");
|
||||
Assertions.assertEquals(1, count);
|
||||
}
|
||||
// // 4.删除
|
||||
// @Test
|
||||
// @Order(76)
|
||||
// public void testDeleteById() {
|
||||
// int count = documentMapper.deleteById("1");
|
||||
// Assertions.assertEquals(1, count);
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// @Order(77)
|
||||
// public void testDeleteBatchIds() {
|
||||
// List<String> idList = Arrays.asList("2", "3", "4");
|
||||
// int count = documentMapper.deleteBatchIds(idList);
|
||||
// Assertions.assertEquals(3, count);
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// @Order(78)
|
||||
// public void testDeleteByWrapper() {
|
||||
// LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
// wrapper.match(Document::getCreator, "老汉");
|
||||
//
|
||||
// int count = documentMapper.delete(wrapper);
|
||||
// Assertions.assertEquals(18, count);
|
||||
// }
|
||||
//
|
||||
// @Test
|
||||
// @Order(80)
|
||||
// public void testDeleteIndex() {
|
||||
// boolean deleted = documentMapper.deleteIndex(EntityInfoHelper.getEntityInfo(Document.class).getIndexName());
|
||||
// boolean lockDeleted = documentMapper.deleteIndex(BaseEsConstants.LOCK_INDEX);
|
||||
// Assertions.assertTrue(deleted);
|
||||
// Assertions.assertTrue(lockDeleted);
|
||||
// }
|
||||
|
||||
@Test
|
||||
@Order(8)
|
||||
public void testDeleteBatchIds() {
|
||||
List<String> idList = Arrays.asList("2", "3", "4");
|
||||
int count = documentMapper.deleteBatchIds(idList);
|
||||
Assertions.assertEquals(3, count);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(9)
|
||||
public void testDeleteByWrapper() {
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.match(Document::getCreator, "老汉");
|
||||
|
||||
int count = documentMapper.delete(wrapper);
|
||||
Assertions.assertEquals(18, count);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(10)
|
||||
public void testDeleteIndex() {
|
||||
boolean deleted = documentMapper.deleteIndex(EntityInfoHelper.getEntityInfo(Document.class).getIndexName());
|
||||
boolean lockDeleted = documentMapper.deleteIndex(BaseEsConstants.LOCK_INDEX);
|
||||
Assertions.assertTrue(deleted);
|
||||
Assertions.assertTrue(lockDeleted);
|
||||
}
|
||||
|
||||
@Test
|
||||
@Order(9)
|
||||
@Order(79)
|
||||
public void testComplex() {
|
||||
// SQL写法
|
||||
// where business_type = 1 and (state = 9 or (state = 8 and bidding_sign = 1)) or (business_type = 2 and state in (2,3))
|
||||
|
||||
// ElasticsearchClient写法
|
||||
List<Integer> values = Arrays.asList(2, 3);
|
||||
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
||||
boolQueryBuilder.must(QueryBuilders.termQuery("business_type", 1));
|
||||
boolQueryBuilder.must(QueryBuilders.boolQuery().must(QueryBuilders.termQuery("state", 9))
|
||||
.should(QueryBuilders.boolQuery().must(QueryBuilders.termQuery("state", 8)).must(QueryBuilders.termQuery("bidding_sign", 1))));
|
||||
boolQueryBuilder.should(QueryBuilders.boolQuery().must(QueryBuilders.termQuery("business_type", 2)).must(QueryBuilders.termsQuery("state", values)));
|
||||
List<FieldValue> values = Arrays.asList(WrapperProcessor.fieldValue(2), WrapperProcessor.fieldValue(3));
|
||||
BoolQuery.Builder boolQueryBuilder = QueryBuilders.bool()
|
||||
.must(a -> a.term(b -> b.field("business_type").value(1)))
|
||||
.must(a -> a.bool(b -> b
|
||||
.must(c -> c.term(d -> d.field("state").value(9)))
|
||||
.should(c -> c.bool(d -> d
|
||||
.must(f -> f.term(e -> e.field("state").value(8)))
|
||||
.must(f -> f.term(e -> e.field("bidding_sign").value(1)))
|
||||
))
|
||||
))
|
||||
.should(a -> a.bool(c -> c
|
||||
.must(d -> d.term(e -> e.field("business_type").value(2)))
|
||||
.must(d -> d.terms(e -> e.field("state").terms(f -> f.value(values))))
|
||||
));
|
||||
|
||||
System.out.println(boolQueryBuilder);
|
||||
Query query = boolQueryBuilder.build()._toQuery();
|
||||
System.out.println(query.toString());
|
||||
System.out.println("--------------------");
|
||||
|
||||
// MP及EE写法
|
||||
@ -1007,7 +1007,12 @@ public class AllTest {
|
||||
wrapper.eq("business_type", 1)
|
||||
.and(a -> a.eq("state", 9).or(b -> b.eq("state", 8).eq("bidding_sign", 1)))
|
||||
.or(i -> i.eq("business_type", 2).in("state", 2, 3));
|
||||
documentMapper.selectList(wrapper);
|
||||
SearchRequest.Builder searchBuilder = documentMapper.getSearchBuilder(wrapper);
|
||||
Query qry = searchBuilder.build().query();
|
||||
if (qry != null) {
|
||||
System.out.println(qry);
|
||||
}
|
||||
List<Document> documents = documentMapper.selectList(wrapper);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -1,17 +1,15 @@
|
||||
package org.dromara.easyes.test.compare;
|
||||
|
||||
import co.elastic.clients.elasticsearch.ElasticsearchClient;
|
||||
import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
|
||||
import co.elastic.clients.elasticsearch.core.SearchRequest;
|
||||
import co.elastic.clients.elasticsearch.core.SearchResponse;
|
||||
import co.elastic.clients.transport.rest_client.RestClientOptions;
|
||||
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
|
||||
import org.dromara.easyes.test.TestEasyEsApplication;
|
||||
import org.dromara.easyes.test.entity.Document;
|
||||
import org.dromara.easyes.test.mapper.DocumentMapper;
|
||||
import org.elasticsearch.action.search.SearchRequest;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.client.RequestOptions;
|
||||
import org.elasticsearch.index.query.BoolQueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.index.query.TermQueryBuilder;
|
||||
import org.elasticsearch.index.query.TermsQueryBuilder;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@ -32,24 +30,25 @@ public class CompareTest {
|
||||
@Resource
|
||||
private DocumentMapper documentMapper;
|
||||
@Autowired
|
||||
private ElasticsearchClient restHighLevelClient;
|
||||
private ElasticsearchClient client;
|
||||
|
||||
public static RestClientOptions options = new RestClientOptions(RequestOptions.DEFAULT, true);
|
||||
|
||||
@Test
|
||||
public void testCompare() {
|
||||
// 需求:查询出文档标题为 "中国功夫"且作者为"老汉"的所有文档
|
||||
// 传统方式, 直接用ElasticsearchClient进行查询 需要11行代码,还不包含解析代码
|
||||
String indexName = "document";
|
||||
SearchRequest searchRequest = new SearchRequest(indexName);
|
||||
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
||||
TermQueryBuilder titleTerm = QueryBuilders.termQuery("title", "中国功夫");
|
||||
TermsQueryBuilder creatorTerm = QueryBuilders.termsQuery("creator", "老汉");
|
||||
boolQueryBuilder.must(titleTerm);
|
||||
boolQueryBuilder.must(creatorTerm);
|
||||
SearchRequest.Builder searchSourceBuilder = new SearchRequest.Builder();
|
||||
searchSourceBuilder.query(boolQueryBuilder);
|
||||
searchRequest.source(searchSourceBuilder);
|
||||
SearchRequest searchRequest = SearchRequest.of(a -> a
|
||||
.index(indexName)
|
||||
.query(QueryBuilders.bool()
|
||||
.must(x -> x.term(b -> b.field("title").value("中国功夫")))
|
||||
.must(x -> x.term(b -> b.field("creator").value("老汉")))
|
||||
.build()._toQuery()
|
||||
)
|
||||
);
|
||||
try {
|
||||
SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
|
||||
SearchResponse<Document> searchResponse = client.withTransportOptions(options).search(searchRequest, Document.class);
|
||||
// 然后从searchResponse中通过各种方式解析出DocumentList 省略这些代码...
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
|
||||
@ -1,12 +1,13 @@
|
||||
package org.dromara.easyes.test.geo;
|
||||
|
||||
import co.elastic.clients.elasticsearch._types.DistanceUnit;
|
||||
import co.elastic.clients.elasticsearch._types.GeoLocation;
|
||||
import co.elastic.clients.elasticsearch._types.GeoShapeRelation;
|
||||
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
|
||||
import org.dromara.easyes.core.toolkit.GeoUtils;
|
||||
import org.dromara.easyes.test.TestEasyEsApplication;
|
||||
import org.dromara.easyes.test.entity.Document;
|
||||
import org.dromara.easyes.test.mapper.DocumentMapper;
|
||||
import org.elasticsearch.common.geo.GeoShapeRelation;
|
||||
import org.elasticsearch.common.unit.DistanceUnit;
|
||||
import org.elasticsearch.geometry.Circle;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
@ -34,9 +35,9 @@ public class GeoTest {
|
||||
// 查询位于下面左上点和右下点坐标构成的长方形内的所有点
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
// 左上点坐标
|
||||
GeoLocation leftTop = new GeoLocation(41.187328D, 115.498353D);
|
||||
GeoLocation leftTop = GeoUtils.create(41.187328D, 115.498353D);
|
||||
// 右下点坐标
|
||||
GeoLocation bottomRight = new GeoLocation(39.084509D, 117.610461D);
|
||||
GeoLocation bottomRight = GeoUtils.create(39.084509D, 117.610461D);
|
||||
wrapper.geoBoundingBox(Document::getLocation, leftTop, bottomRight);
|
||||
// 查不在此长方形内的所有点
|
||||
// wrapper.notInGeoBoundingBox(Document::getLocation, leftTop, bottomRight);
|
||||
@ -48,9 +49,9 @@ public class GeoTest {
|
||||
public void testGeoDistance() {
|
||||
// 查询以纬度为41.0,经度为115.0为圆心,半径168.8公里内的所有点
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.geoDistance(Document::getLocation, 168.8, DistanceUnit.KILOMETERS, new GeoLocation(41.0, 116.0));
|
||||
wrapper.geoDistance(Document::getLocation, 168.8, DistanceUnit.Kilometers, GeoUtils.create(41.0, 116.0));
|
||||
// 上面语法也可以写成下面这几种形式,效果是一样的,兼容不同用户习惯而已:
|
||||
// wrapper.geoDistance(Document::getLocation,"1.5km",new GeoLocation(41.0,115.0));
|
||||
// wrapper.geoDistance(Document::getLocation,"1.5km",GeoUtils.create(41.0,115.0));
|
||||
// wrapper.geoDistance(Document::getLocation, "1.5km", "41.0,115.0");
|
||||
|
||||
List<Document> documents = documentMapper.selectList(wrapper);
|
||||
@ -62,9 +63,9 @@ public class GeoTest {
|
||||
// 查询以给定点列表构成的不规则图形内的所有点,点数至少为3个
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
List<GeoLocation> geoPoints = new ArrayList<>();
|
||||
GeoLocation geoPoint = new GeoLocation(40.178012, 116.577188);
|
||||
GeoLocation geoPoint1 = new GeoLocation(40.169329, 116.586315);
|
||||
GeoLocation geoPoint2 = new GeoLocation(40.178288, 116.591813);
|
||||
GeoLocation geoPoint = GeoUtils.create(40.178012, 116.577188);
|
||||
GeoLocation geoPoint1 = GeoUtils.create(40.169329, 116.586315);
|
||||
GeoLocation geoPoint2 = GeoUtils.create(40.178288, 116.591813);
|
||||
geoPoints.add(geoPoint);
|
||||
geoPoints.add(geoPoint1);
|
||||
geoPoints.add(geoPoint2);
|
||||
@ -98,7 +99,7 @@ public class GeoTest {
|
||||
// 这里以圆形为例演示,其中x,y为圆心坐标,r为半径. 其它图形请读者自行演示,篇幅原因不一一演示了
|
||||
Circle circle = new Circle(13, 14, 100);
|
||||
// shapeRelation支持多种,如果不传则默认为within
|
||||
wrapper.geoShape(Document::getGeoLocation, circle, GeoShapeRelation.INTERSECTS);
|
||||
wrapper.geoShape(Document::getGeoLocation, circle, GeoShapeRelation.Intersects);
|
||||
List<Document> documents = documentMapper.selectList(wrapper);
|
||||
System.out.println(documents);
|
||||
}
|
||||
@ -112,7 +113,7 @@ public class GeoTest {
|
||||
centerDoc.setEsId("1");
|
||||
centerDoc.setTitle("长沙站");
|
||||
centerDoc.setContent("长沙站臭豆腐店");
|
||||
GeoLocation center = new GeoLocation(28.194124244622135, 113.01327520919799);
|
||||
GeoLocation center = GeoUtils.create(28.194124244622135, 113.01327520919799);
|
||||
centerDoc.setLocation(center.toString());
|
||||
|
||||
// 长沙火车站
|
||||
@ -120,7 +121,7 @@ public class GeoTest {
|
||||
document1.setEsId("2");
|
||||
document1.setTitle("长沙火车站");
|
||||
document1.setContent("长沙火车站臭豆腐店");
|
||||
GeoLocation geoPoint1 = new GeoLocation(28.193708185086585, 113.01100069595336);
|
||||
GeoLocation geoPoint1 = GeoUtils.create(28.193708185086585, 113.01100069595336);
|
||||
document1.setLocation(geoPoint1.toString());
|
||||
|
||||
// 长沙站南广场
|
||||
@ -128,7 +129,7 @@ public class GeoTest {
|
||||
document2.setEsId("3");
|
||||
document2.setTitle("长沙站南广场");
|
||||
document2.setContent("长沙站南广场臭豆腐店");
|
||||
GeoLocation geoPoint2 = new GeoLocation(28.192195227668382, 113.01173025680541);
|
||||
GeoLocation geoPoint2 = GeoUtils.create(28.192195227668382, 113.01173025680541);
|
||||
document2.setLocation(geoPoint2.toString());
|
||||
|
||||
|
||||
@ -137,7 +138,7 @@ public class GeoTest {
|
||||
document3.setEsId("4");
|
||||
document3.setTitle("长沙市中医院");
|
||||
document3.setContent("长沙市中医院臭豆腐店");
|
||||
GeoLocation geoPoint3 = new GeoLocation(28.193367771534916, 113.00911242080687);
|
||||
GeoLocation geoPoint3 = GeoUtils.create(28.193367771534916, 113.00911242080687);
|
||||
document3.setLocation(geoPoint3.toString());
|
||||
|
||||
// 阿波罗商业广场
|
||||
@ -145,7 +146,7 @@ public class GeoTest {
|
||||
document4.setEsId("5");
|
||||
document4.setTitle("阿波罗商业广场");
|
||||
document4.setContent("阿波罗商业广场臭豆腐店");
|
||||
GeoLocation geoPoint4 = new GeoLocation(28.196582745180983, 113.00962740493773);
|
||||
GeoLocation geoPoint4 = GeoUtils.create(28.196582745180983, 113.00962740493773);
|
||||
document4.setLocation(geoPoint4.toString());
|
||||
|
||||
// 朝阳一村
|
||||
@ -153,7 +154,7 @@ public class GeoTest {
|
||||
document5.setEsId("6");
|
||||
document5.setTitle("朝阳一村");
|
||||
document5.setContent("朝阳一村臭豆腐店");
|
||||
GeoLocation geoPoint5 = new GeoLocation(28.188980122051586, 113.01177317214965);
|
||||
GeoLocation geoPoint5 = GeoUtils.create(28.188980122051586, 113.01177317214965);
|
||||
document5.setLocation(geoPoint5.toString());
|
||||
|
||||
// 铭威大厦
|
||||
@ -161,7 +162,7 @@ public class GeoTest {
|
||||
document6.setEsId("7");
|
||||
document6.setTitle("铭威大厦");
|
||||
document6.setContent("铭威大厦臭豆腐店");
|
||||
GeoLocation geoPoint6 = new GeoLocation(28.1905309497745, 113.01825338912963);
|
||||
GeoLocation geoPoint6 = GeoUtils.create(28.1905309497745, 113.01825338912963);
|
||||
document6.setLocation(geoPoint6.toString());
|
||||
|
||||
// 袁家岭
|
||||
@ -169,7 +170,7 @@ public class GeoTest {
|
||||
document7.setEsId("8");
|
||||
document7.setTitle("袁家岭");
|
||||
document7.setContent("袁家岭豆腐店");
|
||||
GeoLocation geoPoint7 = new GeoLocation(28.19450247915946, 113.00070101333617);
|
||||
GeoLocation geoPoint7 = GeoUtils.create(28.19450247915946, 113.00070101333617);
|
||||
document7.setLocation(geoPoint7.toString());
|
||||
|
||||
List<Document> documents = Arrays.asList(centerDoc, document1, document2, document3, document4, document5, document6, document7);
|
||||
|
||||
@ -1,8 +1,11 @@
|
||||
package org.dromara.easyes.test.high;
|
||||
|
||||
|
||||
import co.elastic.clients.elasticsearch._types.FieldValue;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import co.elastic.clients.elasticsearch._types.ScriptSortType;
|
||||
import co.elastic.clients.elasticsearch._types.SortOptions;
|
||||
import co.elastic.clients.elasticsearch._types.SortOrder;
|
||||
import co.elastic.clients.elasticsearch.core.SearchResponse;
|
||||
import org.dromara.easyes.common.utils.jackson.JsonUtils;
|
||||
import org.dromara.easyes.core.biz.EsPageInfo;
|
||||
import org.dromara.easyes.core.biz.OrderByParam;
|
||||
import org.dromara.easyes.core.biz.SAPageInfo;
|
||||
@ -11,10 +14,6 @@ import org.dromara.easyes.core.kernel.EsWrappers;
|
||||
import org.dromara.easyes.test.TestEasyEsApplication;
|
||||
import org.dromara.easyes.test.entity.Document;
|
||||
import org.dromara.easyes.test.mapper.DocumentMapper;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.search.sort.ScriptSortBuilder;
|
||||
import org.elasticsearch.search.sort.SortOrder;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
@ -157,7 +156,7 @@ public class HighTest {
|
||||
public void testOrderByParams() {
|
||||
// 此处假设此参数由前端通过xxQuery类传入,排序根据标题降序,根据内容升序
|
||||
String jsonParam = "[{\"order\":\"title\",\"sort\":\"DESC\"},{\"order\":\"creator\",\"sort\":\"ASC\"}]";
|
||||
List<OrderByParam> orderByParams = JSON.parseArray(jsonParam, OrderByParam.class);
|
||||
List<OrderByParam> orderByParams = JsonUtils.toList(jsonParam, OrderByParam.class);
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.match(Document::getContent, "技术")
|
||||
.orderBy(orderByParams);
|
||||
@ -170,9 +169,10 @@ public class HighTest {
|
||||
// 测试复杂排序,SortBuilder的子类非常多,这里仅演示一种, 比如有用户提出需要随机获取数据 0.9.7+
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.match(Document::getContent, "技术");
|
||||
Script script = new Script("Math.random()");
|
||||
ScriptSortBuilder scriptSortBuilder = new ScriptSortBuilder(script, ScriptSortBuilder.ScriptSortType.NUMBER);
|
||||
wrapper.sort(scriptSortBuilder);
|
||||
wrapper.sort(SortOptions.of(a -> a.script(b -> b
|
||||
.type(ScriptSortType.Number)
|
||||
.script(c -> c.source("Math.random()"))
|
||||
)));
|
||||
List<Document> documents = documentMapper.selectList(wrapper);
|
||||
System.out.println(documents);
|
||||
}
|
||||
|
||||
@ -1,14 +1,13 @@
|
||||
package org.dromara.easyes.test.index;
|
||||
|
||||
|
||||
|
||||
import co.elastic.clients.elasticsearch._types.mapping.TypeMapping;
|
||||
import co.elastic.clients.elasticsearch.indices.GetIndexResponse;
|
||||
import org.dromara.easyes.annotation.rely.Analyzer;
|
||||
import org.dromara.easyes.annotation.rely.FieldType;
|
||||
import org.dromara.easyes.core.conditions.index.LambdaEsIndexWrapper;
|
||||
import org.dromara.easyes.test.TestEasyEsApplication;
|
||||
import org.dromara.easyes.test.entity.Document;
|
||||
import org.dromara.easyes.test.mapper.DocumentMapper;
|
||||
import org.elasticsearch.client.indices.GetIndexResponse;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
@ -17,8 +16,6 @@ import org.springframework.boot.test.context.SpringBootTest;
|
||||
import javax.annotation.Resource;
|
||||
import java.time.LocalDate;
|
||||
import java.time.format.DateTimeFormatter;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 不了解Es索引概念的建议先去了解 懒汉可以简单理解为MySQL中的一张表
|
||||
@ -54,7 +51,7 @@ public class IndexTest {
|
||||
wrapper.indexName(Document.class.getSimpleName().toLowerCase());
|
||||
|
||||
// 此处将文章标题映射为keyword类型(不支持分词),文档内容映射为text类型(支持分词查询)
|
||||
wrapper.mapping(Document::getTitle, FieldType.KEYWORD, 2.0f)
|
||||
wrapper.mapping(Document::getTitle, FieldType.KEYWORD)
|
||||
.mapping(Document::getLocation, FieldType.GEO_POINT)
|
||||
.mapping(Document::getGeoLocation, FieldType.GEO_SHAPE)
|
||||
.mapping(Document::getContent, FieldType.TEXT, Analyzer.IK_SMART, Analyzer.IK_MAX_WORD);
|
||||
@ -89,7 +86,7 @@ public class IndexTest {
|
||||
public void testGetIndex() {
|
||||
GetIndexResponse indexResponse = documentMapper.getIndex();
|
||||
// 这里打印下索引结构信息 其它分片等信息皆可从indexResponse中取
|
||||
indexResponse.getMappings().forEach((k, v) -> System.out.println(v.getSourceAsMap()));
|
||||
indexResponse.result().forEach((k, v) -> System.out.println(k + ":" + v));
|
||||
}
|
||||
|
||||
@Test
|
||||
@ -119,13 +116,9 @@ public class IndexTest {
|
||||
// 演示通过自定义map创建索引,最为灵活,若我提供的创建索引API不能满足时可用此方法
|
||||
LambdaEsIndexWrapper<Document> wrapper = new LambdaEsIndexWrapper<>();
|
||||
wrapper.indexName(Document.class.getSimpleName().toLowerCase());
|
||||
wrapper.settings(3, 2, 100);
|
||||
Map<String, Object> map = new HashMap<>();
|
||||
Map<String, Object> prop = new HashMap<>();
|
||||
Map<String, String> field = new HashMap<>();
|
||||
field.put("type", FieldType.KEYWORD.getType());
|
||||
prop.put("this_is_field", field);
|
||||
map.put("properties", prop);
|
||||
wrapper.settings(1, 1, 10000);
|
||||
TypeMapping.Builder map = new TypeMapping.Builder();
|
||||
map.properties("this_is_field", a -> a.keyword(b -> b));
|
||||
wrapper.mapping(map);
|
||||
boolean isOk = documentMapper.createIndex(wrapper);
|
||||
Assertions.assertTrue(isOk);
|
||||
|
||||
@ -1,12 +1,12 @@
|
||||
package org.dromara.easyes.test.interceptor;
|
||||
|
||||
import co.elastic.clients.elasticsearch.core.SearchRequest;
|
||||
import org.dromara.easyes.annotation.Intercepts;
|
||||
import org.dromara.easyes.annotation.Signature;
|
||||
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
|
||||
import org.dromara.easyes.core.kernel.BaseEsMapper;
|
||||
import org.dromara.easyes.extension.context.Interceptor;
|
||||
import org.dromara.easyes.extension.context.Invocation;
|
||||
import org.elasticsearch.action.search.SearchRequest;
|
||||
import org.elasticsearch.client.RequestOptions;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
|
||||
@ -1,21 +1,16 @@
|
||||
package org.dromara.easyes.test.mix;
|
||||
|
||||
|
||||
import co.elastic.clients.elasticsearch._types.ScriptSortType;
|
||||
import co.elastic.clients.elasticsearch._types.SortOptions;
|
||||
import co.elastic.clients.elasticsearch._types.SortOrder;
|
||||
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
|
||||
import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
|
||||
import co.elastic.clients.elasticsearch.core.SearchRequest;
|
||||
import co.elastic.clients.elasticsearch.core.SearchResponse;
|
||||
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
|
||||
import org.dromara.easyes.test.TestEasyEsApplication;
|
||||
import org.dromara.easyes.test.entity.Document;
|
||||
import org.dromara.easyes.test.mapper.DocumentMapper;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.core.TimeValue;
|
||||
import org.elasticsearch.index.query.QueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.search.aggregations.AggregationBuilder;
|
||||
import org.elasticsearch.search.aggregations.AggregationBuilders;
|
||||
import org.elasticsearch.search.sort.ScriptSortBuilder;
|
||||
import org.elasticsearch.search.sort.SortBuilders;
|
||||
import org.elasticsearch.search.sort.SortOrder;
|
||||
import org.junit.jupiter.api.MethodOrderer;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.TestMethodOrder;
|
||||
@ -56,8 +51,12 @@ public class MixTest {
|
||||
// 查询标题为老汉,内容匹配 推*,且最小匹配度不低于80%的数据
|
||||
// 当前我们提供的开箱即用match并不支持设置最小匹配度,此时就可以自己去构造一个matchQueryBuilder来实现
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
QueryBuilder queryBuilder = QueryBuilders.matchQuery("content", "推*").minimumShouldMatch("80%");
|
||||
wrapper.eq(Document::getTitle, "老汉").mix(queryBuilder);
|
||||
wrapper.eq(Document::getTitle, "老汉")
|
||||
.mix(Query.of(a -> a.match(b -> b
|
||||
.field("content")
|
||||
.query("推*")
|
||||
.minimumShouldMatch("80%"))
|
||||
));
|
||||
List<Document> documents = documentMapper.selectList(wrapper);
|
||||
System.out.println(documents);
|
||||
}
|
||||
@ -69,7 +68,11 @@ public class MixTest {
|
||||
public void testMix1() {
|
||||
// ElasticsearchClient原生语法
|
||||
SearchRequest.Builder searchSourceBuilder = new SearchRequest.Builder();
|
||||
searchSourceBuilder.query(QueryBuilders.matchQuery("content", "推*").minimumShouldMatch("80%"));
|
||||
searchSourceBuilder.query(Query.of(a -> a.match(b -> b
|
||||
.field("content")
|
||||
.query("推*")
|
||||
.minimumShouldMatch("80%"))
|
||||
));
|
||||
|
||||
// 仅利用EE查询并解析数据功能
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
@ -90,11 +93,12 @@ public class MixTest {
|
||||
.match(Document::getContent, "推*");
|
||||
|
||||
// ElasticsearchClient原生语法
|
||||
Script script = new Script("doc['star_num'].value");
|
||||
ScriptSortBuilder scriptSortBuilder = SortBuilders.scriptSort(script, ScriptSortBuilder.ScriptSortType.NUMBER).order(SortOrder.Desc);
|
||||
|
||||
// 利用EE查询并解析数据
|
||||
wrapper.sort(scriptSortBuilder);
|
||||
wrapper.sort(SortOptions.of(a -> a.script(b -> b
|
||||
.type(ScriptSortType.Number)
|
||||
.order(SortOrder.Desc)
|
||||
.script(c -> c.source("doc['star_num'].value"))
|
||||
)));
|
||||
List<Document> documents = documentMapper.selectList(wrapper);
|
||||
System.out.println(documents);
|
||||
}
|
||||
@ -111,7 +115,7 @@ public class MixTest {
|
||||
SearchRequest.Builder searchSourceBuilder = documentMapper.getSearchBuilder(wrapper);
|
||||
|
||||
// 追加或者设置一些SearchSourceBuilder支持但EE暂不支持的参数 不建议追加query参数,因为如果追加query参数会直接覆盖上面EE已经帮你生成好的query,以最后set的query为准
|
||||
searchSourceBuilder.timeout(TimeValue.timeValueSeconds(3L));
|
||||
searchSourceBuilder.timeout("3s");
|
||||
wrapper.setSearchBuilder(searchSourceBuilder);
|
||||
List<Document> documents = documentMapper.selectList(wrapper);
|
||||
System.out.println(documents);
|
||||
@ -130,11 +134,9 @@ public class MixTest {
|
||||
SearchRequest.Builder searchSourceBuilder = documentMapper.getSearchBuilder(wrapper);
|
||||
|
||||
// ElasticsearchClient原生语法
|
||||
AggregationBuilder aggregation = AggregationBuilders.terms("titleAgg")
|
||||
.field("title");
|
||||
searchSourceBuilder.aggregation(aggregation);
|
||||
searchSourceBuilder.aggregations("titleAgg", a -> a.terms(b -> b.field("title")));
|
||||
wrapper.setSearchBuilder(searchSourceBuilder);
|
||||
SearchResponse searchResponse = documentMapper.search(wrapper);
|
||||
SearchResponse<Document> searchResponse = documentMapper.search(wrapper);
|
||||
// tip: 聚合后的信息是动态的,框架无法解析,需要用户根据聚合器类型自行从桶中解析,参考ElasticsearchClient官方Aggregation解析文档
|
||||
}
|
||||
|
||||
@ -151,7 +153,7 @@ public class MixTest {
|
||||
SearchRequest.Builder searchSourceBuilder = documentMapper.getSearchBuilder(wrapper);
|
||||
|
||||
// 用户又想在上面的基础上,再追加一些个性化的查询参数进去 但实际上此时执行查询时,查询条件仅仅是最后设置的title=隔壁老王,前面的老汉推*会被覆盖
|
||||
searchSourceBuilder.query(QueryBuilders.matchQuery("title", "隔壁老王"));
|
||||
searchSourceBuilder.query(QueryBuilders.match().field("title").query("隔壁老王").build()._toQuery());
|
||||
wrapper.setSearchBuilder(searchSourceBuilder);
|
||||
List<Document> documents = documentMapper.selectList(wrapper);
|
||||
System.out.println(documents);
|
||||
|
||||
@ -1,24 +1,14 @@
|
||||
package org.dromara.easyes.test.performance;
|
||||
|
||||
import co.elastic.clients.elasticsearch.ElasticsearchClient;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import co.elastic.clients.elasticsearch.core.*;
|
||||
import co.elastic.clients.elasticsearch.core.search.Hit;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.time.StopWatch;
|
||||
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
|
||||
import org.dromara.easyes.test.TestEasyEsApplication;
|
||||
import org.dromara.easyes.test.entity.Document;
|
||||
import org.dromara.easyes.test.mapper.DocumentMapper;
|
||||
import org.elasticsearch.action.bulk.BulkRequest;
|
||||
import org.elasticsearch.action.delete.DeleteRequest;
|
||||
import org.elasticsearch.action.index.IndexRequest;
|
||||
import org.elasticsearch.action.search.SearchRequest;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.action.update.UpdateRequest;
|
||||
import org.elasticsearch.client.RequestOptions;
|
||||
import org.elasticsearch.index.query.BoolQueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
|
||||
import org.elasticsearch.xcontent.XContentType;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
@ -26,10 +16,8 @@ import org.springframework.boot.test.context.SpringBootTest;
|
||||
import javax.annotation.Resource;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.dromara.easyes.common.constants.BaseEsConstants.DEFAULT_SIZE;
|
||||
|
||||
@ -73,36 +61,37 @@ public class PerformanceTest {
|
||||
public void testSelectByElasticsearchClient() {
|
||||
// 构建查询条件
|
||||
StopWatch stopwatch = StopWatch.createStarted();
|
||||
SearchRequest.Builder builder = new SearchRequest.Builder();
|
||||
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
|
||||
boolQueryBuilder.must(QueryBuilders.matchQuery("title", "茶叶"));
|
||||
boolQueryBuilder.must(QueryBuilders.matchQuery("content", "茶叶"));
|
||||
builder.query(boolQueryBuilder);
|
||||
HighlightBuilder highlightBuilder = new HighlightBuilder();
|
||||
highlightBuilder.field("title");
|
||||
highlightBuilder.preTags("<em>");
|
||||
highlightBuilder.postTags("</em>");
|
||||
builder.highlighter(highlightBuilder);
|
||||
builder.size(DEFAULT_SIZE);
|
||||
SearchRequest request = new SearchRequest("kiplatform_library").source(builder);
|
||||
SearchResponse search = null;
|
||||
try {
|
||||
search = client.search(request, RequestOptions.DEFAULT);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
List<Document> documents = Arrays.stream(search.getHits().getHits())
|
||||
.map(hit -> JSON.parseObject(hit.getSourceAsString(), Document.class))
|
||||
.collect(Collectors.toList());
|
||||
SearchRequest.Builder builder = new SearchRequest.Builder()
|
||||
.index("kiplatform_library")
|
||||
.query(a -> a.bool(b -> b
|
||||
.must(c -> c.match(d -> d.field("title").field("茶叶")))
|
||||
.must(c -> c.match(d -> d.field("content").field("茶叶"))
|
||||
)
|
||||
))
|
||||
.highlight(a -> a
|
||||
.fields("title", b -> b.preTags("<em>").postTags("</em>"))
|
||||
)
|
||||
.size(DEFAULT_SIZE);
|
||||
|
||||
SearchRequest countRequest = new SearchRequest("kiplatform_library");
|
||||
SearchResponse countResponse = null;
|
||||
SearchResponse<Document> search = null;
|
||||
try {
|
||||
countResponse = client.search(countRequest, RequestOptions.DEFAULT);
|
||||
search = client.search(builder.build(), Document.class);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Long count = countResponse.getHits().getTotalHits().value;
|
||||
|
||||
List<Document> documents = search.hits().hits().stream()
|
||||
.map(Hit::source)
|
||||
.toList();
|
||||
|
||||
SearchRequest countRequest = SearchRequest.of(a -> a.index("kiplatform_library"));
|
||||
SearchResponse<Document> countResponse = null;
|
||||
try {
|
||||
countResponse = client.search(countRequest, Document.class);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Long count = countResponse.hits().total().value();
|
||||
stopwatch.stop();
|
||||
log.info("本次查询从:{}条数据中共命中:{}条数据,耗时总计:{}毫秒", count, documents.size(), stopwatch.getTime(TimeUnit.MILLISECONDS));
|
||||
// 本次查询从:5135条数据中共命中:15条数据,耗时总计:428毫秒 多次测试 均值维持430毫秒左右
|
||||
@ -129,18 +118,16 @@ public class PerformanceTest {
|
||||
@Test
|
||||
public void testInsertByElasticsearchClient() {
|
||||
StopWatch stopWatch = StopWatch.createStarted();
|
||||
BulkRequest bulkRequest = new BulkRequest();
|
||||
BulkRequest.Builder bulkRequest = new BulkRequest.Builder();
|
||||
int total = 100;
|
||||
for (int i = 0; i < total; i++) {
|
||||
Document document = new Document();
|
||||
document.setTitle("测试新增性能" + i);
|
||||
document.setContent("测试新增性能内容" + i);
|
||||
IndexRequest indexRequest = new IndexRequest("document");
|
||||
indexRequest.source(JSON.toJSON(document), XContentType.JSON);
|
||||
bulkRequest.add(indexRequest);
|
||||
bulkRequest.operations(a -> a.index(b -> b.index("document").document(document)));
|
||||
}
|
||||
try {
|
||||
client.bulk(bulkRequest, RequestOptions.DEFAULT);
|
||||
client.bulk(bulkRequest.build());
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -169,10 +156,12 @@ public class PerformanceTest {
|
||||
Document document = new Document();
|
||||
document.setTitle("哈哈哈");
|
||||
document.setContent("嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿");
|
||||
UpdateRequest updateRequest = new UpdateRequest("document", "RWF0SH8B0E2Rzy0qcFBz");
|
||||
updateRequest.doc(JSON.toJSONString(document), XContentType.JSON);
|
||||
UpdateRequest<Document, Document> updateRequest = new UpdateRequest.Builder<Document, Document>()
|
||||
.index("document")
|
||||
.doc(document)
|
||||
.build();
|
||||
try {
|
||||
client.update(updateRequest, RequestOptions.DEFAULT);
|
||||
client.update(updateRequest, Document.class);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
@ -194,9 +183,12 @@ public class PerformanceTest {
|
||||
@Test
|
||||
public void testDeleteByElasticsearchClient() {
|
||||
StopWatch stopWatch = StopWatch.createStarted();
|
||||
DeleteRequest deleteRequest = new DeleteRequest("document", "RWF0SH8B0E2Rzy0qcFBz");
|
||||
DeleteRequest deleteRequest = new DeleteRequest.Builder()
|
||||
.index("document")
|
||||
.id("TWF0SH8B0E2Rzy0qcFBz")
|
||||
.build();
|
||||
try {
|
||||
client.delete(deleteRequest, RequestOptions.DEFAULT);
|
||||
client.delete(deleteRequest);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
@ -1,24 +1,22 @@
|
||||
package org.dromara.easyes.test.vector;
|
||||
|
||||
|
||||
import co.elastic.clients.elasticsearch._types.ScriptLanguage;
|
||||
import co.elastic.clients.elasticsearch._types.query_dsl.Query;
|
||||
import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
|
||||
import co.elastic.clients.elasticsearch.core.SearchRequest;
|
||||
import co.elastic.clients.json.JsonData;
|
||||
import org.dromara.easyes.core.conditions.select.LambdaEsQueryWrapper;
|
||||
import org.dromara.easyes.test.TestEasyEsApplication;
|
||||
import org.dromara.easyes.test.entity.Document;
|
||||
import org.dromara.easyes.test.mapper.DocumentMapper;
|
||||
import org.elasticsearch.index.query.QueryBuilder;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.script.Script;
|
||||
import org.elasticsearch.script.ScriptType;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Disabled;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 向量测试
|
||||
@ -54,15 +52,17 @@ public class VectorTest {
|
||||
|
||||
@Test
|
||||
public void testVectorSearch() {
|
||||
Map<String, Object> params = new HashMap<>();
|
||||
params.put("vector", new double[]{0.39684247970581055, 0.7687071561813354, 0.5145490765571594});
|
||||
String scriptCode = "cosineSimilarity(params.vector, 'vector') + 1.0";
|
||||
QueryBuilder queryBuilder = QueryBuilders.scriptScoreQuery(QueryBuilders.matchAllQuery(), new Script(ScriptType.INLINE, Script.DEFAULT_SCRIPT_LANG, scriptCode, params));
|
||||
Query query = Query.of(a -> a.scriptScore(b -> b
|
||||
.query(QueryBuilders.matchAll().build()._toQuery())
|
||||
.script(d -> d
|
||||
.lang(ScriptLanguage.Painless)
|
||||
.params("vector", JsonData.of(new double[]{0.39684247970581055, 0.7687071561813354, 0.5145490765571594}))
|
||||
.source("cosineSimilarity(params.vector, 'vector') + 1.0")
|
||||
)
|
||||
));
|
||||
|
||||
SearchRequest.Builder searchSourceBuilder = new SearchRequest.Builder();
|
||||
searchSourceBuilder.query(queryBuilder);
|
||||
LambdaEsQueryWrapper<Document> wrapper = new LambdaEsQueryWrapper<>();
|
||||
wrapper.setSearchBuilder(searchSourceBuilder);
|
||||
wrapper.setSearchBuilder(new SearchRequest.Builder().query(query));
|
||||
|
||||
List<Document> Documents = documentMapper.selectList(wrapper);
|
||||
Assertions.assertFalse(Documents.isEmpty());
|
||||
|
||||
@ -1,9 +1,9 @@
|
||||
easy-es:
|
||||
# enable: true
|
||||
address: 10.18.2.45:9200
|
||||
address: 192.168.1.16:30156
|
||||
# schema: http
|
||||
# username: elastic
|
||||
# password: WG7WVmuNMtM4GwNYkyWH
|
||||
username: elastic
|
||||
password: mg123456
|
||||
keep-alive-millis: 18000
|
||||
global-config:
|
||||
i-kun-mode: true
|
||||
@ -11,7 +11,7 @@ easy-es:
|
||||
async-process-index-blocking: true
|
||||
print-dsl: true
|
||||
db-config:
|
||||
map-underscore-to-camel-case: true
|
||||
map-underscore-to-camel-case: false
|
||||
id-type: customize
|
||||
field-strategy: not_empty
|
||||
refresh-policy: immediate
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user