gpt4 book ai didi

spring-boot - 如何通过Elasticsearch查找排序数据-Spring Boot

转载 作者:行者123 更新时间:2023-12-03 00:38:51 29 4
gpt4 key购买 nike

使用 flex 搜索对数据进行排序时出现错误。我将向您展示我的代码。

这是我的模型课:

@Document(indexName="book",type="book")
public class Book {
@Id
private String id;
private String title;
private String author;
private String releaseDate;
public Book() {
super();
}
public Book(String id, String title, String author, String releaseDate) {
super();
this.id = id;
this.title = title;
this.author = author;
this.releaseDate = releaseDate;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getReleaseDate() {
return releaseDate;
}
public void setReleaseDate(String releaseDate) {
this.releaseDate = releaseDate;
}
@Override
public String toString() {
return "Book [id=" + id + ", title=" + title + ", author=" + author + ", releaseDate=" + releaseDate + "]";
}

}

这是我用来显示基于id的排序数据的查询:
SearchQuery searchQuery = new NativeSearchQueryBuilder()
.withSort(SortBuilders.fieldSort("id").order(SortOrder.DESC))
.withPageable(PageRequest.of(index, 4))
.build();
List<Book> res = elasticTemplate.queryForList(searchQuery, Book.class);
Iterator<Book> itr = res.iterator();
while (itr.hasNext()) {
Book book = (Book) itr.next();
System.out.println(book);
}

,我收到此错误消息:
Caused by: org.elasticsearch.action.search.SearchPhaseExecutionException: all shards failed
at org.elasticsearch.action.search.AbstractSearchAsyncAction.onPhaseFailure(AbstractSearchAsyncAction.java:293) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.action.search.AbstractSearchAsyncAction.executeNextPhase(AbstractSearchAsyncAction.java:133) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.action.search.AbstractSearchAsyncAction.onPhaseDone(AbstractSearchAsyncAction.java:254) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.action.search.InitialSearchPhase.onShardFailure(InitialSearchPhase.java:101) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.action.search.InitialSearchPhase.access$100(InitialSearchPhase.java:48) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.action.search.InitialSearchPhase$2.lambda$onFailure$1(InitialSearchPhase.java:221) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.action.search.InitialSearchPhase.maybeFork(InitialSearchPhase.java:175) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.action.search.InitialSearchPhase.access$000(InitialSearchPhase.java:48) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.action.search.InitialSearchPhase$2.onFailure(InitialSearchPhase.java:221) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.action.ActionListenerResponseHandler.handleException(ActionListenerResponseHandler.java:53) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.action.search.SearchTransportService$ConnectionCountingHandler.handleException(SearchTransportService.java:462) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.transport.TransportService$ContextRestoreResponseHandler.handleException(TransportService.java:1103) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.transport.TransportService$DirectResponseChannel.processException(TransportService.java:1215) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.transport.TransportService$DirectResponseChannel.sendResponse(TransportService.java:1189) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.transport.TaskTransportChannel.sendResponse(TaskTransportChannel.java:60) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.action.support.HandledTransportAction$ChannelActionListener.onFailure(HandledTransportAction.java:112) ~[na:na]
at org.elasticsearch.search.SearchService$1.onFailure(SearchService.java:319) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.search.SearchService$1.onResponse(SearchService.java:313) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.search.SearchService$1.onResponse(SearchService.java:307) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.search.SearchService$4.doRun(SearchService.java:1117) ~[na:na]
at org.elasticsearch.common.util.concurrent.ThreadContext$ContextPreservingAbstractRunnable.doRun(ThreadContext.java:759) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.common.util.concurrent.TimedRunnable.doRun(TimedRunnable.java:41) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37) ~[elasticsearch-6.4.3.jar:6.4.3]
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) ~[na:1.8.0_181]
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) ~[na:1.8.0_181]
at java.lang.Thread.run(Thread.java:748) ~[na:1.8.0_181]
Caused by: org.elasticsearch.common.io.stream.NotSerializableExceptionWrapper: : Fielddata is disabled on text fields by default. Set fielddata=true on [id] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.
at org.elasticsearch.ElasticsearchException.guessRootCauses(ElasticsearchException.java:657) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.action.search.AbstractSearchAsyncAction.executeNextPhase(AbstractSearchAsyncAction.java:131) ~[elasticsearch-6.4.3.jar:6.4.3]
... 25 common frames omitted
Caused by: java.lang.IllegalArgumentException: Fielddata is disabled on text fields by default. Set fielddata=true on [id] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.
at org.elasticsearch.index.mapper.TextFieldMapper$TextFieldType.fielddataBuilder(TextFieldMapper.java:670) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.index.fielddata.IndexFieldDataService.getForField(IndexFieldDataService.java:116) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.index.query.QueryShardContext.getForField(QueryShardContext.java:166) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.search.sort.FieldSortBuilder.build(FieldSortBuilder.java:349) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.search.sort.SortBuilder.buildSort(SortBuilder.java:153) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.search.SearchService.parseSource(SearchService.java:823) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.search.SearchService.createContext(SearchService.java:656) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.search.SearchService.createAndPutContext(SearchService.java:631) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.search.SearchService.executeDfsPhase(SearchService.java:325) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.search.SearchService.access$000(SearchService.java:126) ~[elasticsearch-6.4.3.jar:6.4.3]
at org.elasticsearch.search.SearchService$1.onResponse(SearchService.java:311) ~[elasticsearch-6.4.3.jar:6.4.3]
... 9 common frames omitted

我发现了此错误的原因。默认情况下,文本字段上的fielddata被禁用,但我不知道如何启用字段数据。如何在“id”上设置fielddata = true?我可以在基于方法名的查询中对数据进行排序吗?

最佳答案

你必须用

SortBuilders.fieldSort("id.keyword") 

按此字段的关键字排序。

关于spring-boot - 如何通过Elasticsearch查找排序数据-Spring Boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54514466/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com