com.es.plus.samples.service.SamplesEsService Maven / Gradle / Ivy
The newest version!
package com.es.plus.samples.service;
import com.es.plus.adapter.params.EsHit;
import com.es.plus.adapter.params.EsHits;
import com.es.plus.adapter.params.EsResponse;
import com.es.plus.core.service.EsServiceImpl;
import com.es.plus.core.statics.Es;
import com.es.plus.core.wrapper.aggregation.EsAggWrapper;
import com.es.plus.core.wrapper.chain.EsChainLambdaQueryWrapper;
import com.es.plus.core.wrapper.chain.EsChainQueryWrapper;
import com.es.plus.core.wrapper.core.EsLambdaQueryWrapper;
import com.es.plus.core.wrapper.core.EsLambdaUpdateWrapper;
import com.es.plus.core.wrapper.core.EsWrapper;
import com.es.plus.es7.client.EsPlusAggregations;
import com.es.plus.samples.dto.SamplesEsDTO;
import com.es.plus.samples.dto.SamplesNestedDTO;
import com.es.plus.samples.dto.SamplesNestedInnerDTO;
import org.apache.lucene.search.join.ScoreMode;
import org.elasticsearch.index.query.InnerHitBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
@Service
public class SamplesEsService extends EsServiceImpl {
/**
* 三级嵌套查询,如果不使用框架用原生查询将会非常复杂
*/
public void nested() {
//获取二级查询条件
Consumer> innerConsumer = getSamplesNestedConsumer();
// 声明语句嵌套关系是must
InnerHitBuilder innerHitBuilder = new InnerHitBuilder("test");
innerHitBuilder.setSize(10);
innerHitBuilder.setFetchSourceContext(new FetchSourceContext(false));
//一级查询条件
EsChainLambdaQueryWrapper queryWrapper = esChainQueryWrapper().must().fetch(false)
//二级
.nestedQuery(SamplesEsDTO::getSamplesNesteds, SamplesNestedDTO.class,
(esQueryWrap) -> {
esQueryWrap.must().term(SamplesNestedDTO::getUsername, "3");
InnerHitBuilder innerHitBuilder1 = new InnerHitBuilder();
innerHitBuilder1.setSize(100);
//三级
esQueryWrap.must().nestedQuery(SamplesNestedDTO::getSamplesNestedInner, SamplesNestedInnerDTO.class,
(innerQuery) -> {
innerQuery.must().term(SamplesNestedInnerDTO::getUsername, 3)
.term(SamplesNestedInnerDTO::getState,true);
}, ScoreMode.None, innerHitBuilder1);
}, ScoreMode.None,innerHitBuilder);
EsResponse esResponse = queryWrapper.search();
EsHits innerHits = esResponse.getInnerHits();
List esHitList = innerHits.getEsHitList();
for (EsHit esHit : esHitList) {
long innerHitsTotal = esHit.getInnerHitsTotal("test");
EsHits esHitEsHits = esHit.getEsInnerHits("test");
List test = esHit.getInnerList(SamplesNestedDTO.class, "test");
for (EsHit hit : esHitEsHits.getEsHitList()) {
List list = hit.getInnerList(SamplesNestedInnerDTO.class,"samplesNesteds.samplesNestedInner");
System.out.println();
}
System.out.println();
}
// 查询
List list = esResponse.getList();
System.out.println(list);
}
/**
* 获取二级嵌套查询对象
*/
private Consumer> getSamplesNestedConsumer() {
Consumer> innerConsumer = (esQueryWrap) -> {
esQueryWrap.must().term(SamplesNestedDTO::getUsername, "3");
InnerHitBuilder innerHitBuilder1 = new InnerHitBuilder();
innerHitBuilder1.setSize(100);
Consumer> innerInnerConsumer = skuQueryWrapper();
esQueryWrap.must().nestedQuery(SamplesNestedDTO::getSamplesNestedInner, SamplesNestedInnerDTO.class,
innerInnerConsumer, ScoreMode.None, innerHitBuilder1);
};
return innerConsumer;
}
/**
* 获取三级嵌套查询对象
*/
private Consumer> skuQueryWrapper() {
Consumer> innerInnerConsumer = (innerQuery) -> {
innerQuery.must().term(SamplesNestedInnerDTO::getUsername, 3);
};
return innerInnerConsumer;
}
public void search() {
// 声明语句嵌套关系是must
EsResponse esResponse = esChainQueryWrapper().mustNot()
.wildcard(SamplesEsDTO::getUsername, "*g那好好好好好好好好好好好好哈哈哈哈哈哈哈哈人兔兔兔兔兔兔兔兔吞吞吐吐他吞吞吐吐vvvvvvvvvvvvvvvvvvv请问额额额GG古古怪怪滚滚滚刚刚扭扭捏捏那你哈哈哈哈哈好哈哈哈哈哈哈哈哈应用*")
.term(SamplesEsDTO::getEmail, "bbbbbb")
// 多个must嵌套
// .must(a ->
// // 声明内部语句关系的should
// a.must()
// .term(SamplesEsDTO::getNickName, "dasdsad")
// .term(SamplesEsDTO::getPhone, "1386859111"))
.search();
// EsResponse list2 = esChainQueryWrapper().list();
// System.out.println(list2);
List list = esResponse.getList();
System.out.println(list);
try {
Thread.sleep(2000L);
} catch (InterruptedException e) {
e.printStackTrace();
}
EsChainQueryWrapper