use of org.elasticsearch.action.search.SearchResponse in project crate by crate.
the class ArrayMapperTest method testEmptyArray.
@Test
public void testEmptyArray() throws Exception {
String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("array_field").field("type", ArrayMapper.CONTENT_TYPE).startObject(ArrayMapper.INNER).field("type", "double").field("index", "not_analyzed").endObject().endObject().endObject().endObject().endObject().string();
DocumentMapper mapper = mapper(INDEX, TYPE, mapping);
// parse source with empty array
ParsedDocument doc = mapper.parse(INDEX, TYPE, "abc", XContentFactory.jsonBuilder().startObject().array("array_field").endObject().bytes());
assertThat(doc.dynamicMappingsUpdate() == null, is(true));
assertThat(doc.docs().size(), is(1));
// no lucene field generated
assertThat(doc.docs().get(0).get("array_field"), is(nullValue()));
// insert
IndexResponse response = client().prepareIndex(INDEX, "type", "123").setSource("{array_field:[]}").execute().actionGet();
assertThat(response.getVersion(), is(1L));
client().admin().indices().prepareRefresh(INDEX).execute().actionGet();
SearchResponse searchResponse = client().prepareSearch(INDEX).setTypes("type").setFetchSource(true).addField("array_field").setQuery("{\"term\": {\"_id\": \"123\"}}").execute().actionGet();
assertThat(searchResponse.getHits().getTotalHits(), is(1L));
assertThat(Joiner.on(',').withKeyValueSeparator(":").join(searchResponse.getHits().getAt(0).getSource()), is("array_field:[]"));
assertThat(searchResponse.getHits().getAt(0).fields().containsKey("array_field"), is(false));
}
use of org.elasticsearch.action.search.SearchResponse in project rssriver by dadoonet.
the class RssRiverAllParametersTest method test_mcapp_rivers.
/**
* http://www.malwaredomains.com/wordpress/?feed=rss
* http://www.darkreading.com/rss/all.xml
*/
@Test
public void test_mcapp_rivers() throws IOException, InterruptedException {
startRiver("mcapp", getLastUpdatedId("malwaredomains"), createRiver(true, "malwaredomains", "darkreading"));
// We wait for some documents
existSomeDocs("mcapp", "malwaredomains");
existSomeDocs("mcapp", "darkreading");
SearchResponse response = client().prepareSearch("mcapp").setQuery(QueryBuilders.matchQuery("description", "domains")).addField(RssToJson.Rss.RAW + ".html").execute().actionGet();
assertThat(response.getHits().getTotalHits(), greaterThan(0L));
assertThat(response.getHits().getAt(0).field(RssToJson.Rss.RAW + ".html"), notNullValue());
assertThat(response.getHits().getAt(0).field(RssToJson.Rss.RAW + ".html").getValues(), notNullValue());
}
use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class CombiIT method testSubAggregationForTopAggregationOnUnmappedField.
/**
* Some top aggs (eg. date_/histogram) that are executed on unmapped fields, will generate an estimate count of buckets - zero.
* when the sub aggregator is then created, it will take this estimation into account. This used to cause
* and an ArrayIndexOutOfBoundsException...
*/
public void testSubAggregationForTopAggregationOnUnmappedField() throws Exception {
prepareCreate("idx").addMapping("type", jsonBuilder().startObject().startObject("type").startObject("properties").startObject("name").field("type", "keyword").endObject().startObject("value").field("type", "integer").endObject().endObject().endObject().endObject()).execute().actionGet();
ensureSearchable("idx");
SubAggCollectionMode aggCollectionMode = randomFrom(SubAggCollectionMode.values());
SearchResponse searchResponse = client().prepareSearch("idx").addAggregation(histogram("values").field("value1").interval(1).subAggregation(terms("names").field("name").collectMode(aggCollectionMode))).execute().actionGet();
assertThat(searchResponse.getHits().getTotalHits(), Matchers.equalTo(0L));
Histogram values = searchResponse.getAggregations().get("values");
assertThat(values, notNullValue());
assertThat(values.getBuckets().isEmpty(), is(true));
}
use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class CombiIT method testMultipleAggsOnSameField_WithDifferentRequiredValueSourceType.
/**
* Making sure that if there are multiple aggregations, working on the same field, yet require different
* value source type, they can all still work. It used to fail as we used to cache the ValueSource by the
* field name. If the cached value source was of type "bytes" and another aggregation on the field required to see
* it as "numeric", it didn't work. Now we cache the Value Sources by a custom key (field name + ValueSource type)
* so there's no conflict there.
*/
public void testMultipleAggsOnSameField_WithDifferentRequiredValueSourceType() throws Exception {
createIndex("idx");
IndexRequestBuilder[] builders = new IndexRequestBuilder[randomInt(30)];
IntIntMap values = new IntIntHashMap();
long missingValues = 0;
for (int i = 0; i < builders.length; i++) {
String name = "name_" + randomIntBetween(1, 10);
if (rarely()) {
missingValues++;
builders[i] = client().prepareIndex("idx", "type").setSource(jsonBuilder().startObject().field("name", name).endObject());
} else {
int value = randomIntBetween(1, 10);
values.put(value, values.getOrDefault(value, 0) + 1);
builders[i] = client().prepareIndex("idx", "type").setSource(jsonBuilder().startObject().field("name", name).field("value", value).endObject());
}
}
indexRandom(true, builders);
ensureSearchable();
SubAggCollectionMode aggCollectionMode = randomFrom(SubAggCollectionMode.values());
SearchResponse response = client().prepareSearch("idx").addAggregation(missing("missing_values").field("value")).addAggregation(terms("values").field("value").collectMode(aggCollectionMode)).execute().actionGet();
assertSearchResponse(response);
Aggregations aggs = response.getAggregations();
Missing missing = aggs.get("missing_values");
assertNotNull(missing);
assertThat(missing.getDocCount(), equalTo(missingValues));
Terms terms = aggs.get("values");
assertNotNull(terms);
Collection<Terms.Bucket> buckets = terms.getBuckets();
assertThat(buckets.size(), equalTo(values.size()));
for (Terms.Bucket bucket : buckets) {
values.remove(((Number) bucket.getKey()).intValue());
}
assertTrue(values.isEmpty());
}
use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class SearchServiceTests method testClearIndexDelete.
public void testClearIndexDelete() throws ExecutionException, InterruptedException {
createIndex("index");
client().prepareIndex("index", "type", "1").setSource("field", "value").setRefreshPolicy(IMMEDIATE).get();
SearchResponse searchResponse = client().prepareSearch("index").setSize(1).setScroll("1m").get();
assertThat(searchResponse.getScrollId(), is(notNullValue()));
SearchService service = getInstanceFromNode(SearchService.class);
assertEquals(1, service.getActiveContexts());
assertAcked(client().admin().indices().prepareDelete("index"));
assertEquals(0, service.getActiveContexts());
}
Aggregations