简体   繁体   English

ElasticSearch:更新紧跟查询(Java API)

[英]ElasticSearch: Update followed closely by query (Java API)

We are using elasticsearch. 我们正在使用elasticsearch。 Essentially, I need to insert a record and then query against the data and reliably return the recently inserted row. 本质上,我需要插入一条记录,然后查询数据并可靠地返回最近插入的行。 (This is an import tool that imports hierarchical data, and as the entities are inserted into ES, the tool needs to check for existing nodes that new nodes refer to). (这是一个导入工具,用于导入分层数据,并且在将实体插入ES中时,该工具需要检查新节点引用的现有节点)。

I have tried a few things. 我已经尝试了几件事。

First, if I delete and rebuild the index on every insert, it works (obviously can't do this in the real world). 首先,如果我删除并重建每个插入的索引,它就可以工作(在现实世界中显然无法做到这一点)。

Second, I have tried setting the refresh_interval to -1: 其次,我尝试将refresh_interval设置为-1:

 settings_map.put("index.refresh_interval", "-1");
      org.elasticsearch.common.settings.Settings settings = ImmutableSettings.settingsBuilder().put(settings_map)
          .build();

      UpdateSettingsRequestBuilder usrb = es.admin().indices()
          .prepareUpdateSettings();
      usrb.setIndices("pm");
      usrb.setSettings(settings);
      usrb.execute().actionGet();

Third, I have tried settings the threading option to false: 第三,我尝试将threading选项设置为false:

tc.prepareUpdate(domain, type, id)).setListenerThreaded(false)...

I've also seen the setOperationThreaded setting, but that does not appear to be available on prepareUpdate(). 我也看到过setOperationThreaded设置,但是在prepareUpdate()上似乎不可用。


None of these seem to have the desired effect. 这些似乎都没有达到预期的效果。

The desired effect being: After inserting a record into ElasticSearch, when immediately running a query that should return that record, reliably have the record in the results. 所需的效果是:将记录插入ElasticSearch之后,立即运行应返回该记录的查询时,将记录可靠地包含在结果中。

Setting the refresh_interval at -1 actually disabled automatic refresh. refresh_interval设置为-1实际上会禁用自动刷新。

You can achieve the desired effect by setting the refresh parameter to true like this : 您可以通过将refresh参数设置为true来达到预期的效果,如下所示:

client.prepareUpdate(...).setRefresh(true).execute().actionGet();

This way, a refresh operation will be performed immediately after your update and the updated document will be searchable as you wish. 这样,更新操作将在更新后立即执行,并且可以根据需要搜索更新的文档。

Beware : the refresh operation is costly and should not be performed while indexing lots of documents. 当心 :刷新操作的成本很高,在索引大量文档时不应执行刷新操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM