我与Spring Web上商店,我决定添加文字搜索那里。我用这个指南尝试了hibernate搜索https://reflectoring.io/hibernate-search/ 启动应用程序时出错。
以下是我的(错误)+代码:
说明:
试图调用不存在的方法。尝试从以下位置进行:
org.hibernate.search.elasticsearch.client.impl.DefaultElasticsearchClientFactory.createClient(DefaultElasticsearchClientFactory.java:92)
以下方法不存在:
org.elasticsearch.client.RestClientBuilder.setMaxRetryTimeoutMillis(I)Lorg/elasticsearch/client/RestClientBuilder;
该方法的类org.elasticsearch.client.restclientbuilder可从以下位置获得:
jar:file:/C:/Users/myuser/.m2/repository/org/elasticsearch/client/elasticsearch-rest-client/7.9.3/elasticsearch-rest-client-7.9.3.jar!/org/elasticsearch/client/RestClientBuilder.class
已从以下位置加载类层次结构:
org.elasticsearch.client.RestClientBuilder: file:/C:/Users/myuser/.m2/repository/org/elasticsearch/client/elasticsearch-rest-client/7.9.3/elasticsearch-rest-client-7.9.3.jar
行动:
更正应用程序的类路径,使其包含org.elasticsearch.client.restclientbuilder的单个兼容版本
还有一些我创建的类(根据指南):item.class
@Data
@Entity
@Component
@AllArgsConstructor
@NoArgsConstructor
@Table(name = "items")
@Indexed(index = "items")
@NormalizerDef(name = "lowercase", filters = @TokenFilterDef(factory = LowerCaseFilterFactory.class))
public class Item {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long itemId;
@Field(name = "name", analyzer = @Analyzer(definition = "stop"))
private String itemName;
@Field(name = "description", analyzer = @Analyzer(definition = "stop"))
@Field(name = "name", analyzer = @Analyzer(definition = "stop"))
private String itemDesc;
@Field(normalizer = @Normalizer(definition = "lowercase"))
@Enumerated(EnumType.STRING)
private ItemCategory itemCategory;
索引服务类
@Service
@RequiredArgsConstructor
@Slf4j
public class IndexingService {
private final EntityManager em;
@Transactional // check
public void initiateIndexing() throws InterruptedException {
log.info("Initiating indexing...");
FullTextEntityManager fullTextEntityManager =
Search.getFullTextEntityManager(em);
fullTextEntityManager.createIndexer().startAndWait();
log.info("All entities indexed");
}
}
searchservice.class类
@Component
@Slf4j
@RequiredArgsConstructor
public class SearchService {
private final EntityManager entityManager;
public List<Item> getItemBasedOnWord(String word){
FullTextEntityManager fullTextEntityManager =
Search.getFullTextEntityManager(entityManager);
QueryBuilder qb = fullTextEntityManager
.getSearchFactory()
.buildQueryBuilder()
.forEntity(Item.class)
.get();
Query itemQuery = qb.keyword()
.onFields("name","description")
.matching(word)
.createQuery();
FullTextQuery fullTextQuery = fullTextEntityManager
.createFullTextQuery(itemQuery, Item.class);
return (List<Item>) fullTextQuery.getResultList();
}
}
startupevent.class类
@Component
@Slf4j
@RequiredArgsConstructor
public class StartupEvent implements ApplicationListener<ApplicationReadyEvent> {
private final IndexingService indexingService;
@Override
public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
try {
indexingService.initiateIndexing();
} catch (InterruptedException ex){
log.error("Failed to reindex entities ", ex);
}
}
}
最后是elasticsearch配置类:
@Configuration
public class EsConfig {
@Bean
public Client client() throws UnknownHostException {
Settings settings = Settings.builder().put("cluster.name", "elasticsearch").put("client.transport.sniff", true).build();
TransportClient transportClient = new PreBuiltTransportClient(settings);
transportClient.addTransportAddress(new TransportAddress(InetAddress.getByName("localhost"), 9200)); // 9300
return transportClient;
}
}
我应该在这里添加什么来防止此错误?我可以添加额外的代码,如果你需要。希望你们能帮帮我,伙计们!
1条答案
按热度按时间alen0pnh1#
您使用的是HibernateSearch5,这是一个旧版本,带有spring的最新版本。
HibernateSearch5很旧,它依赖于旧版本的elasticsearch客户端jars。
您的spring版本依赖于更新的、不兼容的elasticsearch客户端jars版本。
两种解决方案:
升级到更新的hibernate search 6,它很可能依赖于与spring捆绑的版本兼容的elasticsearch版本。注意,仅仅更改版本是不够的:组id、工件id和api是不同的。请参阅迁移指南。
或者尝试降级到旧版本的elasticsearch客户端。例如,使用Spring Boot: