Elasticsearch具体版本强依赖性
Elasticsearch以及其周边的相关平台都是强版本依赖的,升级过程也需要升级其他相关组件。
目前Elasticsearch的6.x相当于3.x。之所以从2一跃跳到6,Elastic体系内还有Kibana,Logstash,Beats等产品。为了统一各产品版本,所以直接将Elasticsearch的版本从2提升到6 ,6.x版本提供了许多新的特性,并且基于Lucene7.x
性能方面:
1.磁盘空间可以节省近一半
2.索引时间减少近50%
3.查询性能提升近30%
4.支持IPV6
性能的具体数据可以查看Elasticsearch性能监控。性能的提升主要是Lucene6版本之后的很多底层结构的优化。Lucene6使用Block K-D trees
数据结构来构建索引。BKD Trees
是一种可以动态扩展的KD-tree
结构。
想了解BKD Trees可以参考:
https://users.cs.duke.edu/~pankaj/publications/papers/bkd-sstd.pdf
Elasticsearch也是一种数据库,属于非关系型数据库,不需要自己建库建表
与SpringBoot进行集成
依赖引入
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-data-elasticsearch
实体类:
@Data
@Document(indexName = "province", type = "city")
public class City implements Serializable {
@Id
@Field(fielddata = true)
private Long id;
private String score;
private String name;
private String description;
}
加上@Id
注解后,在Elasticsearch里对应的该列就是主键了,在查询时就可以直接用主键查询
相关代码说明:
@Document源码
@Persistent
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface Document {
String indexName();
String type() default "";
boolean useServerConfiguration() default false;
short shards() default 5;
short replicas() default 1;
String refreshInterval() default "1s";
String indexStoreType() default "fs";
boolean createIndex() default true;
}
indexName()
:索引库的名称,建议以项目的名称命名,类似于关系型数据库中数据库
type()
:类型,建议以实体的名称命名,类似于关系型数据库中数据库表
useServerConfiguration()
:是否使用服务器配置
shards()
:默认分区数
replicas()
:每个分区默认的备份数
refreshInterval()
:刷新间隔
indexStoreType()
:索引文件存储类型
createIndex()
:是否创建索引
@Field源码
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.FIELD})
@Documented
@Inherited
public @interface Field {
FieldType type() default FieldType.Auto;
boolean index() default true;
DateFormat format() default DateFormat.none;
String pattern() default "";
boolean store() default false;
boolean fielddata() default false;
String searchAnalyzer() default "";
String analyzer() default "";
String normalizer() default "";
String[] ignoreFields() default {};
boolean includeInParent() default false;
String[] copyTo() default {};
}
type()
:类型,默认自动检测,也可以根据实际情况设置
index()
:原来的analyzed/not_analyzed/no
变为只接受boolean
值。分别代替not_analyzed/no
format()
:时间类型的格式化
store()
:是否存储原文
searchAnalyzer()
:指定字段搜索时使用的分词器
ignoreFields()
:忽略某个字段
注:因为Elasticsearch
6.x中的Mapping
改变,原有的@Field(type = FieldType.string)
类型的索引映射需要替换成@Field(type = FieldType.Keyword)
或者@Field(type = FieldType.Text)
。 要精确搜索就用Keyword
,否则用Text
。如果不需要通过该字段进行查询,则index
设置false
即可。
可能出现的报错:
Caused by: java.lang.IllegalArgumentException: mapper [xx] of different type, current_type [xxx], merged_type [xxx]
因为索引已经存在了,创建索引时不会覆盖原有的,所以删除原有的就不会报错了
数据操作层
Spring Data JPA和Spring Data Elasticsearch共享相同的通用基础架构。与JPA存储库一样,基本原则是基于方法名称自动构造查询。
public interface CityRepository extends ElasticsearchRepository {
}
ElasticsearchRepository源码
@NoRepositoryBean
public interface ElasticsearchRepository extends ElasticsearchCrudRepository {
S index(S var1);
Iterable search(QueryBuilder var1);
Page search(QueryBuilder var1, Pageable var2);
Page search(SearchQuery var1);
Page searchSimilar(T var1, String[] var2, Pageable var3);
void refresh();
Class getEntityClass();
}
为什么具有Crud操作呢
注意看以下继承关系
public interface ElasticsearchCrudRepository extends PagingAndSortingRepository
public interface PagingAndSortingRepository extends CrudRepository
不难发现最后有继承CrudRepository
接口
CrudRepository源码
@NoRepositoryBean
public interface CrudRepository extends Repository {
S save(S var1);
Iterable saveAll(Iterable var1);
Optional findById(ID var1);
boolean existsById(ID var1);
Iterable findAll();
Iterable findAllById(Iterable var1);
long count();
void deleteById(ID var1);
void delete(T var1);
void deleteAll(Iterable extends T> var1);
void deleteAll();
}
控制层
实现简单的添加功能
@RestController
public class CityRestController {
private final CityRepository cityRepository;
@Autowired
public CityRestController(CityRepository cityRepository) {
this.cityRepository = cityRepository;
}
@PostMapping("/api/city")
public City createCity(@RequestBody City city) {
return cityRepository.save(city);
}
}
yml配置文件
spring:
data:
elasticsearch:
cluster-nodes: localhost:9300
cluster-name: my-application
server:
port: 8888
注:记得修改目录config
下的elasticsearch.yml
配置文件
1.把cluster.name:my-application
注释打开
2.network.host
改为localhost
,也就是network.host: localhost
下面使用postman测试下
再插入一条数据
{
"id":2,
"score":"5",
"name":"广州",
"description":"也是一线城市"
}
查看Elasticsearch的数据
head插件下
kibana客户端下
第一次进入kibana如果提示没索引就创建下,索引名就是indexName
有Table和JSON两种查看方式
注:插入数据的主键如果相同,新数据将会覆盖旧数据
推荐参考:
接口文档:
https://docs.spring.io/spring-data/elasticsearch/docs/3.1.3.RELEASE/reference/html/
API文档:
https://docs.spring.io/spring-data/elasticsearch/docs/3.1.3.RELEASE/api/
Elasticsearch查询
我们都知道es是一个分布式的存储和检索系统,在存储的时候默认是根据每条记录的_id字段做路由分发的,这意味着es服务端是准确知道每个document分布在那个shard上的。
相对比于CURD上操作,search一个比较复杂的执行模式,因为我们不知道那些document会被匹配到,任何一个shard上都有可能,所以一个search请求必须查询一个索引或多个索引里面的所有shard才能完整的查询到我们想要的结果。
QueryBuilder 是Elasticsearch中提供的一个查询接口,
具体实现需要使用QueryBuilders进行查询
public final class QueryBuilders {
private QueryBuilders() {
}
public static MatchAllQueryBuilder matchAllQuery() {
return new MatchAllQueryBuilder();
}
public static MatchQueryBuilder matchQuery(String name, Object text) {
return new MatchQueryBuilder(name, text);
}
public static CommonTermsQueryBuilder commonTermsQuery(String fieldName, Object text) {
return new CommonTermsQueryBuilder(fieldName, text);
}
public static MultiMatchQueryBuilder multiMatchQuery(Object text, String... fieldNames) {
return new MultiMatchQueryBuilder(text, fieldNames);
}
public static MatchPhraseQueryBuilder matchPhraseQuery(String name, Object text) {
return new MatchPhraseQueryBuilder(name, text);
}
public static MatchPhrasePrefixQueryBuilder matchPhrasePrefixQuery(String name, Object text) {
return new MatchPhrasePrefixQueryBuilder(name, text);
}
public static DisMaxQueryBuilder disMaxQuery() {
return new DisMaxQueryBuilder();
}
public static IdsQueryBuilder idsQuery() {
return new IdsQueryBuilder();
}
public static IdsQueryBuilder idsQuery(String... types) {
return (new IdsQueryBuilder()).types(types);
}
public static TermQueryBuilder termQuery(String name, String value) {
return new TermQueryBuilder(name, value);
}
public static TermQueryBuilder termQuery(String name, int value) {
return new TermQueryBuilder(name, value);
}
public static TermQueryBuilder termQuery(String name, long value) {
return new TermQueryBuilder(name, value);
}
public static TermQueryBuilder termQuery(String name, float value) {
return new TermQueryBuilder(name, value);
}
public static TermQueryBuilder termQuery(String name, double value) {
return new TermQueryBuilder(name, value);
}
public static TermQueryBuilder termQuery(String name, boolean value) {
return new TermQueryBuilder(name, value);
}
public static TermQueryBuilder termQuery(String name, Object value) {
return new TermQueryBuilder(name, value);
}
public static FuzzyQueryBuilder fuzzyQuery(String name, String value) {
return new FuzzyQueryBuilder(name, value);
}
public static FuzzyQueryBuilder fuzzyQuery(String name, Object value) {
return new FuzzyQueryBuilder(name, value);
}
public static PrefixQueryBuilder prefixQuery(String name, String prefix) {
return new PrefixQueryBuilder(name, prefix);
}
public static RangeQueryBuilder rangeQuery(String name) {
return new RangeQueryBuilder(name);
}
public static WildcardQueryBuilder wildcardQuery(String name, String query) {
return new WildcardQueryBuilder(name, query);
}
public static RegexpQueryBuilder regexpQuery(String name, String regexp) {
return new RegexpQueryBuilder(name, regexp);
}
public static QueryStringQueryBuilder queryStringQuery(String queryString) {
return new QueryStringQueryBuilder(queryString);
}
public static SimpleQueryStringBuilder simpleQueryStringQuery(String queryString) {
return new SimpleQueryStringBuilder(queryString);
}
public static BoostingQueryBuilder boostingQuery(QueryBuilder positiveQuery, QueryBuilder negativeQuery) {
return new BoostingQueryBuilder(positiveQuery, negativeQuery);
}
public static BoolQueryBuilder boolQuery() {
return new BoolQueryBuilder();
}
public static SpanTermQueryBuilder spanTermQuery(String name, String value) {
return new SpanTermQueryBuilder(name, value);
}
public static SpanTermQueryBuilder spanTermQuery(String name, int value) {
return new SpanTermQueryBuilder(name, value);
}
public static SpanTermQueryBuilder spanTermQuery(String name, long value) {
return new SpanTermQueryBuilder(name, value);
}
public static SpanTermQueryBuilder spanTermQuery(String name, float value) {
return new SpanTermQueryBuilder(name, value);
}
public static SpanTermQueryBuilder spanTermQuery(String name, double value) {
return new SpanTermQueryBuilder(name, value);
}
public static SpanFirstQueryBuilder spanFirstQuery(SpanQueryBuilder match, int end) {
return new SpanFirstQueryBuilder(match, end);
}
public static SpanNearQueryBuilder spanNearQuery(SpanQueryBuilder initialClause, int slop) {
return new SpanNearQueryBuilder(initialClause, slop);
}
public static SpanNotQueryBuilder spanNotQuery(SpanQueryBuilder include, SpanQueryBuilder exclude) {
return new SpanNotQueryBuilder(include, exclude);
}
public static SpanOrQueryBuilder spanOrQuery(SpanQueryBuilder initialClause) {
return new SpanOrQueryBuilder(initialClause);
}
public static SpanWithinQueryBuilder spanWithinQuery(SpanQueryBuilder big, SpanQueryBuilder little) {
return new SpanWithinQueryBuilder(big, little);
}
public static SpanContainingQueryBuilder spanContainingQuery(SpanQueryBuilder big, SpanQueryBuilder little) {
return new SpanContainingQueryBuilder(big, little);
}
public static SpanMultiTermQueryBuilder spanMultiTermQueryBuilder(MultiTermQueryBuilder multiTermQueryBuilder) {
return new SpanMultiTermQueryBuilder(multiTermQueryBuilder);
}
public static FieldMaskingSpanQueryBuilder fieldMaskingSpanQuery(SpanQueryBuilder query, String field) {
return new FieldMaskingSpanQueryBuilder(query, field);
}
public static ConstantScoreQueryBuilder constantScoreQuery(QueryBuilder queryBuilder) {
return new ConstantScoreQueryBuilder(queryBuilder);
}
public static FunctionScoreQueryBuilder functionScoreQuery(QueryBuilder queryBuilder) {
return new FunctionScoreQueryBuilder(queryBuilder);
}
public static FunctionScoreQueryBuilder functionScoreQuery(QueryBuilder queryBuilder, FilterFunctionBuilder[] filterFunctionBuilders) {
return new FunctionScoreQueryBuilder(queryBuilder, filterFunctionBuilders);
}
public static FunctionScoreQueryBuilder functionScoreQuery(FilterFunctionBuilder[] filterFunctionBuilders) {
return new FunctionScoreQueryBuilder(filterFunctionBuilders);
}
public static FunctionScoreQueryBuilder functionScoreQuery(ScoreFunctionBuilder function) {
return new FunctionScoreQueryBuilder(function);
}
public static FunctionScoreQueryBuilder functionScoreQuery(QueryBuilder queryBuilder, ScoreFunctionBuilder function) {
return new FunctionScoreQueryBuilder(queryBuilder, function);
}
public static MoreLikeThisQueryBuilder moreLikeThisQuery(String[] fields, String[] likeTexts, Item[] likeItems) {
return new MoreLikeThisQueryBuilder(fields, likeTexts, likeItems);
}
public static MoreLikeThisQueryBuilder moreLikeThisQuery(String[] likeTexts, Item[] likeItems) {
return moreLikeThisQuery((String[])null, likeTexts, likeItems);
}
public static MoreLikeThisQueryBuilder moreLikeThisQuery(String[] likeTexts) {
return moreLikeThisQuery((String[])null, likeTexts, (Item[])null);
}
public static MoreLikeThisQueryBuilder moreLikeThisQuery(Item[] likeItems) {
return moreLikeThisQuery((String[])null, (String[])null, likeItems);
}
public static NestedQueryBuilder nestedQuery(String path, QueryBuilder query, ScoreMode scoreMode) {
return new NestedQueryBuilder(path, query, scoreMode);
}
public static TermsQueryBuilder termsQuery(String name, String... values) {
return new TermsQueryBuilder(name, values);
}
public static TermsQueryBuilder termsQuery(String name, int... values) {
return new TermsQueryBuilder(name, values);
}
public static TermsQueryBuilder termsQuery(String name, long... values) {
return new TermsQueryBuilder(name, values);
}
public static TermsQueryBuilder termsQuery(String name, float... values) {
return new TermsQueryBuilder(name, values);
}
public static TermsQueryBuilder termsQuery(String name, double... values) {
return new TermsQueryBuilder(name, values);
}
public static TermsQueryBuilder termsQuery(String name, Object... values) {
return new TermsQueryBuilder(name, values);
}
public static TermsQueryBuilder termsQuery(String name, Collection> values) {
return new TermsQueryBuilder(name, values);
}
public static WrapperQueryBuilder wrapperQuery(String source) {
return new WrapperQueryBuilder(source);
}
public static WrapperQueryBuilder wrapperQuery(BytesReference source) {
return new WrapperQueryBuilder(source);
}
public static WrapperQueryBuilder wrapperQuery(byte[] source) {
return new WrapperQueryBuilder(source);
}
public static TypeQueryBuilder typeQuery(String type) {
return new TypeQueryBuilder(type);
}
public static TermsQueryBuilder termsLookupQuery(String name, TermsLookup termsLookup) {
return new TermsQueryBuilder(name, termsLookup);
}
public static ScriptQueryBuilder scriptQuery(Script script) {
return new ScriptQueryBuilder(script);
}
public static GeoDistanceQueryBuilder geoDistanceQuery(String name) {
return new GeoDistanceQueryBuilder(name);
}
public static GeoBoundingBoxQueryBuilder geoBoundingBoxQuery(String name) {
return new GeoBoundingBoxQueryBuilder(name);
}
public static GeoPolygonQueryBuilder geoPolygonQuery(String name, List points) {
return new GeoPolygonQueryBuilder(name, points);
}
public static GeoShapeQueryBuilder geoShapeQuery(String name, ShapeBuilder shape) throws IOException {
return new GeoShapeQueryBuilder(name, shape);
}
public static GeoShapeQueryBuilder geoShapeQuery(String name, String indexedShapeId, String indexedShapeType) {
return new GeoShapeQueryBuilder(name, indexedShapeId, indexedShapeType);
}
public static GeoShapeQueryBuilder geoIntersectionQuery(String name, ShapeBuilder shape) throws IOException {
GeoShapeQueryBuilder builder = geoShapeQuery(name, shape);
builder.relation(ShapeRelation.INTERSECTS);
return builder;
}
public static GeoShapeQueryBuilder geoIntersectionQuery(String name, String indexedShapeId, String indexedShapeType) {
GeoShapeQueryBuilder builder = geoShapeQuery(name, indexedShapeId, indexedShapeType);
builder.relation(ShapeRelation.INTERSECTS);
return builder;
}
public static GeoShapeQueryBuilder geoWithinQuery(String name, ShapeBuilder shape) throws IOException {
GeoShapeQueryBuilder builder = geoShapeQuery(name, shape);
builder.relation(ShapeRelation.WITHIN);
return builder;
}
public static GeoShapeQueryBuilder geoWithinQuery(String name, String indexedShapeId, String indexedShapeType) {
GeoShapeQueryBuilder builder = geoShapeQuery(name, indexedShapeId, indexedShapeType);
builder.relation(ShapeRelation.WITHIN);
return builder;
}
public static GeoShapeQueryBuilder geoDisjointQuery(String name, ShapeBuilder shape) throws IOException {
GeoShapeQueryBuilder builder = geoShapeQuery(name, shape);
builder.relation(ShapeRelation.DISJOINT);
return builder;
}
public static GeoShapeQueryBuilder geoDisjointQuery(String name, String indexedShapeId, String indexedShapeType) {
GeoShapeQueryBuilder builder = geoShapeQuery(name, indexedShapeId, indexedShapeType);
builder.relation(ShapeRelation.DISJOINT);
return builder;
}
public static ExistsQueryBuilder existsQuery(String name) {
return new ExistsQueryBuilder(name);
}
}
部分方法说明
termQuery("key", obj)
完全匹配
termsQuery("key", obj1, obj2..)
: 一次匹配多个值
matchQuery("key", Obj)
:单个匹配, field不支持通配符, 前缀具高级特性
multiMatchQuery("text", "field1", "field2"..)
:匹配多个字段, field有通配符
matchAllQuery()
:匹配所有
详细可以参考官方说明:
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-query-builders.html
通过源码可以看到方法返回的对象时各种Builder
继承关系都是这样的
public class xxxxBuilder extends AbstractQueryBuilder
public abstract class AbstractQueryBuilder> implements QueryBuilder
最后都会实现QueryBuilder接口
QueryBuilder源码
public interface QueryBuilder extends NamedWriteable, ToXContentObject, Rewriteable {
Query toQuery(QueryShardContext var1) throws IOException;
Query toFilter(QueryShardContext var1) throws IOException;
QueryBuilder queryName(String var1);
String queryName();
float boost();
QueryBuilder boost(float var1);
String getName();
default QueryBuilder rewrite(QueryRewriteContext queryShardContext) throws IOException {
return this;
}
}
特殊情况下,ElasticsearchRepository里面有几个特殊的search方法,这些是ES特有的,和普通的JPA区别的地方,用来构建一些ES查询的。
主要是看QueryBuilder和SearchQuery两个参数,要完成一些特殊查询就主要看构建这两个参数。
从这个关系中可以看到ES的search方法需要的参数SearchQuery是一个接口,有一个实现类叫NativeSearchQuery,实际使用中,我们的主要任务就是构建NativeSearchQuery来完成一些复杂的查询的。
NativeSearchQuery源码
public class NativeSearchQuery extends AbstractQuery implements SearchQuery {
private QueryBuilder query;
private QueryBuilder filter;
private List sorts;
private final List scriptFields = new ArrayList();
private List facets;
private List aggregations;
private HighlightBuilder highlightBuilder;
private Field[] highlightFields;
private List indicesBoost;
public NativeSearchQuery(QueryBuilder query) {
this.query = query;
}
public NativeSearchQuery(QueryBuilder query, QueryBuilder filter) {
this.query = query;
this.filter = filter;
}
public NativeSearchQuery(QueryBuilder query, QueryBuilder filter, List sorts) {
this.query = query;
this.filter = filter;
this.sorts = sorts;
}
public NativeSearchQuery(QueryBuilder query, QueryBuilder filter, List sorts, Field[] highlightFields) {
this.query = query;
this.filter = filter;
this.sorts = sorts;
this.highlightFields = highlightFields;
}
public NativeSearchQuery(QueryBuilder query, QueryBuilder filter, List sorts, HighlightBuilder highlighBuilder, Field[] highlightFields) {
this.query = query;
this.filter = filter;
this.sorts = sorts;
this.highlightBuilder = highlighBuilder;
this.highlightFields = highlightFields;
}
public QueryBuilder getQuery() {
return this.query;
}
public QueryBuilder getFilter() {
return this.filter;
}
public List getElasticsearchSorts() {
return this.sorts;
}
public HighlightBuilder getHighlightBuilder() {
return this.highlightBuilder;
}
public Field[] getHighlightFields() {
return this.highlightFields;
}
public List getScriptFields() {
return this.scriptFields;
}
public void setScriptFields(List scriptFields) {
this.scriptFields.addAll(scriptFields);
}
public void addScriptField(ScriptField... scriptField) {
this.scriptFields.addAll(Arrays.asList(scriptField));
}
public void addFacet(FacetRequest facetRequest) {
if (this.facets == null) {
this.facets = new ArrayList();
}
this.facets.add(facetRequest);
}
public void setFacets(List facets) {
this.facets = facets;
}
public List getFacets() {
return this.facets;
}
public List getAggregations() {
return this.aggregations;
}
public void addAggregation(AbstractAggregationBuilder aggregationBuilder) {
if (this.aggregations == null) {
this.aggregations = new ArrayList();
}
this.aggregations.add(aggregationBuilder);
}
public void setAggregations(List aggregations) {
this.aggregations = aggregations;
}
public List getIndicesBoost() {
return this.indicesBoost;
}
public void setIndicesBoost(List indicesBoost) {
this.indicesBoost = indicesBoost;
}
}
这里主要看构造方法
QueryBuilder query:查询构造器
QueryBuilder filter:查询构造器
List
HighlightBuilder highlighBuilder:高亮构造器
Field[] highlightFields:高亮的字段
构造NativeSearchQuery一般不是直接是new ,而是使用NativeSearchQueryBuilder的build方法
源码如下:
public class NativeSearchQueryBuilder {
private QueryBuilder queryBuilder;
private QueryBuilder filterBuilder;
private List scriptFields = new ArrayList();
private List sortBuilders = new ArrayList();
private List facetRequests = new ArrayList();
private List aggregationBuilders = new ArrayList();
private HighlightBuilder highlightBuilder;
private Field[] highlightFields;
private Pageable pageable = Pageable.unpaged();
private String[] indices;
private String[] types;
private String[] fields;
private SourceFilter sourceFilter;
private List indicesBoost;
private float minScore;
private boolean trackScores;
private Collection ids;
private String route;
private SearchType searchType;
private IndicesOptions indicesOptions;
public NativeSearchQueryBuilder() {
}
public NativeSearchQueryBuilder withQuery(QueryBuilder queryBuilder) {
this.queryBuilder = queryBuilder;
return this;
}
public NativeSearchQueryBuilder withFilter(QueryBuilder filterBuilder) {
this.filterBuilder = filterBuilder;
return this;
}
public NativeSearchQueryBuilder withSort(SortBuilder sortBuilder) {
this.sortBuilders.add(sortBuilder);
return this;
}
public NativeSearchQueryBuilder withScriptField(ScriptField scriptField) {
this.scriptFields.add(scriptField);
return this;
}
public NativeSearchQueryBuilder addAggregation(AbstractAggregationBuilder aggregationBuilder) {
this.aggregationBuilders.add(aggregationBuilder);
return this;
}
public NativeSearchQueryBuilder withFacet(FacetRequest facetRequest) {
this.facetRequests.add(facetRequest);
return this;
}
public NativeSearchQueryBuilder withHighlightBuilder(HighlightBuilder highlightBuilder) {
this.highlightBuilder = highlightBuilder;
return this;
}
public NativeSearchQueryBuilder withHighlightFields(Field... highlightFields) {
this.highlightFields = highlightFields;
return this;
}
public NativeSearchQueryBuilder withIndicesBoost(List indicesBoost) {
this.indicesBoost = indicesBoost;
return this;
}
public NativeSearchQueryBuilder withPageable(Pageable pageable) {
this.pageable = pageable;
return this;
}
public NativeSearchQueryBuilder withIndices(String... indices) {
this.indices = indices;
return this;
}
public NativeSearchQueryBuilder withTypes(String... types) {
this.types = types;
return this;
}
public NativeSearchQueryBuilder withFields(String... fields) {
this.fields = fields;
return this;
}
public NativeSearchQueryBuilder withSourceFilter(SourceFilter sourceFilter) {
this.sourceFilter = sourceFilter;
return this;
}
public NativeSearchQueryBuilder withMinScore(float minScore) {
this.minScore = minScore;
return this;
}
public NativeSearchQueryBuilder withTrackScores(boolean trackScores) {
this.trackScores = trackScores;
return this;
}
public NativeSearchQueryBuilder withIds(Collection ids) {
this.ids = ids;
return this;
}
public NativeSearchQueryBuilder withRoute(String route) {
this.route = route;
return this;
}
public NativeSearchQueryBuilder withSearchType(SearchType searchType) {
this.searchType = searchType;
return this;
}
public NativeSearchQueryBuilder withIndicesOptions(IndicesOptions indicesOptions) {
this.indicesOptions = indicesOptions;
return this;
}
public NativeSearchQuery build() {
NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(this.queryBuilder, this.filterBuilder, this.sortBuilders, this.highlightBuilder, this.highlightFields);
nativeSearchQuery.setPageable(this.pageable);
nativeSearchQuery.setTrackScores(this.trackScores);
if (this.indices != null) {
nativeSearchQuery.addIndices(this.indices);
}
if (this.types != null) {
nativeSearchQuery.addTypes(this.types);
}
if (this.fields != null) {
nativeSearchQuery.addFields(this.fields);
}
if (this.sourceFilter != null) {
nativeSearchQuery.addSourceFilter(this.sourceFilter);
}
if (this.indicesBoost != null) {
nativeSearchQuery.setIndicesBoost(this.indicesBoost);
}
if (!CollectionUtils.isEmpty(this.scriptFields)) {
nativeSearchQuery.setScriptFields(this.scriptFields);
}
if (!CollectionUtils.isEmpty(this.facetRequests)) {
nativeSearchQuery.setFacets(this.facetRequests);
}
if (!CollectionUtils.isEmpty(this.aggregationBuilders)) {
nativeSearchQuery.setAggregations(this.aggregationBuilders);
}
if (this.minScore > 0.0F) {
nativeSearchQuery.setMinScore(this.minScore);
}
if (this.ids != null) {
nativeSearchQuery.setIds(this.ids);
}
if (this.route != null) {
nativeSearchQuery.setRoute(this.route);
}
if (this.searchType != null) {
nativeSearchQuery.setSearchType(this.searchType);
}
if (this.indicesOptions != null) {
nativeSearchQuery.setIndicesOptions(this.indicesOptions);
}
return nativeSearchQuery;
}
}
QueryBuilder主要用来构建查询条件、过滤条件,SortBuilder主要是构建排序。
譬如,我们要查询距离某个位置100米范围内的所有人、并且按照距离远近进行排序
double lat = 39.929986;
double lon = 116.395645;
Long nowTime = System.currentTimeMillis();
//查询某经纬度100米范围内
GeoDistanceQueryBuilder builder = QueryBuilders.geoDistanceQuery("address").point(lat, lon)
.distance(100, DistanceUnit.METERS);
GeoDistanceSortBuilder sortBuilder = SortBuilders.geoDistanceSort("address")
.point(lat, lon)
.unit(DistanceUnit.METERS)
.order(SortOrder.ASC);
Pageable pageable = new PageRequest(0, 50);
NativeSearchQueryBuilder builder1 = new NativeSearchQueryBuilder().withFilter(builder).withSort(sortBuilder).withPageable(pageable);
SearchQuery searchQuery = builder1.build();
Elasticsearch与SpringBoot进行集成遇到的问题
1.Caused by: java.lang.IllegalStateException: availableProcessors is already set to [x], rejecting [x]
原因:这是netty与初始化Elasticsearch client冲突时的异常
解决方法
在启动类中加入System.setProperty("es.set.netty.runtime.available.processors", "false");
public class Application {
public static void main(String[] args) {
System.setProperty("es.set.netty.runtime.available.processors", "false");
SpringApplication.run(Application.class, args);
}
}