弹性搜索查询不会间歇性地获取结果

Elastic search query does not fetch results intermittently

当我创建索引并立即搜索它时,我无法间歇性地返回结果。但是,作为回应,它说索引已创建。弹性搜索是否需要一些时间才能使索引可搜索?

package in.blogspot.randomcompiler.elastic_search_demo;

import in.blogspot.randomcompiler.elastic_search_impl.Event;

import java.util.Date;

import org.elasticsearch.action.count.CountRequestBuilder;
import org.elasticsearch.action.count.CountResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.FilterBuilder;
import org.elasticsearch.index.query.FilterBuilders;
import org.elasticsearch.index.query.MatchAllFilterBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;

import com.fasterxml.jackson.core.JsonProcessingException;

public class ElasticSearchDemo
{
    public static void main( String[] args ) throws JsonProcessingException
    {
        Client client = new TransportClient()
        .addTransportAddress(new InetSocketTransportAddress("localhost", 9300));

        DeleteResponse deleteResponse1 = client.prepareDelete("chat-data", "event", "1").execute().actionGet();
        DeleteResponse deleteResponse2 = client.prepareDelete("chat-data", "event", "2").execute().actionGet();
        DeleteResponse deleteResponse3 = client.prepareDelete("chat-data", "event", "3").execute().actionGet();

        Event e1 = new Event("LOGIN", new Date(), "Agent1 logged into chat");
        String e1Json = e1.prepareJson();        
        System.out.println("JSON: " + e1Json);        
        IndexResponse indexResponse1 = client.prepareIndex("chat-data", "event", "1").setSource(e1Json).execute().actionGet();
        printIndexResponse("e1", indexResponse1);

        Event e2 = new Event("LOGOUT", new Date(), "Agent1 logged out of chat");
        String e2Json = e2.prepareJson();        
        System.out.println("JSON: " + e2Json);        
        IndexResponse indexResponse2 = client.prepareIndex("chat-data", "event", "2").setSource(e2Json).execute().actionGet();
        printIndexResponse("e2", indexResponse2);

        Event e3 = new Event("BREAK", new Date(), "Agent1 went on break in the middle of a chat");
        String e3Json = e3.prepareJson();        
        System.out.println("JSON: " + e3Json);        
        IndexResponse indexResponse3 = client.prepareIndex("chat-data", "event", "3").setSource(e3Json).execute().actionGet();
        printIndexResponse("e3", indexResponse3);

        SearchRequestBuilder searchBuilder = client.prepareSearch();
        QueryBuilder queryBuilder = QueryBuilders.matchQuery("value", "middle going");
        searchBuilder.setQuery(queryBuilder);

        CountRequestBuilder countBuilder = client.prepareCount();
        countBuilder.setQuery(QueryBuilders.constantScoreQuery(queryBuilder));

        CountResponse countResponse1 = countBuilder.execute().actionGet();
        System.out.println("HITS: " + countResponse1.getCount());

        SearchResponse searchResponse1 = searchBuilder.execute().actionGet();
        SearchHits hits = searchResponse1.getHits();
        for(int i=0; i<hits.hits().length; i++) {
            SearchHit hit = hits.getAt(i);
            System.out.println("[" + i + "] " + hit.getId() + " : " +hit.sourceAsString());
        }


        client.close();
    }

    private static void printIndexResponse(String description, IndexResponse response) {
        System.out.println("Index response for: " + description);
        System.out.println("Index name: " + response.getIndex());
        System.out.println("Index type: " + response.getType());
        System.out.println("Index id: " + response.getId());
        System.out.println("Index version: " + response.getVersion());
    }
}

输出:

Jan 29, 2015 1:22:06 AM org.elasticsearch.plugins.PluginsService <init>
INFO: [Diablo] loaded [], sites []
JSON: {"type":"LOGIN","date":1422474727304,"value":"Agent1 logged into chat"}
Index response for: e1
Index name: chat-data
Index type: event
Index id: 1
Index version: 184
JSON: {"type":"LOGOUT","date":1422474727360,"value":"Agent1 logged out of chat"}
Index response for: e2
Index name: chat-data
Index type: event
Index id: 2
Index version: 182
JSON: {"type":"BREAK","date":1422474727365,"value":"Agent1 went on break in the middle of a chat"}
Index response for: e3
Index name: chat-data
Index type: event
Index id: 3
Index version: 160
HITS: 1
[0] 3 : {"type":"BREAK","date":1422474716500,"value":"Agent1 went on break in the middle of a chat"}

间歇输出:

Jan 29, 2015 1:23:30 AM org.elasticsearch.plugins.PluginsService <init>
INFO: [Mekano] loaded [], sites []
JSON: {"type":"LOGIN","date":1422474811618,"value":"Agent1 logged into chat"}
Index response for: e1
Index name: chat-data
Index type: event
Index id: 1
Index version: 222
JSON: {"type":"LOGOUT","date":1422474811671,"value":"Agent1 logged out of chat"}
Index response for: e2
Index name: chat-data
Index type: event
Index id: 2
Index version: 220
JSON: {"type":"BREAK","date":1422474811673,"value":"Agent1 went on break in the middle of a chat"}
Index response for: e3
Index name: chat-data
Index type: event
Index id: 3
Index version: 198
HITS: 0

弹性搜索中的保证是什么w.r.t。搜索?

Elasticsearch 提供近乎实时的搜索功能。这意味着默认情况下 Elasticsearch 需要一些时间,直到索引文档可用于搜索。

这个使新文档可用于搜索的过程称为索引刷新。这是默认的 1 秒。这意味着在索引的新文档可供搜索之前最多需要 1 秒。但请记住,这些文档仍然可以使用 GET API 检索,但不能使用 _Search API.

进行搜索

您可以执行以下操作来更改此行为 -

  1. Apply refresh on the index 搜索前手动
  2. 在编制索引时,对于您希望立即可搜索的文档,enable the refresh flag
  3. 更改default refresh interval