mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-03-27 10:28:28 +00:00
more work on the groovy client
This commit is contained in:
parent
fcb99b4d9b
commit
7e041c43e0
@ -38,8 +38,8 @@ import static org.elasticsearch.action.Actions.*;
|
||||
* A request to delete all documents that matching a specific query. Best created with
|
||||
* {@link org.elasticsearch.client.Requests#deleteByQueryRequest(String...)}.
|
||||
*
|
||||
* <p>The request requires the query source to be set either using {@link #querySource(org.elasticsearch.index.query.QueryBuilder)},
|
||||
* or {@link #querySource(byte[])}.
|
||||
* <p>The request requires the query source to be set either using {@link #query(org.elasticsearch.index.query.QueryBuilder)},
|
||||
* or {@link #query(byte[])}.
|
||||
*
|
||||
* @author kimchy (shay.banon)
|
||||
* @see DeleteByQueryResponse
|
||||
@ -79,6 +79,11 @@ public class DeleteByQueryRequest extends IndicesReplicationOperationRequest {
|
||||
return validationException;
|
||||
}
|
||||
|
||||
public DeleteByQueryRequest indices(String... indices) {
|
||||
this.indices = indices;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* The query source to execute.
|
||||
*/
|
||||
@ -91,22 +96,22 @@ public class DeleteByQueryRequest extends IndicesReplicationOperationRequest {
|
||||
*
|
||||
* @see org.elasticsearch.index.query.json.JsonQueryBuilders
|
||||
*/
|
||||
@Required public DeleteByQueryRequest querySource(QueryBuilder queryBuilder) {
|
||||
return querySource(queryBuilder.buildAsBytes());
|
||||
@Required public DeleteByQueryRequest query(QueryBuilder queryBuilder) {
|
||||
return query(queryBuilder.buildAsBytes());
|
||||
}
|
||||
|
||||
/**
|
||||
* The query source to execute. It is preferable to use either {@link #querySource(byte[])}
|
||||
* or {@link #querySource(org.elasticsearch.index.query.QueryBuilder)}.
|
||||
* The query source to execute. It is preferable to use either {@link #query(byte[])}
|
||||
* or {@link #query(org.elasticsearch.index.query.QueryBuilder)}.
|
||||
*/
|
||||
@Required public DeleteByQueryRequest querySource(String querySource) {
|
||||
return querySource(Unicode.fromStringAsBytes(querySource));
|
||||
@Required public DeleteByQueryRequest query(String querySource) {
|
||||
return query(Unicode.fromStringAsBytes(querySource));
|
||||
}
|
||||
|
||||
/**
|
||||
* The query source to execute.
|
||||
*/
|
||||
@Required public DeleteByQueryRequest querySource(byte[] querySource) {
|
||||
@Required public DeleteByQueryRequest query(byte[] querySource) {
|
||||
this.querySource = querySource;
|
||||
return this;
|
||||
}
|
||||
|
@ -55,6 +55,13 @@ public class DeleteByQueryResponse implements ActionResponse, Streamable, Iterab
|
||||
return indices;
|
||||
}
|
||||
|
||||
/**
|
||||
* The responses from all the different indices.
|
||||
*/
|
||||
public Map<String, IndexDeleteByQueryResponse> getIndices() {
|
||||
return indices;
|
||||
}
|
||||
|
||||
/**
|
||||
* The response of a specific index.
|
||||
*/
|
||||
|
@ -56,6 +56,13 @@ public class IndexDeleteByQueryResponse implements ActionResponse, Streamable {
|
||||
return this.index;
|
||||
}
|
||||
|
||||
/**
|
||||
* The index the delete by query operation was executed against.
|
||||
*/
|
||||
public String getIndex() {
|
||||
return index;
|
||||
}
|
||||
|
||||
/**
|
||||
* The total number of shards the delete by query was executed on.
|
||||
*/
|
||||
@ -63,6 +70,13 @@ public class IndexDeleteByQueryResponse implements ActionResponse, Streamable {
|
||||
return failedShards + successfulShards;
|
||||
}
|
||||
|
||||
/**
|
||||
* The total number of shards the delete by query was executed on.
|
||||
*/
|
||||
public int getTotalShards() {
|
||||
return totalShards();
|
||||
}
|
||||
|
||||
/**
|
||||
* The successful number of shards the delete by query was executed on.
|
||||
*/
|
||||
@ -70,6 +84,13 @@ public class IndexDeleteByQueryResponse implements ActionResponse, Streamable {
|
||||
return successfulShards;
|
||||
}
|
||||
|
||||
/**
|
||||
* The successful number of shards the delete by query was executed on.
|
||||
*/
|
||||
public int getSuccessfulShards() {
|
||||
return successfulShards;
|
||||
}
|
||||
|
||||
/**
|
||||
* The failed number of shards the delete by query was executed on.
|
||||
*/
|
||||
@ -77,6 +98,13 @@ public class IndexDeleteByQueryResponse implements ActionResponse, Streamable {
|
||||
return failedShards;
|
||||
}
|
||||
|
||||
/**
|
||||
* The failed number of shards the delete by query was executed on.
|
||||
*/
|
||||
public int getFailedShards() {
|
||||
return failedShards;
|
||||
}
|
||||
|
||||
@Override public void readFrom(StreamInput in) throws IOException {
|
||||
index = in.readUTF();
|
||||
successfulShards = in.readVInt();
|
||||
|
@ -54,7 +54,7 @@ public class RestDeleteByQueryAction extends BaseRestHandler {
|
||||
// we just build a response and send it, no need to fork a thread
|
||||
deleteByQueryRequest.listenerThreaded(false);
|
||||
try {
|
||||
deleteByQueryRequest.querySource(RestActions.parseQuerySource(request));
|
||||
deleteByQueryRequest.query(RestActions.parseQuerySource(request));
|
||||
deleteByQueryRequest.queryParserName(request.param("query_parser_name"));
|
||||
String typesParam = request.param("type");
|
||||
if (typesParam != null) {
|
||||
|
@ -194,7 +194,7 @@ public class DocumentActionsTests extends AbstractNodesTests {
|
||||
}
|
||||
|
||||
logger.info("Delete by query");
|
||||
DeleteByQueryResponse queryResponse = client2.deleteByQuery(deleteByQueryRequest("test").querySource(termQuery("name", "test2"))).actionGet();
|
||||
DeleteByQueryResponse queryResponse = client2.deleteByQuery(deleteByQueryRequest("test").query(termQuery("name", "test2"))).actionGet();
|
||||
assertThat(queryResponse.index(getConcreteIndexName()).successfulShards(), equalTo(5));
|
||||
assertThat(queryResponse.index(getConcreteIndexName()).failedShards(), equalTo(0));
|
||||
client1.admin().indices().refresh(refreshRequest("test")).actionGet();
|
||||
|
@ -1,22 +1,20 @@
|
||||
package org.elasticsearch.groovy.client
|
||||
|
||||
import org.elasticsearch.client.internal.InternalClient
|
||||
|
||||
/**
|
||||
* @author kimchy (shay.banon)
|
||||
*/
|
||||
class GAdminClient {
|
||||
|
||||
private final InternalClient internalClient;
|
||||
private final GClient gClient;
|
||||
|
||||
final GIndicesAdminClient indices;
|
||||
|
||||
final GClusterAdminClient cluster;
|
||||
|
||||
def GAdminClient(internalClient) {
|
||||
this.internalClient = internalClient;
|
||||
def GAdminClient(gClient) {
|
||||
this.gClient = gClient;
|
||||
|
||||
this.indices = new GIndicesAdminClient(internalClient)
|
||||
this.cluster = new GClusterAdminClient(internalClient)
|
||||
this.indices = new GIndicesAdminClient(gClient)
|
||||
this.cluster = new GClusterAdminClient(gClient)
|
||||
}
|
||||
}
|
||||
|
@ -22,6 +22,8 @@ package org.elasticsearch.groovy.client
|
||||
import org.elasticsearch.action.ActionListener
|
||||
import org.elasticsearch.action.delete.DeleteRequest
|
||||
import org.elasticsearch.action.delete.DeleteResponse
|
||||
import org.elasticsearch.action.deletebyquery.DeleteByQueryRequest
|
||||
import org.elasticsearch.action.deletebyquery.DeleteByQueryResponse
|
||||
import org.elasticsearch.action.get.GetRequest
|
||||
import org.elasticsearch.action.get.GetResponse
|
||||
import org.elasticsearch.action.index.IndexRequest
|
||||
@ -43,6 +45,13 @@ class GClient {
|
||||
IndexRequest.metaClass.source = {Closure c ->
|
||||
delegate.source(new JsonBuilder().buildAsBytes(c))
|
||||
}
|
||||
|
||||
DeleteByQueryRequest.metaClass.setQuery = {Closure c ->
|
||||
delegate.query(new JsonBuilder().buildAsBytes(c))
|
||||
}
|
||||
DeleteByQueryRequest.metaClass.query = {Closure c ->
|
||||
delegate.query(new JsonBuilder().buildAsBytes(c))
|
||||
}
|
||||
}
|
||||
|
||||
final Client client;
|
||||
@ -55,12 +64,13 @@ class GClient {
|
||||
this.client = client;
|
||||
this.internalClient = client;
|
||||
|
||||
this.admin = new GAdminClient(internalClient)
|
||||
this.admin = new GAdminClient(this)
|
||||
}
|
||||
|
||||
GActionFuture<IndexResponse> index(Closure c) {
|
||||
IndexRequest request = new IndexRequest()
|
||||
c.setDelegate request
|
||||
c.resolveStrategy = Closure.DELEGATE_FIRST
|
||||
c.call()
|
||||
index(request)
|
||||
}
|
||||
@ -78,6 +88,7 @@ class GClient {
|
||||
GActionFuture<GetResponse> get(Closure c) {
|
||||
GetRequest request = new GetRequest()
|
||||
c.setDelegate request
|
||||
c.resolveStrategy = Closure.DELEGATE_FIRST
|
||||
c.call()
|
||||
get(request)
|
||||
}
|
||||
@ -94,6 +105,7 @@ class GClient {
|
||||
|
||||
GActionFuture<DeleteResponse> delete(Closure c) {
|
||||
DeleteRequest request = new DeleteRequest()
|
||||
c.resolveStrategy = Closure.DELEGATE_FIRST
|
||||
c.setDelegate request
|
||||
c.call()
|
||||
delete(request)
|
||||
@ -108,4 +120,22 @@ class GClient {
|
||||
void delete(DeleteRequest request, ActionListener<DeleteResponse> listener) {
|
||||
client.delete(request, listener)
|
||||
}
|
||||
|
||||
GActionFuture<DeleteByQueryResponse> deleteByQuery(Closure c) {
|
||||
DeleteByQueryRequest request = new DeleteByQueryRequest()
|
||||
c.resolveStrategy = Closure.DELEGATE_FIRST
|
||||
c.setDelegate request
|
||||
c.call()
|
||||
deleteByQuery(request)
|
||||
}
|
||||
|
||||
GActionFuture<DeleteByQueryResponse> deleteByQuery(DeleteByQueryRequest request) {
|
||||
GActionFuture<DeleteByQueryResponse> future = new GActionFuture<DeleteByQueryResponse>(internalClient.threadPool(), request);
|
||||
client.deleteByQuery(request, future)
|
||||
return future
|
||||
}
|
||||
|
||||
void deleteByQuery(DeleteByQueryRequest request, ActionListener<DeleteByQueryResponse> listener) {
|
||||
client.deleteByQuery(request, listener)
|
||||
}
|
||||
}
|
||||
|
@ -8,12 +8,15 @@ import org.elasticsearch.client.internal.InternalClient
|
||||
*/
|
||||
class GClusterAdminClient {
|
||||
|
||||
private final GClient gClient
|
||||
|
||||
private final InternalClient internalClient;
|
||||
|
||||
private final ClusterAdminClient clusterAdminClient;
|
||||
final ClusterAdminClient clusterAdminClient;
|
||||
|
||||
def GClusterAdminClient(internalClient) {
|
||||
this.internalClient = internalClient;
|
||||
def GClusterAdminClient(gClient) {
|
||||
this.gClient = gClient;
|
||||
this.internalClient = gClient.client;
|
||||
this.clusterAdminClient = internalClient.admin().cluster();
|
||||
}
|
||||
}
|
||||
|
@ -12,18 +12,22 @@ import org.elasticsearch.groovy.client.action.GActionFuture
|
||||
*/
|
||||
class GIndicesAdminClient {
|
||||
|
||||
private final GClient gClient
|
||||
|
||||
private final InternalClient internalClient;
|
||||
|
||||
private final IndicesAdminClient indicesAdminClient;
|
||||
final IndicesAdminClient indicesAdminClient;
|
||||
|
||||
def GIndicesAdminClient(internalClient) {
|
||||
this.internalClient = internalClient;
|
||||
def GIndicesAdminClient(gClient) {
|
||||
this.gClient = gClient
|
||||
this.internalClient = gClient.client
|
||||
this.indicesAdminClient = internalClient.admin().indices();
|
||||
}
|
||||
|
||||
GActionFuture<RefreshResponse> refresh(Closure c) {
|
||||
RefreshRequest request = new RefreshRequest()
|
||||
c.setDelegate request
|
||||
c.resolveStrategy = Closure.DELEGATE_FIRST
|
||||
c.call()
|
||||
refresh(request)
|
||||
}
|
||||
|
@ -132,5 +132,47 @@ class SimpleActionsTests extends GroovyTestCase {
|
||||
id "1"
|
||||
}
|
||||
assertFalse getR.response.exists
|
||||
|
||||
indexR = node.client.index {
|
||||
index "test"
|
||||
type "type1"
|
||||
id "1"
|
||||
source {
|
||||
test = "value"
|
||||
complex {
|
||||
value1 = "value1"
|
||||
value2 = "value2"
|
||||
}
|
||||
}
|
||||
}
|
||||
assertEquals "1", indexR.response.id
|
||||
|
||||
refresh = node.client.admin.indices.refresh {}
|
||||
assertEquals 0, refresh.response.failedShards
|
||||
|
||||
getR = node.client.get {
|
||||
index "test"
|
||||
type "type1"
|
||||
id "1"
|
||||
}
|
||||
assertTrue getR.response.exists
|
||||
|
||||
def deleteByQuery = node.client.deleteByQuery {
|
||||
indices "test"
|
||||
query {
|
||||
term("test": "value")
|
||||
}
|
||||
}
|
||||
assertEquals 0, deleteByQuery.response.indices.test.failedShards
|
||||
|
||||
refresh = node.client.admin.indices.refresh {}
|
||||
assertEquals 0, refresh.response.failedShards
|
||||
|
||||
getR = node.client.get {
|
||||
index "test"
|
||||
type "type1"
|
||||
id "1"
|
||||
}
|
||||
assertFalse getR.response.exists
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user