gpt4 book ai didi

java - Elasticsearch查询不会间歇性地获取结果

转载 作者:行者123 更新时间:2023-12-01 22:28:54 29 4
gpt4 key购买 nike

当我创建索引并立即搜索它时,我不会间歇性地返回结果。然而,作为回应,它说索引已创建。 Elastic Search 是否需要一些时间才能使索引可搜索?

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

Elasticsearch 中的保证是什么?搜索?

最佳答案

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

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

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

  1. Apply refresh on the index在搜索之前手动进行
  2. 索引时,对于您想要立即搜索的文档,enable the refresh flag
  3. 更改 default refresh interval

关于java - Elasticsearch查询不会间歇性地获取结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28201237/

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