Skip to content

Commit

Permalink
HSEARCH-2434 Perform a refresh after flushes in the FlushWork for Ela…
Browse files Browse the repository at this point in the history
…sticsearch 5

This is necessary because the "refresh" parameter in the Flush API has
been removed in ES5.

See elasticsearch/elasticsearch:7cc48c8e8723d3b31fbcb371070bc2a8d87b1f7e
  • Loading branch information
yrodiere committed Mar 21, 2017
1 parent d3339e7 commit 6df3c59
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
import com.google.gson.JsonObject;

/**
* A flush work for ES2, using the Flush API.
*
* @author Yoann Rodiere
*/
public class FlushWork extends SimpleElasticsearchWork<Void> {
public class ES2FlushWork extends SimpleElasticsearchWork<Void> {

protected FlushWork(Builder builder) {
protected ES2FlushWork(Builder builder) {
super( builder );
}

Expand Down Expand Up @@ -59,8 +61,8 @@ protected ElasticsearchRequest buildRequest() {
}

@Override
public FlushWork build() {
return new FlushWork( this );
public ES2FlushWork build() {
return new ES2FlushWork( this );
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.elasticsearch.work.impl;

import java.util.ArrayList;
import java.util.List;

import org.elasticsearch.client.Response;
import org.hibernate.search.elasticsearch.work.impl.builder.FlushWorkBuilder;
import org.hibernate.search.elasticsearch.work.impl.builder.RefreshWorkBuilder;
import org.hibernate.search.elasticsearch.work.impl.factory.ElasticsearchWorkFactory;

import com.google.gson.JsonObject;

/**
* A flush work for ES5, using the Flush API then the Refresh API.
* <p>
* This is necessary because the "refresh" parameter in the Flush API has been removed
* in ES5 (elasticsearch/elasticsearch:7cc48c8e8723d3b31fbcb371070bc2a8d87b1f7e).
*
* @author Yoann Rodiere
*/
public class ES5FlushWork extends SimpleElasticsearchWork<Void> {

private final ElasticsearchWork<?> refreshWork;

protected ES5FlushWork(Builder builder) {
super( builder );
this.refreshWork = builder.buildRefreshWork();
}

@Override
protected void afterSuccess(ElasticsearchWorkExecutionContext executionContext) {
super.afterSuccess( executionContext );
refreshWork.execute( executionContext );
}

@Override
protected Void generateResult(ElasticsearchWorkExecutionContext context, Response response, JsonObject parsedResponseBody) {
return null;
}

public static class Builder
extends SimpleElasticsearchWork.Builder<Builder>
implements FlushWorkBuilder {
private final RefreshWorkBuilder refreshWorkBuilder;

private List<String> indexNames = new ArrayList<>();

public Builder(ElasticsearchWorkFactory workFactory) {
super( null, DefaultElasticsearchRequestSuccessAssessor.INSTANCE );
this.refreshWorkBuilder = workFactory.refresh();
}

@Override
public Builder index(String indexName) {
this.indexNames.add( indexName );
this.refreshWorkBuilder.index( indexName );
return this;
}

@Override
protected ElasticsearchRequest buildRequest() {
ElasticsearchRequest.Builder builder =
ElasticsearchRequest.post();

if ( !indexNames.isEmpty() ) {
builder.multiValuedPathComponent( indexNames );
}

builder.pathComponent( "_flush" );

return builder.build();
}

protected ElasticsearchWork<?> buildRefreshWork() {
return refreshWorkBuilder.build();
}

@Override
public ES5FlushWork build() {
return new ES5FlushWork( this );
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import org.hibernate.search.elasticsearch.work.impl.DeleteWork;
import org.hibernate.search.elasticsearch.work.impl.DropIndexWork;
import org.hibernate.search.elasticsearch.work.impl.ExplainWork;
import org.hibernate.search.elasticsearch.work.impl.FlushWork;
import org.hibernate.search.elasticsearch.work.impl.ES2FlushWork;
import org.hibernate.search.elasticsearch.work.impl.GetIndexSettingsWork;
import org.hibernate.search.elasticsearch.work.impl.GetIndexTypeMappingsWork;
import org.hibernate.search.elasticsearch.work.impl.IndexExistsWork;
Expand Down Expand Up @@ -87,7 +87,7 @@ public DeleteByQueryWorkBuilder deleteByQuery(String indexName, JsonObject paylo

@Override
public FlushWorkBuilder flush() {
return new FlushWork.Builder();
return new ES2FlushWork.Builder();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
package org.hibernate.search.elasticsearch.work.impl.factory;

import org.hibernate.search.elasticsearch.gson.impl.GsonProvider;
import org.hibernate.search.elasticsearch.work.impl.ES5FlushWork;
import org.hibernate.search.elasticsearch.work.impl.builder.FlushWorkBuilder;

/**
* @author Yoann Rodiere
Expand All @@ -17,4 +19,8 @@ public Elasticsearch5WorkFactory(GsonProvider gsonProvider) {
super( gsonProvider );
}

@Override
public FlushWorkBuilder flush() {
return new ES5FlushWork.Builder( this );
}
}

0 comments on commit 6df3c59

Please sign in to comment.