本文整理汇总了Java中org.elasticsearch.action.search.MultiSearchResponse类的典型用法代码示例。如果您正苦于以下问题:Java MultiSearchResponse类的具体用法?Java MultiSearchResponse怎么用?Java MultiSearchResponse使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
MultiSearchResponse类属于org.elasticsearch.action.search包,在下文中一共展示了MultiSearchResponse类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: testSimpleMultiSearchMoreRequests
import org.elasticsearch.action.search.MultiSearchResponse; //导入依赖的package包/类
public void testSimpleMultiSearchMoreRequests() {
createIndex("test");
int numDocs = randomIntBetween(0, 16);
for (int i = 0; i < numDocs; i++) {
client().prepareIndex("test", "type", Integer.toString(i)).setSource("{}", XContentType.JSON).get();
}
refresh();
int numSearchRequests = randomIntBetween(1, 64);
MultiSearchRequest request = new MultiSearchRequest();
if (randomBoolean()) {
request.maxConcurrentSearchRequests(randomIntBetween(1, numSearchRequests));
}
for (int i = 0; i < numSearchRequests; i++) {
request.add(client().prepareSearch("test"));
}
MultiSearchResponse response = client().multiSearch(request).actionGet();
assertThat(response.getResponses().length, equalTo(numSearchRequests));
for (MultiSearchResponse.Item item : response) {
assertNoFailures(item.getResponse());
assertHitCount(item.getResponse(), numDocs);
}
}
示例2: testSimpleMultiSearch
import org.elasticsearch.action.search.MultiSearchResponse; //导入依赖的package包/类
public void testSimpleMultiSearch() {
createIndex("test");
ensureGreen();
client().prepareIndex("test", "type", "1").setSource("field", "xxx").execute().actionGet();
client().prepareIndex("test", "type", "2").setSource("field", "yyy").execute().actionGet();
refresh();
MultiSearchResponse response = client().prepareMultiSearch()
.add(client().prepareSearch("test").setQuery(QueryBuilders.termQuery("field", "xxx")))
.add(client().prepareSearch("test").setQuery(QueryBuilders.termQuery("field", "yyy")))
.add(client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()))
.execute().actionGet();
for (MultiSearchResponse.Item item : response) {
assertNoFailures(item.getResponse());
}
assertThat(response.getResponses().length, equalTo(3));
assertHitCount(response.getResponses()[0].getResponse(), 1L);
assertHitCount(response.getResponses()[1].getResponse(), 1L);
assertHitCount(response.getResponses()[2].getResponse(), 2L);
assertFirstHit(response.getResponses()[0].getResponse(), hasId("1"));
assertFirstHit(response.getResponses()[1].getResponse(), hasId("2"));
}
示例3: testFailedMultiSearchWithWrongQuery
import org.elasticsearch.action.search.MultiSearchResponse; //导入依赖的package包/类
public void testFailedMultiSearchWithWrongQuery() throws Exception {
prepareData();
logger.info("Start Testing failed multi search with a wrong query");
MultiSearchResponse response = client().prepareMultiSearch()
.add(client().prepareSearch("test").setQuery(new MatchQueryBuilder("foo", "biz")))
.add(client().prepareSearch("test").setQuery(QueryBuilders.termQuery("nid", 2)))
.add(client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()))
.execute().actionGet();
assertThat(response.getResponses().length, equalTo(3));
assertThat(response.getResponses()[0].getFailureMessage(), notNullValue());
assertThat(response.getResponses()[1].getFailureMessage(), nullValue());
assertThat(response.getResponses()[1].getResponse().getHits().getHits().length, equalTo(1));
assertThat(response.getResponses()[2].getFailureMessage(), nullValue());
assertThat(response.getResponses()[2].getResponse().getHits().getHits().length, equalTo(10));
logger.info("Done Testing failed search");
}
示例4: testFailedMultiSearchWithWrongQueryWithFunctionScore
import org.elasticsearch.action.search.MultiSearchResponse; //导入依赖的package包/类
public void testFailedMultiSearchWithWrongQueryWithFunctionScore() throws Exception {
prepareData();
logger.info("Start Testing failed multi search with a wrong query");
MultiSearchResponse response = client().prepareMultiSearch()
// Add custom score query with bogus script
.add(client().prepareSearch("test").setQuery(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("nid", 1), new ScriptScoreFunctionBuilder(new Script(ScriptType.INLINE, "bar", "foo", Collections.emptyMap())))))
.add(client().prepareSearch("test").setQuery(QueryBuilders.termQuery("nid", 2)))
.add(client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()))
.execute().actionGet();
assertThat(response.getResponses().length, equalTo(3));
assertThat(response.getResponses()[0].getFailureMessage(), notNullValue());
assertThat(response.getResponses()[1].getFailureMessage(), nullValue());
assertThat(response.getResponses()[1].getResponse().getHits().getHits().length, equalTo(1));
assertThat(response.getResponses()[2].getFailureMessage(), nullValue());
assertThat(response.getResponses()[2].getResponse().getHits().getHits().length, equalTo(10));
logger.info("Done Testing failed search");
}
示例5: toResponse
import org.elasticsearch.action.search.MultiSearchResponse; //导入依赖的package包/类
private ListStoresActionResponse toResponse(MultiSearchResponse response, List<Tuple<String, Integer>> versions) {
assert versions.size() == response.getResponses().length;
Iterator<Tuple<String, Integer>> vs = versions.iterator();
Iterator<MultiSearchResponse.Item> rs = response.iterator();
List<ListStoresAction.IndexStoreInfo> infos = new ArrayList<>(versions.size());
while (vs.hasNext() && rs.hasNext()) {
MultiSearchResponse.Item it = rs.next();
Tuple<String, Integer> idxAndVersion = vs.next();
Map<String, Integer> counts = Collections.emptyMap();
if (!it.isFailure()) {
Terms aggs = it.getResponse()
.getAggregations()
.get("type");
counts = aggs
.getBuckets()
.stream()
.collect(toMap(MultiBucketsAggregation.Bucket::getKeyAsString,
(b) -> (int) b.getDocCount()));
}
infos.add(new ListStoresAction.IndexStoreInfo(idxAndVersion.v1(), idxAndVersion.v2(), counts));
}
return new ListStoresActionResponse(infos);
}
示例6: executeRequest
import org.elasticsearch.action.search.MultiSearchResponse; //导入依赖的package包/类
/**
* Execute query request with filter and aggregations.
*
* @param queries
* list of queries to execute
* @return ES search response
* @throws ODataApplicationException
* if any error appeared during executing request
*/
public MultiSearchResponse executeRequest(List<SearchQuery> queries)
throws ODataApplicationException {
MultiSearchRequestBuilder multiSearchRequestBuilder = client.prepareMultiSearch();
for (SearchQuery query : queries) {
Pagination pagination = query.getPagination();
SearchRequestBuilder requestBuilder = client.prepareSearch(query.getIndex())
.setTypes(query.getTypes()).setQuery(query.getQueryBuilder());
if (pagination != null) {
List<Sort> orderBy = pagination.getOrderBy();
for (Sort sort : orderBy) {
FieldSortBuilder sortQuery = SortBuilders.fieldSort(sort.getProperty())
.order(SortOrder.valueOf(sort.getDirection().toString()));
requestBuilder.addSort(sortQuery);
}
requestBuilder.setSize(pagination.getTop()).setFrom(pagination.getSkip());
}
Set<String> fields = query.getFields();
if (fields != null && !fields.isEmpty()) {
requestBuilder.setFetchSource(fields.toArray(new String[fields.size()]), null);
}
multiSearchRequestBuilder.add(requestBuilder);
}
return executeRequest(multiSearchRequestBuilder);
}
示例7: testMultiSearch
import org.elasticsearch.action.search.MultiSearchResponse; //导入依赖的package包/类
@Test
public void testMultiSearch() throws Exception {
//first, INDEX two values
Map<String, Object> headers = new HashMap<String, Object>();
node.client().prepareIndex("test", "type", "1").setSource("field", "xxx").execute().actionGet();
node.client().prepareIndex("test", "type", "2").setSource("field", "yyy").execute().actionGet();
//now, verify MULTISEARCH
headers.put(ElasticsearchConstants.PARAM_OPERATION, ElasticsearchConstants.OPERATION_MULTISEARCH);
SearchRequestBuilder srb1 = node.client().prepareSearch("test").setTypes("type").setQuery(QueryBuilders.termQuery("field", "xxx"));
SearchRequestBuilder srb2 = node.client().prepareSearch("test").setTypes("type").setQuery(QueryBuilders.termQuery("field", "yyy"));
SearchRequestBuilder srb3 = node.client().prepareSearch("instagram")
.setTypes("type").setQuery(QueryBuilders.termQuery("test-multisearchkey", "test-multisearchvalue"));
List<SearchRequest> list = new ArrayList<>();
list.add(srb1.request());
list.add(srb2.request());
list.add(srb3.request());
MultiSearchResponse response = template.requestBodyAndHeaders("direct:multisearch", list, headers, MultiSearchResponse.class);
MultiSearchResponse.Item[] responses = response.getResponses();
assertNotNull("response should not be null", response);
assertEquals("response should contains three multiSearchResponse object", 3, response.getResponses().length);
assertFalse("response 1 should be ok", responses[0].isFailure());
assertFalse("response 2 should be ok", responses[1].isFailure());
assertTrue("response 3 should be failed", responses[2].isFailure());
}
示例8: testMultiSearch
import org.elasticsearch.action.search.MultiSearchResponse; //导入依赖的package包/类
@Test
public void testMultiSearch() {
// query string type
SearchRequestBuilder srb1 =
client.prepareSearch(indexShakeSpeare).setQuery(QueryBuilders.queryString("elephant"))
// size: 10 is the default
.setSize(10);
// match query type
SearchRequestBuilder srb2 =
client.prepareSearch(indexShakeSpeare)
.setQuery(QueryBuilders.matchQuery(fieldShakeSpeare, "havoc")).setSize(10);
// correlate the srbs
MultiSearchResponse sr = client.prepareMultiSearch().add(srb1).add(srb2).execute().actionGet();
// get all individual responses from MultiSearchResponse
long totalHits = 0;
for (MultiSearchResponse.Item item : sr.getResponses()) {
SearchResponse response = item.getResponse();
totalHits += response.getHits().getTotalHits();
}
System.out.println("Total matching for the elephant and havoc: " + totalHits);
}
示例9: testMultiSearch
import org.elasticsearch.action.search.MultiSearchResponse; //导入依赖的package包/类
private void testMultiSearch() {
SearchRequestBuilder srb1 = client.prepareSearch() //
.setQuery(QueryBuilders.queryString("elasticsearch")) //
.setSize(1);
SearchRequestBuilder srb2 = client.prepareSearch() //
.setQuery(QueryBuilders.matchQuery("name", "kimchy")) //
.setSize(1);
MultiSearchResponse sr = client.prepareMultiSearch() //
.add(srb1) //
.add(srb2) //
.execute() //
.actionGet();
// You will get all individual responses from
// MultiSearchResponse#responses()
long nbHits = 0;
for (MultiSearchResponse.Item item : sr.getResponses()) {
SearchResponse response = item.getResponse();
nbHits += response.getHits().totalHits();
}
}
示例10: handleRequest
import org.elasticsearch.action.search.MultiSearchResponse; //导入依赖的package包/类
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) throws Exception {
MultiSearchRequest multiSearchRequest = new MultiSearchRequest();
String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
String[] types = Strings.splitStringByCommaToArray(request.param("type"));
String path = request.path();
boolean isTemplateRequest = isTemplateRequest(path);
IndicesOptions indicesOptions = IndicesOptions.fromRequest(request, multiSearchRequest.indicesOptions());
multiSearchRequest.add(RestActions.getRestContent(request), isTemplateRequest, indices, types, request.param("search_type"), request.param("routing"), indicesOptions, allowExplicitIndex);
client.multiSearch(multiSearchRequest, new RestToXContentListener<MultiSearchResponse>(channel));
}
示例11: main
import org.elasticsearch.action.search.MultiSearchResponse; //导入依赖的package包/类
public static void main(String[] args) throws UnknownHostException {
// InetSocketAddress inetSocketAddress = new InetSocketAddress("127.0.0.1", 9300);
InetAddress byName = InetAddress.getByName("127.0.0.1");
InetSocketTransportAddress address = new InetSocketTransportAddress(byName, 9300);
TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
.addTransportAddress(address);
System.out.println(client);
// client.prepareGet("", "", "")
MultiSearchResponse response = client.prepareMultiSearch()
.add(client.prepareSearch("zsxtest")).execute().actionGet();
System.out.println(response);
}
示例12: handleRequest
import org.elasticsearch.action.search.MultiSearchResponse; //导入依赖的package包/类
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) throws Exception {
MultiSearchRequest multiSearchRequest = new MultiSearchRequest();
String[] indices = Strings.splitStringByCommaToArray(request.param("index"));
String[] types = Strings.splitStringByCommaToArray(request.param("type"));
String path = request.path();
boolean isTemplateRequest = isTemplateRequest(path);
IndicesOptions indicesOptions = IndicesOptions.fromRequest(request, multiSearchRequest.indicesOptions());
multiSearchRequest.add(RestActions.getRestContent(request), isTemplateRequest, indices, types, request.param("search_type"), request.param("routing"), indicesOptions, allowExplicitIndex);
client.execute(CoordinateMultiSearchAction.INSTANCE, multiSearchRequest, new RestToXContentListener<MultiSearchResponse>(channel));
}
示例13: testMultiSearch
import org.elasticsearch.action.search.MultiSearchResponse; //导入依赖的package包/类
@Test
public void testMultiSearch() {
SearchRequestBuilder srb1 = client.prepareSearch().setQuery(QueryBuilders.queryStringQuery("elasticsearch")).setSize(1);
SearchRequestBuilder srb2 = client.prepareSearch().setQuery(QueryBuilders.matchQuery("name", "kimchy")).setSize(1);
MultiSearchResponse sr = client.prepareMultiSearch().add(srb1).add(srb2).execute().actionGet();
// You will get all individual responses from
// MultiSearchResponse#getResponses()
long nbHits = 0;
for (MultiSearchResponse.Item item : sr.getResponses()) {
SearchResponse response = item.getResponse();
nbHits += response.getHits().getTotalHits();
}
}
示例14: multiSearch
import org.elasticsearch.action.search.MultiSearchResponse; //导入依赖的package包/类
/**
*
* @param queryString
*/
private static void multiSearch(String queryString){
Client client = createTransportClient();
SearchRequestBuilder srb1 = client.prepareSearch()
.setQuery(QueryBuilders.queryStringQuery(queryString));
SearchRequestBuilder srb2 = client.prepareSearch()
.setQuery(QueryBuilders.matchQuery("desc", queryString));
MultiSearchResponse sr = client.prepareMultiSearch()
.add(srb1)
.add(srb2)
.execute().actionGet();
long nbHits = 0;
for (MultiSearchResponse.Item item : sr.getResponses()) {
SearchResponse response = item.getResponse();
nbHits += response.getHits().getTotalHits();
System.out.println("本次查询共匹配到:"+nbHits+"记录");
SearchHits searchHits = response.getHits();
System.out.println("-----------------搜索关键字为:["+queryString+"]---------------------");
System.out.println("共匹配到:"+searchHits.getTotalHits()+"条记录!");
SearchHit[] hits = searchHits.getHits();
for (SearchHit searchHit : hits) {
Map<String, Object> sourceAsMap = searchHit.sourceAsMap();
Set<String> keySet = sourceAsMap.keySet();
for (String string : keySet) {
System.out.println(string+":"+sourceAsMap.get(string));
}
System.out.println();
}
}
}
示例15: createResponse
import org.elasticsearch.action.search.MultiSearchResponse; //导入依赖的package包/类
/**
* Create response based on elastic multi search response (does not add UUID for now)
*
* @param response
* @param responseUuid
* @return
*/
public StreamingOutput createResponse(final MultiSearchResponse response, final String responseUuid) {
return new StreamingOutput() {
@Override
public void write(OutputStream output) throws IOException, WebApplicationException {
XContentBuilder builder = XContentFactory.jsonBuilder(output);
builder.startObject();
if (responseUuid != null)
builder.field("uuid", responseUuid);
response.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
builder.close();
}
};
}