本文整理汇总了Java中org.elasticsearch.action.search.MultiSearchResponse.Item方法的典型用法代码示例。如果您正苦于以下问题:Java MultiSearchResponse.Item方法的具体用法?Java MultiSearchResponse.Item怎么用?Java MultiSearchResponse.Item使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.elasticsearch.action.search.MultiSearchResponse
的用法示例。
在下文中一共展示了MultiSearchResponse.Item方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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: 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());
}
示例4: 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);
}
示例5: 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();
}
}
示例6: 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();
}
}
示例7: 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();
}
}
}
示例8: processMultiSearchResponse
import org.elasticsearch.action.search.MultiSearchResponse; //导入方法依赖的package包/类
@Override
public SearchResultEventWithSearchRequestKey<FrequentlyRelatedSearchResult[]>[] processMultiSearchResponse(RelatedItemSearch[] searches,MultiSearchResponse searchResponse) {
int i = 0;
SearchResultEventWithSearchRequestKey[] results = new SearchResultEventWithSearchRequestKey[searches.length];
for(MultiSearchResponse.Item item : searchResponse.getResponses()) {
SearchRequestLookupKey key = searches[i].getLookupKey();
results[i] = frequentlyRelatedWithResultsConverter(key,item.getResponse(),item.getFailureMessage(),item.isFailure(),searches[i++].getStartOfRequestNanos());
}
return results;
}
示例9: testPercolatorQueryViaMultiSearch
import org.elasticsearch.action.search.MultiSearchResponse; //导入方法依赖的package包/类
public void testPercolatorQueryViaMultiSearch() throws Exception {
createIndex("test", client().admin().indices().prepareCreate("test")
.addMapping("type", "field1", "type=text")
.addMapping("queries", "query", "type=percolator")
);
client().prepareIndex("test", "queries", "1")
.setSource(jsonBuilder().startObject().field("query", matchQuery("field1", "b")).field("a", "b").endObject())
.execute().actionGet();
client().prepareIndex("test", "queries", "2")
.setSource(jsonBuilder().startObject().field("query", matchQuery("field1", "c")).endObject())
.execute().actionGet();
client().prepareIndex("test", "queries", "3")
.setSource(jsonBuilder().startObject().field("query", boolQuery()
.must(matchQuery("field1", "b"))
.must(matchQuery("field1", "c"))
).endObject())
.execute().actionGet();
client().prepareIndex("test", "queries", "4")
.setSource(jsonBuilder().startObject().field("query", matchAllQuery()).endObject())
.execute().actionGet();
client().prepareIndex("test", "type", "1")
.setSource(jsonBuilder().startObject().field("field1", "c").endObject())
.execute().actionGet();
client().admin().indices().prepareRefresh().get();
MultiSearchResponse response = client().prepareMultiSearch()
.add(client().prepareSearch("test")
.setQuery(new PercolateQueryBuilder("query", "type",
jsonBuilder().startObject().field("field1", "b").endObject().bytes(), XContentType.JSON)))
.add(client().prepareSearch("test")
.setQuery(new PercolateQueryBuilder("query", "type",
yamlBuilder().startObject().field("field1", "c").endObject().bytes(), XContentType.JSON)))
.add(client().prepareSearch("test")
.setQuery(new PercolateQueryBuilder("query", "type",
smileBuilder().startObject().field("field1", "b c").endObject().bytes(), XContentType.JSON)))
.add(client().prepareSearch("test")
.setQuery(new PercolateQueryBuilder("query", "type",
jsonBuilder().startObject().field("field1", "d").endObject().bytes(), XContentType.JSON)))
.add(client().prepareSearch("test")
.setQuery(new PercolateQueryBuilder("query", "type", "test", "type", "1", null, null, null)))
.add(client().prepareSearch("test") // non existing doc, so error element
.setQuery(new PercolateQueryBuilder("query", "type", "test", "type", "2", null, null, null)))
.get();
MultiSearchResponse.Item item = response.getResponses()[0];
assertHitCount(item.getResponse(), 2L);
assertSearchHits(item.getResponse(), "1", "4");
assertThat(item.getFailureMessage(), nullValue());
item = response.getResponses()[1];
assertHitCount(item.getResponse(), 2L);
assertSearchHits(item.getResponse(), "2", "4");
assertThat(item.getFailureMessage(), nullValue());
item = response.getResponses()[2];
assertHitCount(item.getResponse(), 4L);
assertSearchHits(item.getResponse(), "1", "2", "3", "4");
assertThat(item.getFailureMessage(), nullValue());
item = response.getResponses()[3];
assertHitCount(item.getResponse(), 1L);
assertSearchHits(item.getResponse(), "4");
assertThat(item.getFailureMessage(), nullValue());
item = response.getResponses()[4];
assertHitCount(item.getResponse(), 2L);
assertSearchHits(item.getResponse(), "2", "4");
assertThat(item.getFailureMessage(), nullValue());
item = response.getResponses()[5];
assertThat(item.getResponse(), nullValue());
assertThat(item.getFailureMessage(), notNullValue());
assertThat(item.getFailureMessage(), equalTo("all shards failed"));
assertThat(ExceptionsHelper.unwrapCause(item.getFailure().getCause()).getMessage(),
containsString("[test/type/2] couldn't be found"));
}
示例10: testProfileMatchesRegular
import org.elasticsearch.action.search.MultiSearchResponse; //导入方法依赖的package包/类
/**
* This test generates 1-10 random queries and executes a profiled and non-profiled
* search for each query. It then does some basic sanity checking of score and hits
* to make sure the profiling doesn't interfere with the hits being returned
*/
public void testProfileMatchesRegular() throws Exception {
createIndex("test");
ensureGreen();
int numDocs = randomIntBetween(100, 150);
IndexRequestBuilder[] docs = new IndexRequestBuilder[numDocs];
for (int i = 0; i < numDocs; i++) {
docs[i] = client().prepareIndex("test", "type1", String.valueOf(i)).setSource(
"field1", English.intToEnglish(i),
"field2", i
);
}
List<String> stringFields = Arrays.asList("field1");
List<String> numericFields = Arrays.asList("field2");
indexRandom(true, docs);
refresh();
int iters = between(1, 10);
for (int i = 0; i < iters; i++) {
QueryBuilder q = randomQueryBuilder(stringFields, numericFields, numDocs, 3);
logger.info("Query: {}", q);
SearchRequestBuilder vanilla = client().prepareSearch("test")
.setQuery(q)
.setProfile(false)
.addSort("_uid", SortOrder.ASC)
.setPreference("_primary")
.setSearchType(SearchType.QUERY_THEN_FETCH);
SearchRequestBuilder profile = client().prepareSearch("test")
.setQuery(q)
.setProfile(true)
.addSort("_uid", SortOrder.ASC)
.setPreference("_primary")
.setSearchType(SearchType.QUERY_THEN_FETCH);
MultiSearchResponse.Item[] responses = client().prepareMultiSearch()
.add(vanilla)
.add(profile)
.execute().actionGet().getResponses();
SearchResponse vanillaResponse = responses[0].getResponse();
SearchResponse profileResponse = responses[1].getResponse();
float vanillaMaxScore = vanillaResponse.getHits().getMaxScore();
float profileMaxScore = profileResponse.getHits().getMaxScore();
if (Float.isNaN(vanillaMaxScore)) {
assertTrue("Vanilla maxScore is NaN but Profile is not [" + profileMaxScore + "]",
Float.isNaN(profileMaxScore));
} else {
assertTrue("Profile maxScore of [" + profileMaxScore + "] is not close to Vanilla maxScore [" + vanillaMaxScore + "]",
nearlyEqual(vanillaMaxScore, profileMaxScore, 0.001));
}
assertThat(
"Profile totalHits of [" + profileResponse.getHits().getTotalHits() + "] is not close to Vanilla totalHits ["
+ vanillaResponse.getHits().getTotalHits() + "]",
vanillaResponse.getHits().getTotalHits(), equalTo(profileResponse.getHits().getTotalHits()));
SearchHit[] vanillaHits = vanillaResponse.getHits().getHits();
SearchHit[] profileHits = profileResponse.getHits().getHits();
for (int j = 0; j < vanillaHits.length; j++) {
assertThat("Profile hit #" + j + " has a different ID from Vanilla",
vanillaHits[j].getId(), equalTo(profileHits[j].getId()));
}
}
}
示例11: CoordinateMultiSearchResponse
import org.elasticsearch.action.search.MultiSearchResponse; //导入方法依赖的package包/类
CoordinateMultiSearchResponse() {
// hack: empty constructor is private, use this one instead
// we use an empty array to avoid NPE during serialization
super(new MultiSearchResponse.Item[0]);
}
示例12: iterator
import org.elasticsearch.action.search.MultiSearchResponse; //导入方法依赖的package包/类
@Override
public Iterator<MultiSearchResponse.Item> iterator() {
return Iterators.forArray((MultiSearchResponse.Item[]) items);
}
示例13: allReader
import org.elasticsearch.action.search.MultiSearchResponse; //导入方法依赖的package包/类
public void allReader(ElastisearchCrawlerMultipleHits crawler) throws Exception {
try {
MultiSearchResponse.Item[] items = Elasticsearch.withRetry(new ElasticsearchWithRetry<Item[]>() {
public Item[] call(Client client) throws NoNodeAvailableException {
MultiSearchRequestBuilder multisearchrequestbuilder = new MultiSearchRequestBuilder(client);
for (int pos = 0; pos < queries.size(); pos++) {
multisearchrequestbuilder.add(queries.get(pos).getRequest());
}
return multisearchrequestbuilder.execute().actionGet().getResponses();
}
});
SearchHit[] hits;
SearchResponse response;
for (int pos_response = 0; pos_response < items.length; pos_response++) {
if (items[pos_response].isFailure()) {
Loggers.ElasticSearch.warn("Search failed", new Exception(items[pos_response].getFailureMessage()));
continue;
}
response = items[pos_response].getResponse();
if (response == null) {
continue;
}
if (response.getHits() == null) {
continue;
}
hits = response.getHits().hits();
if (hits.length == 0) {
continue;
}
if (crawler.onMultipleResponse(response, Arrays.asList(hits)) == false) {
return;
}
}
} catch (IndexMissingException ime) {
/**
* No items == no callbacks
*/
Loggers.ElasticSearch.debug("Index missing", ime);
return;
} catch (SearchPhaseExecutionException e) {
Loggers.ElasticSearch.debug("No datas", e);
}
}