eclipse IndexNotFoundException[没有这样的索引]

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35113565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 23:02:12  来源:igfitidea点击:

IndexNotFoundException[no such index]

javaeclipseelasticsearch

提问by Lance Zhao Zai

I was running my first elasticsearch test case, I am using Java as the solution perspective to do elasticsearch experiment. it works perfectly fine in eclipse Debug Mode,

我正在运行我的第一个 elasticsearch 测试用例,我使用 Java 作为解决方案的视角来进行 elasticsearch 实验。它在 Eclipse 调试模式下运行良好,

the debug mode result:

调试模式结果:

{postDate=2016-01-31T10:32:58.952Z, title=Posting, content=today's weather is hot, tags=[hashtag]}

But when I try this on in normal Run application mode, I am getting the following exception and I have no idea at all. Please guide me.

但是当我在正常的运行应用程序模式下尝试这个时,我收到以下异常并且我完全不知道。请指导我。

The following exception:

以下异常:

8253 [main] INFO  org.elasticsearch.node  - [Marc Spector] started
8257 [elasticsearch[Marc Spector][clusterService#updateTask][T#1]] DEBUG org.elasticsearch.index.store  - [Marc Spector] [facebook] using index.store.throttle.type [none], with index.store.throttle.max_bytes_per_sec [0b]
8273 [elasticsearch[Marc Spector][search][T#4]] DEBUG org.elasticsearch.action.search.type  - [Marc Spector] All shards failed for phase: [query]
RemoteTransportException[[Marc Spector][127.0.0.1:9300][indices:data/read/search[phase/query]]]; nested: IndexNotFoundException[no such index];
Caused by: [facebook] IndexNotFoundException[no such index]
    at org.elasticsearch.indices.IndicesService.indexServiceSafe(IndicesService.java:310)
    at org.elasticsearch.search.SearchService.createContext(SearchService.java:635)
    at org.elasticsearch.search.SearchService.createAndPutContext(SearchService.java:617)
    at org.elasticsearch.search.SearchService.executeQueryPhase(SearchService.java:368)
    at org.elasticsearch.search.action.SearchServiceTransportAction$SearchQueryTransportHandler.messageReceived(SearchServiceTransportAction.java:368)
    at org.elasticsearch.search.action.SearchServiceTransportAction$SearchQueryTransportHandler.messageReceived(SearchServiceTransportAction.java:365)
    at org.elasticsearch.transport.TransportService.doRun(TransportService.java:350)
    at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:37)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Exception in thread "main" Failed to execute phase [query], all shards failed
    at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.onFirstPhaseResult(TransportSearchTypeAction.java:228)
    at org.elasticsearch.action.search.type.TransportSearchTypeAction$BaseAsyncAction.onFailure(TransportSearchTypeAction.java:174)
    at org.elasticsearch.action.ActionListenerResponseHandler.handleException(ActionListenerResponseHandler.java:46)
    at org.elasticsearch.transport.TransportService$DirectResponseChannel.processException(TransportService.java:821)
    at org.elasticsearch.transport.TransportService$DirectResponseChannel.sendResponse(TransportService.java:799)
    at org.elasticsearch.transport.TransportService.onFailure(TransportService.java:361)
    at org.elasticsearch.common.util.concurrent.AbstractRunnable.run(AbstractRunnable.java:42)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
8278 [elasticsearch[Marc Spector][clusterService#updateTask][T#1]] DEBUG org.elasticsearch.index.mapper  - [Marc Spector] [facebook] using dynamic[true]

The I think showing source code can be more clear to the issue

我认为显示源代码可以更清楚地解决问题

Source:

来源:

Node node = nodeBuilder().clusterName("testing2").node(); 
        Client client = node.client();

SearchResponse response = client.prepareSearch("facebook")
                .setTypes("Lance")
                .setSearchType(SearchType.QUERY_THEN_FETCH)
                .setQuery(QueryBuilders.matchPhrasePrefixQuery("title", "Pos"))
                .setFrom(0).setSize(60).setExplain(true)
                .execute()
                .actionGet();

        SearchHit[] searchResponse = response.getHits().getHits();

        for(SearchHit hit : searchResponse){
            System.out.println(hit.getSource());
        }

回答by Thibault Clement

Before querying your facebook index, you need to create it first:

在查询您的 facebook 索引之前,您需要先创建它:

Settings indexSettings = ImmutableSettings.settingsBuilder()
                 .put("number_of_shards", 5)
                 .put("number_of_replicas", 1)
                 .build();
CreateIndexRequest indexRequest = new CreateIndexRequest("facebook", indexSettings);
client.admin().indices().create(indexRequest).actionGet();

And if you expect to find some results, you need to index your data also:

如果您希望找到一些结果,您还需要索引您的数据:

IndexResponse response = client.prepareIndex("facebook", "Lance", "1")
        .setSource(jsonBuilder()
                    .startObject()
                        .field("title", "Posting")
                        .field("postDate", new Date())
                        .field("content", "today's weather is hot")
                        .field("tags", Lists.newArrayList("hashtag"))
                    .endObject()
                  )
        .execute()
        .actionGet();

Then you can search on your index.

然后你可以搜索你的索引。

Hope this help

希望这有帮助