gpt4 book ai didi

java - Elastic Search java (spring) 索引问题

转载 作者:太空宇宙 更新时间:2023-11-04 13:29:32 26 4
gpt4 key购买 nike

我已经将elastic-search 1.7.1与spring应用程序集成。我有一个cron作业,它在每次运行时更新elasticsearch的索引。我遵循了 github 上提供的各种示例代码来使其工作。首先,我有 Autowired ElasticSearchOperations 用于索引目的:

@Autowired
private ElasticsearchOperations elasticsearchOperations;

然后按以下方式执行内部循环索引

for(int i=0;i<list.size();i++)
{
CategoryProductSearch search = new CategoryProductSearch();
// set data to fields
System.out.println("BEFORE SAVING DATA");
IndexQuery indexQuery =new

IndexQueryBuilder().withId(search.getId()).withObject(search).build();
//indexQuery.setId(search.getId());
//indexQuery.setObject(search);
//elasticsearchOperations.createIndex(CategoryProductSearch.class);
elasticsearchOperations.putMapping(CategoryProductSearch.class);
elasticsearchOperations.index(indexQuery);
elasticsearchOperations.refresh(CategoryProductSearch.class,true);

System.out.println("SAVING DATA");
}

当我第一次运行它时,它按预期工作。我已在 config 文件夹内的 elasticsearch.yml 中将集群重命名为“mycluster”。第一次运行后我可以看到创建的文件夹。索引和搜索(在另一个文件中实现)工作得很好。但有时代码会卡在下面提到的行并显示连续警告 [Chase Stein] 节点 null 不是集群的一部分 Cluster [elasticsearch],忽略...

 elasticsearchOperations.putMapping(CategoryProductSearch.class);

然后一段时间后它抛出 NoNodeAvailableException。我已经阅读过有关此问题的信息,它说可能没有足够的磁盘空间供 Elasticsearch 索引数据。我是 Spring 新手,第一次尝试 Elasticsearch 。这是磁盘空间问题还是我索引数据的方式有问题?另外,如果我从/data 目录中手动删除“mycluster”文件夹并重新启动应用程序,它会再次正常工作。!

我已在本地 PC 上设置好所有内容。每当我重新启动elasticsearch服务时,就会出现这个问题。

异常的堆栈跟踪是:

  org.elasticsearch.action.UnavailableShardsException: [mycluster][0]       

Primary shard is not active or isn't assigned to a known node. Timeout:

[1m], request: index {[mycluster][categoryproductsearch][1],

source[{// Source string }]
at
org.elasticsearch.action.support.replication
.TransportShardReplicationOperationAction$PrimaryPhase
.retryBecauseUnavailable
(TransportShardReplicationOperationAction.java:655)
at
org.elasticsearch.action.support.replication
.TransportShardReplicationOperationAction$PrimaryPhase.doRun
(TransportShardReplicationOperationAction.java:362)
at

org.elasticsearch.common.util.concurrent.AbstractRunnable.run
(AbstractRunnable.java:36)
at
org.elasticsearch.action.support.replication.
TransportShardReplicationOperationAction$PrimaryPhase$3.onTimeout
(TransportShardReplicationOperationAction.java:515)
at

org.elasticsearch.cluster.ClusterStateObserver$ObserverClusterStateListener
.onTimeout
(ClusterStateObserver.java:231)
at
org.elasticsearch.cluster.service.
InternalClusterService$NotifyTimeout.run
(InternalClusterService.java:560)
at
java.util.concurrent.ThreadPoolExecutor.runWorker
(ThreadPoolExecutor.java:1145)
at
java.util.concurrent.ThreadPoolExecutor$Worker.run
(ThreadPoolExecutor.java:615)
at
java.lang.Thread.run(Thread.java:745)

最佳答案

嗨,我从 generator-jhipster-elasticsearch-reindexer 获取了此代码

@Transactional(readOnly = true)
@SuppressWarnings("unchecked")
private <T> void reindexForClass(Class<T> entityClass, JpaRepository<T, Long> jpaRepository,
ElasticsearchRepository<T, Long> elasticsearchRepository) {
elasticsearchTemplate.deleteIndex(entityClass);
try {
elasticsearchTemplate.createIndex(entityClass);
} catch (IndexAlreadyExistsException e) {
// Do nothing. Index was already concurrently recreated by some other service.
}
elasticsearchTemplate.putMapping(entityClass);
if (jpaRepository.count() > 0) {
try {
Method m = jpaRepository.getClass().getMethod("findAllWithEagerRelationships");
elasticsearchRepository.save((List<T>) m.invoke(jpaRepository));
} catch (Exception e) {
elasticsearchRepository.save(jpaRepository.findAll());
}
}
log.info("Elasticsearch: Indexed all rows for " + entityClass.getSimpleName());
}

正如您所看到的,索引首先被删除,然后再次创建,并放置了映射,我认为您的顺序是错误的,并且会导致一些损坏的分片。您可以在 localhost:9200 上访问 Elastic Rest API 并尝试获取请求/_cat/indices 以查看您的索引。

关于java - Elastic Search java (spring) 索引问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32327074/

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