gpt4 book ai didi

elasticsearch - 无法使用设置和源Java API创建索引

转载 作者:行者123 更新时间:2023-12-02 22:38:57 29 4
gpt4 key购买 nike

使用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: Failed to load ApplicationContext

at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124) at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83) at org.springframework.boot.test.autoconfigure.AutoConfigureReportTestExecutionListener.prepareTestInstance(AutoConfigureReportTestExecutionListener.java:49) at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:287) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94) at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288) at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268) at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61) at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70) at org.junit.runners.ParentRunner.run(ParentRunner.java:363) at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:117) at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234) at 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) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144) Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'bookServiceImpl': Unsatisfied dependency expressed through method 'setBookRepository' parameter 0: Error creating bean with name 'bookRepository': Invocation of init method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.elasticsearch.repository.support.SimpleElasticsearchRepository]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Mapper for [title] conflicts with existing mapping in other types: [mapper [title] has different [store] values]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'bookRepository': Invocation of init method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.elasticsearch.repository.support.SimpleElasticsearchRepository]: Constructor threw exception; nested exception is java.lang.IllegalArgumentException: Mapper for [title] conflicts with existing mapping in other types: [mapper [title] has different [store] values] at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject



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

如何使用以下方法创建索引并添加两个示例性文档:
  • shard nr
  • 复制副本nr
  • 索引名称
  • 索引类型
  • 新文档ID。

    使用Java API?
  • 最佳答案

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

    您可能最好手动清除book索引(我假设它并不重要,因为上面的代码中有一个删除),然后查看是否连续两次运行该应用程序(您的测试用例)仍然失败。

    关于elasticsearch - 无法使用设置和源Java API创建索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39201345/

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