mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-03-26 18:08:36 +00:00
small improvements to groovy client, allows for closure in the search request
This commit is contained in:
parent
747aa2e30a
commit
457b56937e
modules/elasticsearch/src/main/java/org/elasticsearch
client/action
search
plugins/client/groovy/src
main/groovy/org/elasticsearch/groovy/client
test/groovy/org/elasticsearch/groovy/test/client
10
modules/elasticsearch/src/main/java/org/elasticsearch/client/action/count/CountRequestBuilder.java
10
modules/elasticsearch/src/main/java/org/elasticsearch/client/action/count/CountRequestBuilder.java
@ -89,6 +89,16 @@ public class CountRequestBuilder extends BaseRequestBuilder<CountRequest, CountR
|
|||||||
return this;
|
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.
|
* Controls the operation threading model.
|
||||||
*/
|
*/
|
||||||
|
8
modules/elasticsearch/src/main/java/org/elasticsearch/client/action/search/SearchRequestBuilder.java
8
modules/elasticsearch/src/main/java/org/elasticsearch/client/action/search/SearchRequestBuilder.java
@ -169,6 +169,14 @@ public class SearchRequestBuilder extends BaseRequestBuilder<SearchRequest, Sear
|
|||||||
return this;
|
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>.
|
* From index to start the search from. Defaults to <tt>0</tt>.
|
||||||
*/
|
*/
|
||||||
|
@ -70,6 +70,8 @@ public class SearchSourceBuilder implements ToXContent {
|
|||||||
|
|
||||||
private XContentQueryBuilder queryBuilder;
|
private XContentQueryBuilder queryBuilder;
|
||||||
|
|
||||||
|
private byte[] queryBinary;
|
||||||
|
|
||||||
private int from = -1;
|
private int from = -1;
|
||||||
|
|
||||||
private int size = -1;
|
private int size = -1;
|
||||||
@ -107,6 +109,14 @@ public class SearchSourceBuilder implements ToXContent {
|
|||||||
return this;
|
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>.
|
* 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);
|
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) {
|
if (explain != null) {
|
||||||
builder.field("explain", explain);
|
builder.field("explain", explain);
|
||||||
}
|
}
|
||||||
|
41
modules/elasticsearch/src/main/java/org/elasticsearch/search/query/QueryBinaryParseElement.java
Normal file
41
modules/elasticsearch/src/main/java/org/elasticsearch/search/query/QueryBinaryParseElement.java
Normal 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);
|
||||||
|
}
|
||||||
|
}
|
@ -55,6 +55,8 @@ public class QueryPhase implements SearchPhase {
|
|||||||
.put("indices_boost", new IndicesBoostParseElement())
|
.put("indices_boost", new IndicesBoostParseElement())
|
||||||
.put("indicesBoost", new IndicesBoostParseElement())
|
.put("indicesBoost", new IndicesBoostParseElement())
|
||||||
.put("query", new QueryParseElement())
|
.put("query", new QueryParseElement())
|
||||||
|
.put("queryBinary", new QueryBinaryParseElement())
|
||||||
|
.put("query_binary", new QueryBinaryParseElement())
|
||||||
.put("sort", new SortParseElement())
|
.put("sort", new SortParseElement())
|
||||||
.putAll(facetsPhase.parseElements());
|
.putAll(facetsPhase.parseElements());
|
||||||
return parseElements.build();
|
return parseElements.build();
|
||||||
|
@ -121,6 +121,12 @@ class GClient {
|
|||||||
SearchRequestBuilder.metaClass.extraSource = {Closure c ->
|
SearchRequestBuilder.metaClass.extraSource = {Closure c ->
|
||||||
delegate.setExtraSource(new GXContentBuilder().buildAsBytes(c, contentType))
|
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 ->
|
MoreLikeThisRequest.metaClass.setSearchSource = {Closure c ->
|
||||||
delegate.searchSource(new GXContentBuilder().buildAsBytes(c, contentType))
|
delegate.searchSource(new GXContentBuilder().buildAsBytes(c, contentType))
|
||||||
|
@ -67,6 +67,20 @@ class BuilderActionsTests {
|
|||||||
assertThat indexR.response.type, equalTo("type1")
|
assertThat indexR.response.type, equalTo("type1")
|
||||||
assertThat indexR.response.id, equalTo("1")
|
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()
|
def delete = node.client.prepareDelete("test", "type1", "1").gexecute()
|
||||||
assertThat delete.response.index, equalTo("test")
|
assertThat delete.response.index, equalTo("test")
|
||||||
assertThat delete.response.type, equalTo("type1")
|
assertThat delete.response.type, equalTo("type1")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user