无法使用Settings和Source Java API

时间:2016-08-29 08:05:47

标签: elasticsearch spring-boot spring-data-elasticsearch elasticsearch-java-api

使用Java API设置源代码和设置的唯一方法是使用这样的代码(这是一个只有一个@test方法的简单测试类):

   @RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest

@TestPropertySource(value = "classpath:testApplication.properties")
public class ESJavaAPITests {


    @Value("${ES.cluster.name}")
    private String    CLUSTER_NAME;

    @Value("${ES.host}")
    private String    HOSTNAME;

    @Value("${ES.port}")
    private Integer   HOST_PORT;

    private static final String BOOK_INDEX_NAME ="bookshop";
    private static final String BOOK_TYPE_NAME ="book";

    private Client client(){
        Settings settings = Settings.settingsBuilder()
                .put("cluster.name", CLUSTER_NAME)
                .build();

        return new TransportClient.Builder().settings(settings).build()
                .addTransportAddress(
                        new InetSocketTransportAddress(
                                new InetSocketAddress(HOSTNAME, HOST_PORT))
                );

    }

    @Test
    public void shouldSaveDocToPredefinedShard() throws IOException {
        //delete all indexes if any
        client().admin().indices().prepareDelete("_all").get();


        CreateIndexResponse createIndexRequestBuilder = client().admin().indices()
                .prepareCreate(BOOK_INDEX_NAME)
                .setSettings(
                        Settings.settingsBuilder()
                                .put("index.number_of_shards", 2)
                                .put("index.number_of_replicas", 2)
                )
                .execute()
                .actionGet();

        IndexResponse response1 = client().prepareIndex(BOOK_INDEX_NAME, BOOK_TYPE_NAME, "id1")
                .setSource(XContentFactory.jsonBuilder()
                        .startObject()
                            .field("title", "Clean COde")
                            .field("author", "John Smith")
                        .endObject()
                )
                .setRouting("route1")
                .get();

        IndexResponse response2 = client().prepareIndex(BOOK_INDEX_NAME, BOOK_TYPE_NAME, "id2")
                .setSource(XContentFactory.jsonBuilder()
                        .startObject()
                            .field("title", "Learn Scala")
                            .field("author", "John Doe")
                        .endObject()
                )
                .setRouting("route2")
                .get();
    }
}

这是我第一次运行时的效果。但是当我第二次跑它时,我得到了:

  

java.lang.IllegalStateException:无法加载ApplicationContext

     

在   org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)     在   org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)     在   org.springframework.boot.test.autoconfigure.AutoConfigureReportTestExecutionListener.prepareTestInstance(AutoConfigureReportTestExecutionListener.java:49)     在   org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)     在   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)at   org.springframework.test.context.junit4.SpringJUnit4ClassRunner $ 1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)     在   org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)     在   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)     在   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)     在   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)     在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)at at   org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)at at   org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)at at   org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)at at   org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)at at   org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)     在   org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)     在org.junit.runners.ParentRunner.run(ParentRunner.java:363)at   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)     在org.junit.runner.JUnitCore.run(JUnitCore.java:137)at   com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117)     在   com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)     在   com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在java.lang.reflect.Method.invoke(Method.java:498)at   com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)   引起:   org.springframework.beans.factory.UnsatisfiedDependencyException:   创建名为'bookServiceImpl'的bean时出错:不满意   通过方法'setBookRepository'参数0表示的依赖:   创建名为'bookRepository'的bean时出错:init的调用   方法失败;嵌套异常是   org.springframework.beans.BeanInstantiationException:失败   实例   [org.springframework.data.elasticsearch.repository.support.SimpleElasticsearchRepository]:   构造函数抛出异常;嵌套异常是   java.lang.IllegalArgumentException: [title]的Mapper与   其他类型的现有映射:[mapper [title]有不同的[store]   值]; 嵌套异常是   org.springframework.beans.factory.BeanCreationException:错误   创建名为'bookRepository'的bean:调用init方法   失败;嵌套异常是   org.springframework.beans.BeanInstantiationException:失败   实例   [org.springframework.data.elasticsearch.repository.support.SimpleElasticsearchRepository]:   构造函数抛出异常;嵌套异常是   java.lang.IllegalArgumentException:[title]的Mapper与   其他类型的现有映射:[mapper [title]有不同的[store]   价值观   org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor $ AutowiredMethodElement.inject

为什么在我第二次运行时这似乎是一个问题?

如何创建索引并使用以下方法添加两个示例文档:

  1. shard nr
  2. replica nr
  3. 索引名称
  4. 索引类型
  5. 新文件ID。

    使用Java API?

1 个答案:

答案 0 :(得分:0)

这里没有足够的代码来轻松解释这个问题,但你有一个名为“bookRepository”的存储库类,可能设置为自动配置。存储库不会被删除,所以当你第二次(第二次运行)重新创建索引时,它会将“book”类(假设)与现有ES索引的模式进行比较 - 而且你可能会'我们对Title字段进行了更改。

您可能最好手动清除图书索引(我假设它并不重要,因为您在上面的代码中有删除)并且看到连续两次运行应用程序(您的测试用例)仍然失败。