use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.SearchHit in project elasticsearch by elastic.
the class GeoBoundingBoxIT method testSimpleBoundingBoxTest.
public void testSimpleBoundingBoxTest() 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("type1").startObject("properties").startObject("location").field("type", "geo_point");
xContentBuilder.endObject().endObject().endObject().endObject();
assertAcked(prepareCreate("test").setSettings(settings).addMapping("type1", xContentBuilder));
ensureGreen();
client().prepareIndex("test", "type1", "1").setSource(jsonBuilder().startObject().field("name", "New York").startObject("location").field("lat", 40.7143528).field("lon", -74.0059731).endObject().endObject()).execute().actionGet();
// to NY: 5.286 km
client().prepareIndex("test", "type1", "2").setSource(jsonBuilder().startObject().field("name", "Times Square").startObject("location").field("lat", 40.759011).field("lon", -73.9844722).endObject().endObject()).execute().actionGet();
// to NY: 0.4621 km
client().prepareIndex("test", "type1", "3").setSource(jsonBuilder().startObject().field("name", "Tribeca").startObject("location").field("lat", 40.718266).field("lon", -74.007819).endObject().endObject()).execute().actionGet();
// to NY: 1.055 km
client().prepareIndex("test", "type1", "4").setSource(jsonBuilder().startObject().field("name", "Wall Street").startObject("location").field("lat", 40.7051157).field("lon", -74.0088305).endObject().endObject()).execute().actionGet();
// to NY: 1.258 km
client().prepareIndex("test", "type1", "5").setSource(jsonBuilder().startObject().field("name", "Soho").startObject("location").field("lat", 40.7247222).field("lon", -74).endObject().endObject()).execute().actionGet();
// to NY: 2.029 km
client().prepareIndex("test", "type1", "6").setSource(jsonBuilder().startObject().field("name", "Greenwich Village").startObject("location").field("lat", 40.731033).field("lon", -73.9962255).endObject().endObject()).execute().actionGet();
// to NY: 8.572 km
client().prepareIndex("test", "type1", "7").setSource(jsonBuilder().startObject().field("name", "Brooklyn").startObject("location").field("lat", 40.65).field("lon", -73.95).endObject().endObject()).execute().actionGet();
client().admin().indices().prepareRefresh().execute().actionGet();
SearchResponse searchResponse = // from NY
client().prepareSearch().setQuery(geoBoundingBoxQuery("location").setCorners(40.73, -74.1, 40.717, -73.99)).execute().actionGet();
assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L));
assertThat(searchResponse.getHits().getHits().length, equalTo(2));
for (SearchHit hit : searchResponse.getHits()) {
assertThat(hit.getId(), anyOf(equalTo("1"), equalTo("3"), equalTo("5")));
}
searchResponse = // from NY
client().prepareSearch().setQuery(geoBoundingBoxQuery("location").setCorners(40.73, -74.1, 40.717, -73.99).type("indexed")).execute().actionGet();
assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L));
assertThat(searchResponse.getHits().getHits().length, equalTo(2));
for (SearchHit hit : searchResponse.getHits()) {
assertThat(hit.getId(), anyOf(equalTo("1"), equalTo("3"), equalTo("5")));
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.SearchHit in project elasticsearch by elastic.
the class DuelScrollIT method testDuelIndexOrder.
private void testDuelIndexOrder(SearchType searchType, boolean trackScores, int numDocs) throws Exception {
final int size = scaledRandomIntBetween(5, numDocs + 5);
final SearchResponse control = client().prepareSearch("test").setSearchType(searchType).setSize(numDocs).setQuery(QueryBuilders.matchQuery("foo", "true")).addSort(SortBuilders.fieldSort("_doc")).setTrackScores(trackScores).get();
assertNoFailures(control);
SearchResponse scroll = client().prepareSearch("test").setSearchType(searchType).setSize(size).setQuery(QueryBuilders.matchQuery("foo", "true")).addSort(SortBuilders.fieldSort("_doc")).setTrackScores(trackScores).setScroll("10m").get();
int scrollDocs = 0;
try {
while (true) {
assertNoFailures(scroll);
assertEquals(control.getHits().getTotalHits(), scroll.getHits().getTotalHits());
assertEquals(control.getHits().getMaxScore(), scroll.getHits().getMaxScore(), 0.01f);
if (scroll.getHits().getHits().length == 0) {
break;
}
for (int i = 0; i < scroll.getHits().getHits().length; ++i) {
SearchHit controlHit = control.getHits().getAt(scrollDocs + i);
SearchHit scrollHit = scroll.getHits().getAt(i);
assertEquals(controlHit.getId(), scrollHit.getId());
}
scrollDocs += scroll.getHits().getHits().length;
scroll = client().prepareSearchScroll(scroll.getScrollId()).setScroll("10m").get();
}
assertEquals(control.getHits().getTotalHits(), scrollDocs);
} catch (AssertionError e) {
logger.info("Control:\n{}", control);
logger.info("Scroll size={}, from={}:\n{}", size, scrollDocs, scroll);
throw e;
} finally {
clearScroll(scroll.getScrollId());
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.SearchHit in project elasticsearch by elastic.
the class SearchQueryIT method testConstantScoreQuery.
// see #3521
public void testConstantScoreQuery() throws Exception {
Random random = random();
createIndex("test");
indexRandom(true, client().prepareIndex("test", "type1", "1").setSource("field1", "quick brown fox", "field2", "quick brown fox"), client().prepareIndex("test", "type1", "2").setSource("field1", "quick lazy huge brown fox", "field2", "quick lazy huge brown fox"));
SearchResponse searchResponse = client().prepareSearch().setQuery(constantScoreQuery(matchQuery("field1", "quick"))).get();
assertHitCount(searchResponse, 2L);
for (SearchHit searchHit : searchResponse.getHits().getHits()) {
assertSearchHit(searchHit, hasScore(1.0f));
}
searchResponse = client().prepareSearch("test").setQuery(boolQuery().must(matchAllQuery()).must(constantScoreQuery(matchQuery("field1", "quick")).boost(1.0f + random().nextFloat()))).get();
assertHitCount(searchResponse, 2L);
assertFirstHit(searchResponse, hasScore(searchResponse.getHits().getAt(1).getScore()));
client().prepareSearch("test").setQuery(constantScoreQuery(matchQuery("field1", "quick")).boost(1.0f + random().nextFloat())).get();
assertHitCount(searchResponse, 2L);
assertFirstHit(searchResponse, hasScore(searchResponse.getHits().getAt(1).getScore()));
searchResponse = client().prepareSearch("test").setQuery(constantScoreQuery(boolQuery().must(matchAllQuery()).must(constantScoreQuery(matchQuery("field1", "quick")).boost(1.0f + (random.nextBoolean() ? 0.0f : random.nextFloat()))))).get();
assertHitCount(searchResponse, 2L);
assertFirstHit(searchResponse, hasScore(searchResponse.getHits().getAt(1).getScore()));
for (SearchHit searchHit : searchResponse.getHits().getHits()) {
assertSearchHit(searchHit, hasScore(1.0f));
}
int num = scaledRandomIntBetween(100, 200);
IndexRequestBuilder[] builders = new IndexRequestBuilder[num];
for (int i = 0; i < builders.length; i++) {
builders[i] = client().prepareIndex("test_1", "type", "" + i).setSource("f", English.intToEnglish(i));
}
createIndex("test_1");
indexRandom(true, builders);
int queryRounds = scaledRandomIntBetween(10, 20);
for (int i = 0; i < queryRounds; i++) {
MatchQueryBuilder matchQuery = matchQuery("f", English.intToEnglish(between(0, num)));
searchResponse = client().prepareSearch("test_1").setQuery(constantScoreQuery(matchQuery)).setSize(num).get();
long totalHits = searchResponse.getHits().getTotalHits();
SearchHits hits = searchResponse.getHits();
for (SearchHit searchHit : hits) {
assertSearchHit(searchHit, hasScore(1.0f));
}
searchResponse = client().prepareSearch("test_1").setQuery(boolQuery().must(matchAllQuery()).must(constantScoreQuery(matchQuery).boost(1.0f + (random.nextBoolean() ? 0.0f : random.nextFloat())))).setSize(num).get();
hits = searchResponse.getHits();
assertThat(hits.getTotalHits(), equalTo(totalHits));
if (totalHits > 1) {
float expected = hits.getAt(0).getScore();
for (SearchHit searchHit : hits) {
assertSearchHit(searchHit, hasScore(expected));
}
}
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.SearchHit in project elasticsearch by elastic.
the class SearchScrollIT method testCloseAndReopenOrDeleteWithActiveScroll.
public void testCloseAndReopenOrDeleteWithActiveScroll() throws IOException {
createIndex("test");
for (int i = 0; i < 100; i++) {
client().prepareIndex("test", "type1", Integer.toString(i)).setSource(jsonBuilder().startObject().field("field", i).endObject()).execute().actionGet();
}
refresh();
SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(35).setScroll(TimeValue.timeValueMinutes(2)).addSort("field", SortOrder.ASC).execute().actionGet();
long counter = 0;
assertThat(searchResponse.getHits().getTotalHits(), equalTo(100L));
assertThat(searchResponse.getHits().getHits().length, equalTo(35));
for (SearchHit hit : searchResponse.getHits()) {
assertThat(((Number) hit.getSortValues()[0]).longValue(), equalTo(counter++));
}
if (randomBoolean()) {
client().admin().indices().prepareClose("test").get();
client().admin().indices().prepareOpen("test").get();
ensureGreen("test");
} else {
client().admin().indices().prepareDelete("test").get();
}
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.SearchHit in project jena by apache.
the class TextIndexES method query.
/**
* Query the ElasticSearch for the given Node, with the given query String and limit.
* @param property the node property to make a search for
* @param qs the query string
* @param limit limit on the number of records to return
* @return List of {@link TextHit}s containing the documents that have been found
*/
@Override
public List<TextHit> query(Node property, String qs, String graphURI, String lang, int limit) {
if (property != null) {
qs = parse(property.getLocalName(), qs, lang);
} else {
qs = parse(null, qs, lang);
}
LOGGER.debug("Querying ElasticSearch for QueryString: " + qs);
SearchResponse response = client.prepareSearch(indexName).setTypes(docDef.getEntityField()).setQuery(QueryBuilders.queryStringQuery(qs)).setFetchSource(false).setFrom(0).setSize(limit).get();
List<TextHit> results = new ArrayList<>();
for (SearchHit hit : response.getHits()) {
//It has been decided to return NULL literal values for now.
String entityField = hit.getId();
Node entityNode = TextQueryFuncs.stringToNode(entityField);
Float score = hit.getScore();
TextHit textHit = new TextHit(entityNode, score, null);
results.add(textHit);
}
return results;
}
Aggregations