org.hibernate.search.elasticsearch.client.impl.ElasticsearchRequest Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hibernate-search-elasticsearch Show documentation
Show all versions of hibernate-search-elasticsearch Show documentation
Hibernate Search backend which has indexing operations forwarded to Elasticsearch
The newest version!
/*
* 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 .
*/
package org.hibernate.search.elasticsearch.client.impl;
import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import com.google.gson.JsonObject;
/**
* @author Yoann Rodiere
*/
public final class ElasticsearchRequest {
public static Builder put() {
return new Builder( "PUT" );
}
public static Builder get() {
return new Builder( "GET" );
}
public static Builder post() {
return new Builder( "POST" );
}
public static Builder delete() {
return new Builder( "DELETE" );
}
public static Builder head() {
return new Builder( "HEAD" );
}
private final String method;
private final String path;
private final Map parameters;
private final List bodyParts;
private ElasticsearchRequest(Builder builder) {
this.method = builder.method;
this.path = builder.pathBuilder.toString();
this.parameters = builder.parameters == null ? Collections.emptyMap() : Collections.unmodifiableMap( builder.parameters );
this.bodyParts = builder.bodyParts == null ? Collections.emptyList() : Collections.unmodifiableList( builder.bodyParts );
}
public String getMethod() {
return method;
}
public String getPath() {
return path;
}
public Map getParameters() {
return parameters;
}
public List getBodyParts() {
return bodyParts;
}
@Override
public String toString() {
return new StringBuilder( getClass().getSimpleName() )
.append( "[" )
.append( "method=" ).append( method )
.append( ", path=" ).append( path )
.append( ", parameters=" ).append( parameters )
.append( "]" )
.toString();
}
public static final class Builder {
private static final char PATH_SEPARATOR = '/';
private final String method;
private final StringBuilder pathBuilder = new StringBuilder( 20 );
private Map parameters;
private List bodyParts;
private Builder(String method) {
super();
this.method = method;
}
public Builder pathComponent(URLEncodedString pathComponent) {
pathBuilder.append( PATH_SEPARATOR ).append( pathComponent.encoded );
return this;
}
public Builder multiValuedPathComponent(Iterable indexNames) {
boolean first = true;
for ( URLEncodedString name : indexNames ) {
if ( !first ) {
pathBuilder.append( ',' );
}
else {
pathBuilder.append( PATH_SEPARATOR );
first = false;
}
pathBuilder.append( name.encoded );
}
return this;
}
public Builder param(String name, String value) {
if ( parameters == null ) {
parameters = new LinkedHashMap<>();
}
parameters.put( name, value );
return this;
}
public Builder param(String name, int value) {
return param( name, String.valueOf( value ) );
}
public Builder param(String name, boolean value) {
return param( name, String.valueOf( value ) );
}
public Builder body(JsonObject object) {
if ( bodyParts == null ) {
bodyParts = new ArrayList<>();
}
bodyParts.add( object );
return this;
}
public ElasticsearchRequest build() {
return new ElasticsearchRequest( this );
}
}
}