small improvements to groovy client, allows for closure in the search request

This commit is contained in:
kimchy 2010-07-25 10:32:22 +03:00
parent 747aa2e30a
commit 457b56937e
7 changed files with 100 additions and 0 deletions

View File

@ -89,6 +89,16 @@ public class CountRequestBuilder extends BaseRequestBuilder<CountRequest, CountR
return this;
}
/**
* The query source to execute.
*
* @see org.elasticsearch.index.query.xcontent.QueryBuilders
*/
public CountRequestBuilder setQuery(byte[] querySource) {
request.query(querySource);
return this;
}
/**
* Controls the operation threading model.
*/

View File

@ -169,6 +169,14 @@ public class SearchRequestBuilder extends BaseRequestBuilder<SearchRequest, Sear
return this;
}
/**
* Constructs a new search source builder with a raw search query.
*/
public SearchRequestBuilder setQuery(byte[] queryBinary) {
sourceBuilder().query(queryBinary);
return this;
}
/**
* From index to start the search from. Defaults to <tt>0</tt>.
*/

View File

@ -70,6 +70,8 @@ public class SearchSourceBuilder implements ToXContent {
private XContentQueryBuilder queryBuilder;
private byte[] queryBinary;
private int from = -1;
private int size = -1;
@ -107,6 +109,14 @@ public class SearchSourceBuilder implements ToXContent {
return this;
}
/**
* Constructs a new search source builder with a raw search query.
*/
public SearchSourceBuilder query(byte[] queryBinary) {
this.queryBinary = queryBinary;
return this;
}
/**
* From index to start the search from. Defaults to <tt>0</tt>.
*/
@ -310,6 +320,15 @@ public class SearchSourceBuilder implements ToXContent {
queryBuilder.toXContent(builder, params);
}
if (queryBinary != null) {
if (XContentFactory.xContentType(queryBinary) == builder.contentType()) {
builder.rawField("query", queryBinary);
} else {
builder.field("query_binary");
builder.value(queryBinary);
}
}
if (explain != null) {
builder.field("explain", explain);
}

View File

@ -0,0 +1,41 @@
/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.elasticsearch.search.query;
import org.apache.lucene.search.Query;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.index.query.xcontent.XContentIndexQueryParser;
import org.elasticsearch.search.SearchParseElement;
import org.elasticsearch.search.internal.SearchContext;
/**
* @author kimchy (shay.banon)
*/
public class QueryBinaryParseElement implements SearchParseElement {
@Override public void parse(XContentParser parser, SearchContext context) throws Exception {
XContentIndexQueryParser indexQueryParser = (XContentIndexQueryParser) context.queryParser();
byte[] querySource = parser.binaryValue();
XContentParser qSourceParser = XContentFactory.xContent(querySource).createParser(querySource);
Query query = indexQueryParser.parse(qSourceParser);
context.query(query);
}
}

View File

@ -55,6 +55,8 @@ public class QueryPhase implements SearchPhase {
.put("indices_boost", new IndicesBoostParseElement())
.put("indicesBoost", new IndicesBoostParseElement())
.put("query", new QueryParseElement())
.put("queryBinary", new QueryBinaryParseElement())
.put("query_binary", new QueryBinaryParseElement())
.put("sort", new SortParseElement())
.putAll(facetsPhase.parseElements());
return parseElements.build();

View File

@ -121,6 +121,12 @@ class GClient {
SearchRequestBuilder.metaClass.extraSource = {Closure c ->
delegate.setExtraSource(new GXContentBuilder().buildAsBytes(c, contentType))
}
SearchRequestBuilder.metaClass.setQuery = {Closure c ->
delegate.setQuery(new GXContentBuilder().buildAsBytes(c, contentType))
}
SearchRequestBuilder.metaClass.query = {Closure c ->
delegate.setQuery(new GXContentBuilder().buildAsBytes(c, contentType))
}
MoreLikeThisRequest.metaClass.setSearchSource = {Closure c ->
delegate.searchSource(new GXContentBuilder().buildAsBytes(c, contentType))

View File

@ -67,6 +67,20 @@ class BuilderActionsTests {
assertThat indexR.response.type, equalTo("type1")
assertThat indexR.response.id, equalTo("1")
node.client.admin.indices.refresh {}.actionGet()
def countR = node.client.prepareCount("test").setQuery({
term(test: "value")
}).gexecute();
assertThat countR.response.count, equalTo(1l)
def searchR = node.client.prepareSearch("test").setQuery({
term(test: "value")
}).gexecute();
assertThat searchR.response.hits.totalHits, equalTo(1l)
def delete = node.client.prepareDelete("test", "type1", "1").gexecute()
assertThat delete.response.index, equalTo("test")
assertThat delete.response.type, equalTo("type1")