use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class SearchQueryIT method testSimpleSpan.
// see #2994
public void testSimpleSpan() throws IOException, ExecutionException, InterruptedException {
createIndex("test");
indexRandom(true, client().prepareIndex("test", "test", "1").setSource("description", "foo other anything bar"), client().prepareIndex("test", "test", "2").setSource("description", "foo other anything"), client().prepareIndex("test", "test", "3").setSource("description", "foo other"), client().prepareIndex("test", "test", "4").setSource("description", "foo"));
SearchResponse searchResponse = client().prepareSearch("test").setQuery(spanOrQuery(spanTermQuery("description", "bar"))).get();
assertHitCount(searchResponse, 1L);
searchResponse = client().prepareSearch("test").setQuery(spanNearQuery(spanTermQuery("description", "foo"), 3).addClause(spanTermQuery("description", "other"))).get();
assertHitCount(searchResponse, 3L);
}
use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class FieldSortIT method testSortOnRareField.
public void testSortOnRareField() throws IOException {
assertAcked(prepareCreate("test").addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("string_values").field("type", "keyword").endObject().endObject().endObject().endObject()));
ensureGreen();
client().prepareIndex("test", "type1", Integer.toString(1)).setSource(jsonBuilder().startObject().array("string_values", "01", "05", "10", "08").endObject()).execute().actionGet();
refresh();
SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(3).addSort("string_values", SortOrder.DESC).execute().actionGet();
assertThat(searchResponse.getHits().getHits().length, equalTo(1));
assertThat(searchResponse.getHits().getAt(0).getId(), equalTo(Integer.toString(1)));
assertThat(searchResponse.getHits().getAt(0).getSortValues()[0], equalTo("10"));
client().prepareIndex("test", "type1", Integer.toString(2)).setSource(jsonBuilder().startObject().array("string_values", "11", "15", "20", "07").endObject()).execute().actionGet();
for (int i = 0; i < 15; i++) {
client().prepareIndex("test", "type1", Integer.toString(300 + i)).setSource(jsonBuilder().startObject().array("some_other_field", "foobar").endObject()).execute().actionGet();
}
refresh();
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(2).addSort("string_values", SortOrder.DESC).execute().actionGet();
assertThat(searchResponse.getHits().getHits().length, equalTo(2));
assertThat(searchResponse.getHits().getAt(0).getId(), equalTo(Integer.toString(2)));
assertThat(searchResponse.getHits().getAt(0).getSortValues()[0], equalTo("20"));
assertThat(searchResponse.getHits().getAt(1).getId(), equalTo(Integer.toString(1)));
assertThat(searchResponse.getHits().getAt(1).getSortValues()[0], equalTo("10"));
client().prepareIndex("test", "type1", Integer.toString(3)).setSource(jsonBuilder().startObject().array("string_values", "02", "01", "03", "!4").endObject()).execute().actionGet();
for (int i = 0; i < 15; i++) {
client().prepareIndex("test", "type1", Integer.toString(300 + i)).setSource(jsonBuilder().startObject().array("some_other_field", "foobar").endObject()).execute().actionGet();
}
refresh();
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(3).addSort("string_values", SortOrder.DESC).execute().actionGet();
assertThat(searchResponse.getHits().getHits().length, equalTo(3));
assertThat(searchResponse.getHits().getAt(0).getId(), equalTo(Integer.toString(2)));
assertThat(searchResponse.getHits().getAt(0).getSortValues()[0], equalTo("20"));
assertThat(searchResponse.getHits().getAt(1).getId(), equalTo(Integer.toString(1)));
assertThat(searchResponse.getHits().getAt(1).getSortValues()[0], equalTo("10"));
assertThat(searchResponse.getHits().getAt(2).getId(), equalTo(Integer.toString(3)));
assertThat(searchResponse.getHits().getAt(2).getSortValues()[0], equalTo("03"));
for (int i = 0; i < 15; i++) {
client().prepareIndex("test", "type1", Integer.toString(300 + i)).setSource(jsonBuilder().startObject().array("some_other_field", "foobar").endObject()).execute().actionGet();
refresh();
}
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(3).addSort("string_values", SortOrder.DESC).execute().actionGet();
assertThat(searchResponse.getHits().getHits().length, equalTo(3));
assertThat(searchResponse.getHits().getAt(0).getId(), equalTo(Integer.toString(2)));
assertThat(searchResponse.getHits().getAt(0).getSortValues()[0], equalTo("20"));
assertThat(searchResponse.getHits().getAt(1).getId(), equalTo(Integer.toString(1)));
assertThat(searchResponse.getHits().getAt(1).getSortValues()[0], equalTo("10"));
assertThat(searchResponse.getHits().getAt(2).getId(), equalTo(Integer.toString(3)));
assertThat(searchResponse.getHits().getAt(2).getSortValues()[0], equalTo("03"));
}
use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class FieldSortIT method testSortDuelBetweenSingleShardAndMultiShardIndex.
public void testSortDuelBetweenSingleShardAndMultiShardIndex() throws Exception {
String sortField = "sortField";
assertAcked(prepareCreate("test1").setSettings(IndexMetaData.SETTING_NUMBER_OF_SHARDS, between(2, maximumNumberOfShards())).addMapping("type", sortField, "type=long").get());
assertAcked(prepareCreate("test2").setSettings(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).addMapping("type", sortField, "type=long").get());
for (String index : new String[] { "test1", "test2" }) {
List<IndexRequestBuilder> docs = new ArrayList<>();
for (int i = 0; i < 256; i++) {
docs.add(client().prepareIndex(index, "type", Integer.toString(i)).setSource(sortField, i));
}
indexRandom(true, docs);
}
ensureSearchable("test1", "test2");
SortOrder order = randomBoolean() ? SortOrder.ASC : SortOrder.DESC;
int from = between(0, 256);
int size = between(0, 256);
SearchResponse multiShardResponse = client().prepareSearch("test1").setFrom(from).setSize(size).addSort(sortField, order).get();
assertNoFailures(multiShardResponse);
SearchResponse singleShardResponse = client().prepareSearch("test2").setFrom(from).setSize(size).addSort(sortField, order).get();
assertNoFailures(singleShardResponse);
assertThat(multiShardResponse.getHits().getTotalHits(), equalTo(singleShardResponse.getHits().getTotalHits()));
assertThat(multiShardResponse.getHits().getHits().length, equalTo(singleShardResponse.getHits().getHits().length));
for (int i = 0; i < multiShardResponse.getHits().getHits().length; i++) {
assertThat(multiShardResponse.getHits().getAt(i).getSortValues()[0], equalTo(singleShardResponse.getHits().getAt(i).getSortValues()[0]));
assertThat(multiShardResponse.getHits().getAt(i).getId(), equalTo(singleShardResponse.getHits().getAt(i).getId()));
}
}
use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class GeoDistanceIT method testGeoDistanceFilter.
/**
* Issue 3073
*/
public void testGeoDistanceFilter() throws IOException {
Version version = VersionUtils.randomVersionBetween(random(), Version.V_2_0_0, Version.CURRENT);
Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build();
double lat = 40.720611;
double lon = -73.998776;
XContentBuilder mapping = JsonXContent.contentBuilder().startObject().startObject("location").startObject("properties").startObject("pin").field("type", "geo_point");
mapping.endObject().endObject().endObject().endObject();
XContentBuilder source = JsonXContent.contentBuilder().startObject().field("pin", GeoHashUtils.stringEncode(lon, lat)).endObject();
assertAcked(prepareCreate("locations").setSettings(settings).addMapping("location", mapping));
client().prepareIndex("locations", "location", "1").setCreate(true).setSource(source).execute().actionGet();
refresh();
client().prepareGet("locations", "location", "1").execute().actionGet();
SearchResponse result = client().prepareSearch("locations").setQuery(QueryBuilders.matchAllQuery()).setPostFilter(QueryBuilders.geoDistanceQuery("pin").geoDistance(GeoDistance.ARC).point(lat, lon).distance("1m")).execute().actionGet();
assertHitCount(result, 1);
}
use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class GeoDistanceIT method testDistanceSortingNestedFields.
public void testDistanceSortingNestedFields() throws Exception {
Version version = VersionUtils.randomVersionBetween(random(), Version.V_2_0_0, Version.CURRENT);
Settings settings = Settings.builder().put(IndexMetaData.SETTING_VERSION_CREATED, version).build();
XContentBuilder xContentBuilder = XContentFactory.jsonBuilder().startObject().startObject("company").startObject("properties").startObject("name").field("type", "text").endObject().startObject("branches").field("type", "nested").startObject("properties").startObject("name").field("type", "text").endObject().startObject("location").field("type", "geo_point");
xContentBuilder.endObject().endObject().endObject().endObject().endObject().endObject();
assertAcked(prepareCreate("companies").setSettings(settings).addMapping("company", xContentBuilder));
ensureGreen();
indexRandom(true, client().prepareIndex("companies", "company", "1").setSource(jsonBuilder().startObject().field("name", "company 1").startArray("branches").startObject().field("name", "New York").startObject("location").field("lat", 40.7143528).field("lon", -74.0059731).endObject().endObject().endArray().endObject()), client().prepareIndex("companies", "company", "2").setSource(jsonBuilder().startObject().field("name", "company 2").startArray("branches").startObject().field("name", "Times Square").startObject("location").field("lat", 40.759011).field("lon", -73.9844722).endObject().endObject().startObject().field("name", "Tribeca").startObject("location").field("lat", 40.718266).field("lon", -74.007819).endObject().endObject().endArray().endObject()), client().prepareIndex("companies", "company", "3").setSource(jsonBuilder().startObject().field("name", "company 3").startArray("branches").startObject().field("name", "Wall Street").startObject("location").field("lat", 40.7051157).field("lon", -74.0088305).endObject().endObject().startObject().field("name", "Soho").startObject("location").field("lat", 40.7247222).field("lon", -74).endObject().endObject().endArray().endObject()), client().prepareIndex("companies", "company", "4").setSource(jsonBuilder().startObject().field("name", "company 4").startArray("branches").startObject().field("name", "Greenwich Village").startObject("location").field("lat", 40.731033).field("lon", -73.9962255).endObject().endObject().startObject().field("name", "Brooklyn").startObject("location").field("lat", 40.65).field("lon", -73.95).endObject().endObject().endArray().endObject()));
// Order: Asc
SearchResponse searchResponse = client().prepareSearch("companies").setQuery(matchAllQuery()).addSort(SortBuilders.geoDistanceSort("branches.location", 40.7143528, -74.0059731).order(SortOrder.ASC).setNestedPath("branches")).execute().actionGet();
assertHitCount(searchResponse, 4);
assertOrderedSearchHits(searchResponse, "1", "2", "3", "4");
assertThat(((Number) searchResponse.getHits().getAt(0).getSortValues()[0]).doubleValue(), closeTo(0d, 10d));
assertThat(((Number) searchResponse.getHits().getAt(1).getSortValues()[0]).doubleValue(), closeTo(462.1d, 10d));
assertThat(((Number) searchResponse.getHits().getAt(2).getSortValues()[0]).doubleValue(), closeTo(1055.0d, 10d));
assertThat(((Number) searchResponse.getHits().getAt(3).getSortValues()[0]).doubleValue(), closeTo(2029.0d, 10d));
// Order: Asc, Mode: max
searchResponse = client().prepareSearch("companies").setQuery(matchAllQuery()).addSort(SortBuilders.geoDistanceSort("branches.location", 40.7143528, -74.0059731).order(SortOrder.ASC).sortMode(SortMode.MAX).setNestedPath("branches")).execute().actionGet();
assertHitCount(searchResponse, 4);
assertOrderedSearchHits(searchResponse, "1", "3", "2", "4");
assertThat(((Number) searchResponse.getHits().getAt(0).getSortValues()[0]).doubleValue(), closeTo(0d, 10d));
assertThat(((Number) searchResponse.getHits().getAt(1).getSortValues()[0]).doubleValue(), closeTo(1258.0d, 10d));
assertThat(((Number) searchResponse.getHits().getAt(2).getSortValues()[0]).doubleValue(), closeTo(5286.0d, 10d));
assertThat(((Number) searchResponse.getHits().getAt(3).getSortValues()[0]).doubleValue(), closeTo(8572.0d, 10d));
// Order: Desc
searchResponse = client().prepareSearch("companies").setQuery(matchAllQuery()).addSort(SortBuilders.geoDistanceSort("branches.location", 40.7143528, -74.0059731).order(SortOrder.DESC).setNestedPath("branches")).execute().actionGet();
assertHitCount(searchResponse, 4);
assertOrderedSearchHits(searchResponse, "4", "2", "3", "1");
assertThat(((Number) searchResponse.getHits().getAt(0).getSortValues()[0]).doubleValue(), closeTo(8572.0d, 10d));
assertThat(((Number) searchResponse.getHits().getAt(1).getSortValues()[0]).doubleValue(), closeTo(5286.0d, 10d));
assertThat(((Number) searchResponse.getHits().getAt(2).getSortValues()[0]).doubleValue(), closeTo(1258.0d, 10d));
assertThat(((Number) searchResponse.getHits().getAt(3).getSortValues()[0]).doubleValue(), closeTo(0d, 10d));
// Order: Desc, Mode: min
searchResponse = client().prepareSearch("companies").setQuery(matchAllQuery()).addSort(SortBuilders.geoDistanceSort("branches.location", 40.7143528, -74.0059731).order(SortOrder.DESC).sortMode(SortMode.MIN).setNestedPath("branches")).execute().actionGet();
assertHitCount(searchResponse, 4);
assertOrderedSearchHits(searchResponse, "4", "3", "2", "1");
assertThat(((Number) searchResponse.getHits().getAt(0).getSortValues()[0]).doubleValue(), closeTo(2029.0d, 10d));
assertThat(((Number) searchResponse.getHits().getAt(1).getSortValues()[0]).doubleValue(), closeTo(1055.0d, 10d));
assertThat(((Number) searchResponse.getHits().getAt(2).getSortValues()[0]).doubleValue(), closeTo(462.1d, 10d));
assertThat(((Number) searchResponse.getHits().getAt(3).getSortValues()[0]).doubleValue(), closeTo(0d, 10d));
searchResponse = client().prepareSearch("companies").setQuery(matchAllQuery()).addSort(SortBuilders.geoDistanceSort("branches.location", 40.7143528, -74.0059731).sortMode(SortMode.AVG).order(SortOrder.ASC).setNestedPath("branches")).execute().actionGet();
assertHitCount(searchResponse, 4);
assertOrderedSearchHits(searchResponse, "1", "3", "2", "4");
assertThat(((Number) searchResponse.getHits().getAt(0).getSortValues()[0]).doubleValue(), closeTo(0d, 10d));
assertThat(((Number) searchResponse.getHits().getAt(1).getSortValues()[0]).doubleValue(), closeTo(1157.0d, 10d));
assertThat(((Number) searchResponse.getHits().getAt(2).getSortValues()[0]).doubleValue(), closeTo(2874.0d, 10d));
assertThat(((Number) searchResponse.getHits().getAt(3).getSortValues()[0]).doubleValue(), closeTo(5301.0d, 10d));
searchResponse = client().prepareSearch("companies").setQuery(matchAllQuery()).addSort(SortBuilders.geoDistanceSort("branches.location", 40.7143528, -74.0059731).setNestedPath("branches").sortMode(SortMode.AVG).order(SortOrder.DESC).setNestedPath("branches")).execute().actionGet();
assertHitCount(searchResponse, 4);
assertOrderedSearchHits(searchResponse, "4", "2", "3", "1");
assertThat(((Number) searchResponse.getHits().getAt(0).getSortValues()[0]).doubleValue(), closeTo(5301.0d, 10d));
assertThat(((Number) searchResponse.getHits().getAt(1).getSortValues()[0]).doubleValue(), closeTo(2874.0d, 10d));
assertThat(((Number) searchResponse.getHits().getAt(2).getSortValues()[0]).doubleValue(), closeTo(1157.0d, 10d));
assertThat(((Number) searchResponse.getHits().getAt(3).getSortValues()[0]).doubleValue(), closeTo(0d, 10d));
searchResponse = client().prepareSearch("companies").setQuery(matchAllQuery()).addSort(SortBuilders.geoDistanceSort("branches.location", 40.7143528, -74.0059731).setNestedFilter(termQuery("branches.name", "brooklyn")).sortMode(SortMode.AVG).order(SortOrder.ASC).setNestedPath("branches")).execute().actionGet();
assertHitCount(searchResponse, 4);
assertFirstHit(searchResponse, hasId("4"));
assertSearchHits(searchResponse, "1", "2", "3", "4");
assertThat(((Number) searchResponse.getHits().getAt(0).getSortValues()[0]).doubleValue(), closeTo(8572.0d, 10d));
assertThat(((Number) searchResponse.getHits().getAt(1).getSortValues()[0]).doubleValue(), equalTo(Double.POSITIVE_INFINITY));
assertThat(((Number) searchResponse.getHits().getAt(2).getSortValues()[0]).doubleValue(), equalTo(Double.POSITIVE_INFINITY));
assertThat(((Number) searchResponse.getHits().getAt(3).getSortValues()[0]).doubleValue(), equalTo(Double.POSITIVE_INFINITY));
try {
client().prepareSearch("companies").setQuery(matchAllQuery()).addSort(SortBuilders.geoDistanceSort("branches.location", 40.7143528, -74.0059731).sortMode(SortMode.SUM).setNestedPath("branches"));
fail("Sum should not be allowed as sort mode");
} catch (IllegalArgumentException e) {
//expected
}
}
Aggregations