测试报错
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.test.context.junit4.SpringRunner;
import com.blog.spring.boot.blog.domain.es.EsBlog;
@RunWith(SpringRunner.class)
@SpringBootTest
public class EsBlogRepositoryTest{
@Autowired
private EsBlogRepository esBlogRepository;
@Before
public void initRepositoryData() {
//清除所有数据
esBlogRepository.deleteAll();
esBlogRepository.save(new EsBlog("登鹳雀楼", "王之涣的登鹳雀楼",
"白日依山尽,黄河入海流。欲穷千里目,更上一层楼。"));
esBlogRepository.save(new EsBlog("相思", "王维的相思",
"红豆生南国,春来发几枝。愿君多采撷,此物最相思"));
esBlogRepository.save(new EsBlog("静夜思", "李白的静夜思",
"床前明月光,疑是地上霜。举头望明月,低头思故乡"));
}
@Test
public void testFindDistinctEsBlogByTitleContainingOrSummaryContainingOrContentContaining() {
Pageable pageable = new PageRequest(0, 20);
String title = "思";
String summary = "相思";
String content = "相思";
Page<EsBlog> page = esBlogRepository.findDistinctEsBlogByTitleContainingOrSummaryContainingOrContentContaining(title, summary, content, pageable);
page.getTotalElements();
assertThat(page.getTotalElements()).isEqualTo(2);
System.out.println("--------start 1");
for(EsBlog blog : page.getContent()) {
System.out.println(blog.toString());
}
System.out.println("--------end 1");
}
}
到查询的时候,就报错了
Failed to execute phase [query], all shards failed
at org.elasticsearch.action.search.AbstractSearchAsyncAction.onPhaseFailure(AbstractSearchAsyncAction.java:293)
at org.elasticsearch.action.search.AbstractSearchAsyncAction.executeNextPhase(AbstractSearchAsyncAction.java:133)
at org.elasticsearch.action.search.AbstractSearchAsyncAction.onPhaseDone(AbstractSearchAsyncAction.java:254)
at org.elasticsearch.action.search.InitialSearchPhase.onShardFailure(InitialSearchPhase.java:101)
at org.elasticsearch.action.search.InitialSearchPhase.lambda$performPhaseOnShard$1(InitialSearchPhase.java:210)
at org.elasticsearch.action.search.InitialSearchPhase$1.doRun(InitialSearchPhase.java:189)
at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:723)
at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)
at org.elasticsearch.common.util.concurrent.TimedRunnable.doRun(TimedRunnable.java:41)
at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)
11月22日 00:30 问题终于解决了
记录下来,留给后来者参考
经过一天的调试
过程
1.怀疑代码问题
反复对比了官方代码并作了各种替换后确认代码没有问题
2.怀疑版本问题
由于用的最新版springboot,和上课的不一样,所以怀疑很可能是springboot版本问题,经过各种版本替换,无果,未能解决问题。
3.怀疑springboot自身的问题
有可能是springboot的胶水代码出错,所以试验了不同的elasticsearch号,无果。
4.最后假设前面的都没有错,代码本身和jar包版本都没问题
那么就是elasticsearch软件的问题。下载的是官方最新GA版本,先实验了最新的测试版本7.0,出现提示需要6.6.0的jar才能兼容,目前没有那么高版本,于是退一步,试验了6.4.3, 成功了
结论
目前,springboot 2.1.0版本的elasticsearch用6.5.0的服务端会出错,用6.4.3的服务端就正常,而之所以选6.4.3是因为记着之前springboot2.1.0版本默认的elasticsearch好像就是6.4.3
毕设 Elasticsearch搜索+Thymeleaf模板+JPA+Security+BootStrap
了解课程