索引

时间:2015-09-01 08:35:23

标签: java spring elasticsearch spring-data-elasticsearch

我已经将弹性搜索1.7.1与spring应用程序集成在一起。我有一个cron作业,可以在每次运行时更新elasticsearch的索引。 我已经遵循github上提供的各种示例代码来使其工作。首先我有自动装配的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");
 }

当我第一次运行时,它按预期工作。我已将群集重命名为&#34; mycluster&#34;在配置文件夹里面的elasticsearch.yml中。首次运行后,我可以看到创建的文件夹。索引和搜索(在另一个文件中实现)完美运行。但有时代码会卡在下面提到的行并显示连续警告[Chase Stein]节点null不属于集群Cluster [elasticsearch],忽略...

 elasticsearchOperations.putMapping(CategoryProductSearch.class);

然后在一段时间后抛出NoNodeAvailableException。我已经读过这个问题,它说可能没有足够的磁盘空间用于弹性搜索来索引数据。 我是春天的新手,并且第一次尝试了弹性搜索。这是一个磁盘空间问题还是我索引数据的方式有问题? 如果我手动删除&#34; mycluster&#34;来自/ data目录的文件夹并重启应用程序再次正常工作。

我已在本地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)

1 个答案:

答案 0 :(得分:0)

您好我从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并尝试使用get request / _cat / indices来查看索引。