org.elasticsearch.search.SearchHits Java Examples

The following examples show how to use org.elasticsearch.search.SearchHits. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ESClient.java    From dht-spider with MIT License 10 votes vote down vote up
public List<MetaData> search(String searchValue) throws Exception{
    List<MetaData> list=new ArrayList<>();
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    searchSourceBuilder.from(0);
    searchSourceBuilder.size(100);
    searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
    searchSourceBuilder.query(QueryBuilders.matchQuery("name", searchValue));
    SearchRequest searchRequest = new SearchRequest();
    searchRequest.indices("torrent");
    searchRequest.source(searchSourceBuilder);
    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    SearchHits hits = searchResponse.getHits();
    SearchHit[] searchHits = hits.getHits();
    for (SearchHit hit : searchHits) {
        Map<String, Object> sourceAsMap = hit.getSourceAsMap();
        list.add(new MetaData((String)sourceAsMap.get("infoHash"),
                Long.parseLong(String.valueOf(sourceAsMap.get("length"))),
                (String)sourceAsMap.get("name"),(String)sourceAsMap.get("nameInfo")));
    }
    return list;
}
 
Example #2
Source File: ElasticSearchDAOV5.java    From conductor with Apache License 2.0 6 votes vote down vote up
@Override
public List<String> searchArchivableWorkflows(String indexName, long archiveTtlDays) {
    QueryBuilder q = QueryBuilders.boolQuery()
        .must(QueryBuilders.rangeQuery("endTime").lt(LocalDate.now(ZoneOffset.UTC).minusDays(archiveTtlDays).toString()).gte(LocalDate.now(ZoneOffset.UTC).minusDays(archiveTtlDays).minusDays(1).toString()))
        .should(QueryBuilders.termQuery("status", "COMPLETED"))
        .should(QueryBuilders.termQuery("status", "FAILED"))
        .should(QueryBuilders.termQuery("status", "TIMED_OUT"))
        .should(QueryBuilders.termQuery("status", "TERMINATED"))
        .mustNot(QueryBuilders.existsQuery("archived"))
        .minimumShouldMatch(1);
    SearchRequestBuilder s = elasticSearchClient.prepareSearch(indexName)
        .setTypes("workflow")
        .setQuery(q)
        .addSort("endTime", SortOrder.ASC)
        .setSize(archiveSearchBatchSize);

    SearchResponse response = s.execute().actionGet();

    SearchHits hits = response.getHits();
    logger.info("Archive search totalHits - {}", hits.getTotalHits());

    return Arrays.stream(hits.getHits())
        .map(SearchHit::getId)
        .collect(Collectors.toCollection(LinkedList::new));
}
 
Example #3
Source File: ElasticSearchClient.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
public SearchHits queryBoostedQueryIndex(String boostedQuery, String field, int resultsSize) throws IOException
{
	if(client==null)
		return null;
	
	QueryBuilder query = QueryBuilders.queryStringQuery(boostedQuery).defaultField(field);
	SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
	sourceBuilder.query(query);
	sourceBuilder.size(resultsSize);
	
	SearchRequest searchRequest = new SearchRequest();
	searchRequest.source(sourceBuilder);
	
	SearchResponse searchResponse = client.search(searchRequest);
	return searchResponse.getHits();
}
 
Example #4
Source File: ElasticsearchNamespaceStore.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public Iterator<SimpleNamespace> iterator() {

	SearchResponse searchResponse = clientProvider.getClient()
			.prepareSearch(index)
			.addSort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC)
			.setQuery(QueryBuilders.constantScoreQuery(matchAllQuery()))
			.setSize(10000)
			.get();

	SearchHits hits = searchResponse.getHits();
	if (hits.totalHits > 10000) {
		throw new SailException("Namespace store only supports 10 000 items, found " + hits.totalHits);
	}

	return StreamSupport.stream(hits.spliterator(), false)
			.map(SearchHit::getSourceAsMap)
			.map(map -> new SimpleNamespace(map.get(PREFIX).toString(), map.get(NAMESPACE).toString()))
			.iterator();

}
 
Example #5
Source File: SearchService.java    From jakduk-api with MIT License 6 votes vote down vote up
private SearchGalleryResult getGallerySearchResponse(SearchResponse searchResponse) {
	SearchHits searchHits = searchResponse.getHits();

	List<EsGallerySource> searchList = Arrays.stream(searchHits.getHits())
			.map(searchHit -> {
				Map<String, Object> sourceMap = searchHit.getSourceAsMap();
				EsGallerySource esGallerySource = ObjectMapperUtils.convertValue(sourceMap, EsGallerySource.class);
				esGallerySource.setScore(searchHit.getScore());

				Map<String, List<String>> highlight = this.getHighlight(searchHit.getHighlightFields().entrySet());
				esGallerySource.setHighlight(highlight);

				return esGallerySource;
			})
			.collect(Collectors.toList());

	return new SearchGalleryResult() {{
		setTook(searchResponse.getTook().getMillis());
		setTotalCount(searchHits.getTotalHits());
		setGalleries(searchList);
	}};
}
 
Example #6
Source File: ElasticSearchClient.java    From camel-kafka-connector with Apache License 2.0 6 votes vote down vote up
private boolean hasData(int expect) {
    SearchHits searchHits = getData();

    if (searchHits == null) {
        LOG.debug("There are not search hit to return");

        return false;
    }

    SearchHit[] hits = searchHits.getHits();
    if (hits == null) {
        LOG.debug("Empty data set");

        return false;
    }

    int count = hits.length;

    if (count != expect) {
        LOG.debug("Not enough records: {} available, but {} expected", count, expect);

        return false;
    }

    return true;
}
 
Example #7
Source File: CompleteSetupIntegrationTest.java    From searchanalytics-bigdata with MIT License 6 votes vote down vote up
private void FlumeESSinkAndTestData(List<Event> searchEvents)
		throws EventDeliveryException, IOException, FileNotFoundException {
	flumeESSinkService.processEvents(searchEvents);

	Client client = searchClientService.getClient();
	client.admin().indices().refresh(Requests.refreshRequest()).actionGet();

	String indexName = "recentlyviewed" + '-'
			+ ElasticSearchIndexRequestBuilderFactory.df.format(new Date());
	long totalCount = client.prepareCount(indexName).get().getCount();
	System.out.println("Search total count is: " + totalCount);

	SearchHits hits = client.prepareSearch(indexName).get().getHits();
	System.out.println("Total hits: " + hits.getTotalHits());
	for (SearchHit searchHit : hits) {
		System.out.println(searchHit.getSource());
	}
}
 
Example #8
Source File: OccurrenceSearchEsImpl.java    From occurrence with Apache License 2.0 6 votes vote down vote up
private <T> T searchByKey(Long key, Function<SearchHit, T> mapper) {
  //This should be changed to use GetRequest once ElasticSearch stores id correctly
  SearchRequest searchRequest = new SearchRequest();
  SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
  searchSourceBuilder.size(1);
  searchRequest.indices(esIndex);
  searchSourceBuilder.query(QueryBuilders.termQuery(OccurrenceEsField.GBIF_ID.getFieldName(), key));
  searchRequest.source(searchSourceBuilder);
  try {
    SearchHits hits = esClient.search(searchRequest, HEADERS.get()).getHits();
    if (hits != null && hits.totalHits > 0) {
      return mapper.apply(hits.getAt(0));
    }
    return null;
  } catch (IOException ex) {
    throw new SearchException(ex);
  }
}
 
Example #9
Source File: ScrollSpout.java    From storm-crawler with Apache License 2.0 6 votes vote down vote up
@Override
public void onResponse(SearchResponse response) {
    SearchHits hits = response.getHits();
    LOG.info("{} ES query returned {} hits in {} msec", logIdprefix,
            hits.getHits().length, response.getTook().getMillis());
    hasFinished = hits.getHits().length == 0;
    synchronized (this.queue) {
        // Unlike standard spouts, the scroll queries should never return
        // the same
        // document twice -> no need to look in the buffer or cache
        for (SearchHit hit : hits) {
            Map<String, Object> keyValues = hit.getSourceAsMap();
            String url = (String) keyValues.get("url");
            String status = (String) keyValues.get("status");
            String nextFetchDate = (String) keyValues.get("nextFetchDate");
            Metadata metadata = fromKeyValues(keyValues);
            metadata.setValue(
                    AbstractStatusUpdaterBolt.AS_IS_NEXTFETCHDATE_METADATA,
                    nextFetchDate);
            this.queue.add(new Values(url, metadata, Status.valueOf(status)));
        }
    }
    scrollId = response.getScrollId();
    // remove lock
    markQueryReceivedNow();
}
 
Example #10
Source File: ElasticsearchRequestSubmitterTest.java    From metron with Apache License 2.0 6 votes vote down vote up
@Test
public void searchShouldSucceedWhenOK() throws InvalidSearchException, IOException {
  // mocks
  SearchResponse response = mock(SearchResponse.class);
  SearchRequest request = new SearchRequest();

  // response will indicate 1 search hit
  SearchHits hits = mock(SearchHits.class);
  when(hits.getTotalHits()).thenReturn(1L);

  // response will have status of OK and no failed shards
  when(response.status()).thenReturn(RestStatus.OK);
  when(response.getFailedShards()).thenReturn(0);
  when(response.getTotalShards()).thenReturn(2);
  when(response.getHits()).thenReturn(hits);

  // search should succeed
  ElasticsearchRequestSubmitter submitter = setup(response);
  SearchResponse actual = submitter.submitSearch(request);
  assertNotNull(actual);
}
 
Example #11
Source File: AbstractElasticSearchSinkTest.java    From ingestion with Apache License 2.0 6 votes vote down vote up
void assertSearch(int expectedHits, SearchResponse response, Map<String, Object> expectedBody, Event... events) {
  SearchHits hitResponse = response.getHits();
  assertEquals(expectedHits, hitResponse.getTotalHits());

  SearchHit[] hits = hitResponse.getHits();
  Arrays.sort(hits, new Comparator<SearchHit>() {
    @Override
    public int compare(SearchHit o1, SearchHit o2) {
      return o1.getSourceAsString().compareTo(o2.getSourceAsString());
    }
  });

  for (int i = 0; i < events.length; i++) {
    Event event = events[i];
    SearchHit hit = hits[i];
    Map<String, Object> source = hit.getSource();
    if (expectedBody == null) {
      assertEquals(new String(event.getBody()), source.get("@message"));
    } else {
      assertEquals(expectedBody, source.get("@message"));
    }
  }
}
 
Example #12
Source File: QueryTest.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
@Test
public void dateBetweenSearch() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
	DateTimeFormatter formatter = DateTimeFormat.forPattern(DATE_FORMAT);

	DateTime dateLimit1 = new DateTime(2014, 8, 18, 0, 0, 0);
	DateTime dateLimit2 = new DateTime(2014, 8, 21, 0, 0, 0);

	SearchHits response = query(String.format("SELECT insert_time FROM %s/online WHERE insert_time BETWEEN '2014-08-18' AND '2014-08-21' LIMIT 3", TEST_INDEX_ONLINE));
	SearchHit[] hits = response.getHits();
	for(SearchHit hit : hits) {
		Map<String, Object> source = hit.getSourceAsMap();
		DateTime insertTime = formatter.parseDateTime((String) source.get("insert_time"));

		boolean isBetween =
				(insertTime.isAfter(dateLimit1) || insertTime.isEqual(dateLimit1)) &&
				(insertTime.isBefore(dateLimit2) || insertTime.isEqual(dateLimit2));

		Assert.assertTrue("insert_time must be between 2014-08-18 and 2014-08-21", isBetween);
	}
}
 
Example #13
Source File: ElasticSearchService.java    From sanshanblog with Apache License 2.0 6 votes vote down vote up
/**
 * 组装自定义filed结果
 *
 * @param searchResponse
 * @return
 */
private ElasticResponseVO assembleElasticReuturnFiledsResponse(SearchResponse searchResponse) {
    ElasticResponseVO responseVO = new ElasticResponseVO();
    List<ElasticSearchResultDTO> fileds = new LinkedList<>();
    SearchHits hits = searchResponse.getHits();
    SearchHit[] searchHits = hits.hits();
    for (int i = 0; i < searchHits.length; i++) {
        ElasticSearchResultDTO hit = new ElasticSearchResultDTO();
        hit.setFields(searchHits[i].getFields());
        hit.setId(searchHits[i].getId());
        hit.setType(searchHits[i].getType());
        hit.setScore(searchHits[i].getScore());
        fileds.add(hit);
    }
    responseVO.setTotal(hits.getTotalHits());
    responseVO.setResult(fileds);
    return responseVO;
}
 
Example #14
Source File: TestHelpers.java    From anomaly-detection with Apache License 2.0 6 votes vote down vote up
public static SearchResponse createSearchResponse(ToXContentObject o) throws IOException {
    XContentBuilder content = o.toXContent(XContentFactory.jsonBuilder(), ToXContent.EMPTY_PARAMS);

    SearchHit[] hits = new SearchHit[1];
    hits[0] = new SearchHit(0).sourceRef(BytesReference.bytes(content));

    return new SearchResponse(
        new InternalSearchResponse(
            new SearchHits(hits, new TotalHits(1, TotalHits.Relation.EQUAL_TO), 1.0f),
            new InternalAggregations(Collections.emptyList()),
            new Suggest(Collections.emptyList()),
            new SearchProfileShardResults(Collections.emptyMap()),
            false,
            false,
            1
        ),
        "",
        5,
        5,
        0,
        100,
        ShardSearchFailure.EMPTY_ARRAY,
        SearchResponse.Clusters.EMPTY
    );
}
 
Example #15
Source File: ElasticSearchService.java    From sanshanblog with Apache License 2.0 6 votes vote down vote up
/**
 * 组装返回Source结果
 *
 * @param searchResponse
 * @return
 */
private ElasticResponseVO assembleElasticReuturnSourceResponse(SearchResponse searchResponse) {
    ElasticResponseVO responseVO = new ElasticResponseVO();
    List<ElasticSearchResultDTO> sources = new LinkedList<>();
    SearchHits hits = searchResponse.getHits();
    SearchHit[] searchHits = hits.hits();
    for (int i = 0; i < searchHits.length; i++) {
        ElasticSearchResultDTO hit = new ElasticSearchResultDTO();
        hit.setSource(searchHits[i].getSource());
        hit.setId(searchHits[i].getId());
        hit.setType(searchHits[i].getType());
        hit.setScore(searchHits[i].getScore());
        sources.add(hit);
    }
    responseVO.setTotal(hits.getTotalHits());
    responseVO.setResult(sources);
    return responseVO;
}
 
Example #16
Source File: ConsoleHistoryManager.java    From foxtrot with Apache License 2.0 6 votes vote down vote up
private void deleteOldData(final String name) {
    String updatedAt = "updatedAt";
    try {
        SearchHits searchHits = connection.getClient()
                .prepareSearch(INDEX_HISTORY)
                .setTypes(TYPE)
                .setSearchType(SearchType.QUERY_THEN_FETCH)
                .setQuery(QueryBuilders.termQuery("name.keyword", name))
                .addSort(SortBuilders.fieldSort(updatedAt)
                                 .order(SortOrder.DESC))
                .setFrom(10)
                .setSize(9000)
                .execute()
                .actionGet()
                .getHits();
        for(SearchHit searchHit : CollectionUtils.nullAndEmptySafeValueList(searchHits.getHits())) {
            ConsoleV2 consoleV2 = mapper.readValue(searchHit.getSourceAsString(), ConsoleV2.class);
            elasticsearchConsolePersistence.deleteOldVersion(consoleV2.getId());
        }
    } catch (Exception e) {
        throw new ConsoleFetchException(e);
    }
}
 
Example #17
Source File: ElasticsearchSearchQueryBase.java    From vertexium with Apache License 2.0 6 votes vote down vote up
private SearchResponse getSearchResponse(EnumSet<ElasticsearchDocumentType> elementType, FetchHints fetchHints, int skip, int limit, boolean includeAggregations) {
    SearchRequestBuilder q = buildQuery(elementType, fetchHints, includeAggregations)
        .setFrom(skip)
        .setSize(limit);
    if (QUERY_LOGGER.isTraceEnabled()) {
        QUERY_LOGGER.trace("query: %s", q);
    }

    SearchResponse searchResponse = checkForFailures(q.execute().actionGet());
    SearchHits hits = searchResponse.getHits();
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug(
            "elasticsearch results %d of %d (time: %dms)",
            hits.getHits().length,
            hits.getTotalHits(),
            searchResponse.getTookInMillis()
        );
    }
    return searchResponse;
}
 
Example #18
Source File: MinusExecutor.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
private void fillMinusHitsFromOneField(String fieldName, Set<Object> fieldValues, SearchHit someHit) {
    List<SearchHit> minusHitsList = new ArrayList<>();
    int currentId = 1;
    for(Object result : fieldValues){
        Map<String,DocumentField> fields = new HashMap<>();
        ArrayList<Object> values = new ArrayList<Object>();
        values.add(result);
        fields.put(fieldName,new DocumentField(fieldName, values));
        SearchHit searchHit = new SearchHit(currentId,currentId+"", new Text(someHit.getType()), fields, null);
        searchHit.sourceRef(someHit.getSourceRef());
        searchHit.getSourceAsMap().clear();
        Map<String, Object> sourceAsMap = new HashMap<>();
        sourceAsMap.put(fieldName,result);
        searchHit.getSourceAsMap().putAll(sourceAsMap);
        currentId++;
        minusHitsList.add(searchHit);
    }
    int totalSize = currentId - 1;
    SearchHit[] unionHitsArr = minusHitsList.toArray(new SearchHit[totalSize]);
    this.minusHits = new SearchHits(unionHitsArr, new TotalHits(totalSize, TotalHits.Relation.EQUAL_TO), 1.0f);
}
 
Example #19
Source File: TransportClient.java    From elasticsearch-jest-example with MIT License 6 votes vote down vote up
private static void matchAllQuery() {
	Client client = createTransportClient();
	SearchResponse searchResponse = client.prepareSearch("book")
			.setQuery(QueryBuilders.matchAllQuery())
			.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
	        // 设置查询数据的位置,分页用
			.setFrom(0)
			// 设置查询结果集的最大条数
			.setSize(60)
			// 设置是否按查询匹配度排序
			.setExplain(true)
			.execute()
			.actionGet();
	SearchHits searchHits = searchResponse.getHits();
	System.out.println("-----------------matchAllQuery---------------------");
	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();
	}
}
 
Example #20
Source File: ElasticsearchSupportHandler.java    From super-cloudops with Apache License 2.0 6 votes vote down vote up
@Override
public List<T> findAll(SearchRequest searchRequest) throws Exception {
	SearchResponse searchResp = this.restHighLevelClient.search(searchRequest);
	for (ShardSearchFailure failure : searchResp.getShardFailures()) {
		listener.onFailure(failure);
	}
	SearchHits hits = searchResp.getHits();
	SearchHit[] searchHits = hits.getHits();
	List<T> list = new ArrayList<>();
	for (SearchHit hit : searchHits) {
		String sourceAsString = hit.getSourceAsString();
		T t = JacksonUtils.parseJSON(sourceAsString, clazzP);
		list.add(t);
	}
	Collections.reverse(list);
	return list;
}
 
Example #21
Source File: ElasticsearchConsolePersistence.java    From foxtrot with Apache License 2.0 6 votes vote down vote up
@Override
public List<ConsoleV2> getAllOldVersions(final String name, final String sortBy) {
    try {
        SearchHits searchHits = connection.getClient()
                .prepareSearch(INDEX_HISTORY)
                .setSearchType(SearchType.QUERY_THEN_FETCH)
                .setQuery(QueryBuilders.termQuery("name.keyword", name))
                .addSort(SortBuilders.fieldSort(sortBy)
                        .order(SortOrder.DESC))
                .setFrom(0)
                .setSize(10)
                .execute()
                .actionGet()
                .getHits();
        List<ConsoleV2> results = new ArrayList<>();
        for (SearchHit searchHit : CollectionUtils.nullAndEmptySafeValueList(searchHits.getHits())) {
            results.add(mapper.readValue(searchHit.getSourceAsString(), ConsoleV2.class));
        }
        return results;
    } catch (Exception e) {
        throw new ConsoleFetchException(e);
    }
}
 
Example #22
Source File: QueryTest.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
@Test
public void greaterThanTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
	int someAge = 25;
	SearchHits response = query(String.format("SELECT * FROM %s WHERE age > %s LIMIT 1000", TEST_INDEX_PEOPLE, someAge));
	SearchHit[] hits = response.getHits();
	for(SearchHit hit : hits) {
		int age = (int) hit.getSourceAsMap().get("age");
		assertThat(age, greaterThan(someAge));
	}
}
 
Example #23
Source File: ESClientTest.java    From emotional_analysis with Apache License 2.0 5 votes vote down vote up
@Test
public void test4(){
	SearchRequestBuilder responsebuilder = client.prepareSearch("twitter").setTypes("tweet") ;
	SearchResponse myresponse=responsebuilder.setQuery(QueryBuilders.matchPhraseQuery("user", "kimchy"))  
			.setFrom(0).setSize(10).setExplain(true).execute().actionGet();
	SearchHits hits = myresponse.getHits();  
	for (int i = 0; i < hits.getHits().length; i++) {  
	           System.out.println(hits.getHits()[i].getSourceAsString());}  
 }
 
Example #24
Source File: ElasticsearchSearchQueryBase.java    From vertexium with Apache License 2.0 5 votes vote down vote up
private List<ElasticsearchVertex> getElasticsearchVertices(SearchHits hits, FetchHints fetchHints, Authorizations authorizations) {
    return stream(hits)
        .map(hit -> {
            String elementId = hit.getFields().get(Elasticsearch7SearchIndex.ELEMENT_ID_FIELD_NAME).getValue();
            return new ElasticsearchVertex(
                getGraph(),
                elementId,
                fetchHints,
                authorizations
            );
        }).collect(Collectors.toList());
}
 
Example #25
Source File: QueryTest.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
@Test
public void inTermsTestWithIdentifiersTreatLikeStrings() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
    SearchHits response = query(String.format("SELECT name FROM %s/gotCharacters WHERE name.firstname = IN_TERMS(daenerys,eddard) LIMIT 1000", TEST_INDEX_GAME_OF_THRONES));
    SearchHit[] hits = response.getHits();
    Assert.assertEquals(2, response.getTotalHits().value);
    for(SearchHit hit : hits) {
        String firstname =  ((Map<String,Object>) hit.getSourceAsMap().get("name")).get("firstname").toString();
        assertThat(firstname, isOneOf("Daenerys", "Eddard"));
    }
}
 
Example #26
Source File: QueryTest.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
@Test
public void andTest() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
	SearchHits response = query(String.format("SELECT * FROM %s WHERE age=32 AND gender='M' LIMIT 1000", TEST_INDEX_PEOPLE));
	SearchHit[] hits = response.getHits();
	for(SearchHit hit : hits) {
		Assert.assertEquals(32, hit.getSourceAsMap().get("age"));
		Assert.assertEquals("M", hit.getSourceAsMap().get("gender"));
	}
}
 
Example #27
Source File: ESQueryMapper.java    From syncer with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Map<String, Object> map(ExtraQuery extraQuery) {
  String[] select = extraQuery.getSelect();
  SearchResponse response;
  try {
    response = client.prepareSearch(extraQuery.getIndexName())
        .setTypes(extraQuery.getTypeName())
        .setSearchType(SearchType.DEFAULT)
        .setFetchSource(select, null)
        .setQuery(getFilter(extraQuery))
        .execute()
        .actionGet();
  } catch (Exception e) {
    logger.error("Fail to do the extra query {}", extraQuery, e);
    return Collections.emptyMap();
  }
  SearchHits hits = response.getHits();
  if (hits.totalHits > 1) {
    // todo toList
    logger.warn("Multiple query results exists, only use the first");
  } else if (hits.totalHits == 0) {
    logger.warn("Fail to find any match by {}", extraQuery);
    return Collections.emptyMap();
  }
  Map<String, Object> hit = hits.getAt(0).getSource();
  Map<String, Object> res = new HashMap<>();
  for (int i = 0; i < select.length; i++) {
    Object value = hit.get(select[i]);
    if (value == null) {
      logger.warn("No {} in {} by {}", select[i], hit, extraQuery);
    }
    res.put(extraQuery.getAs(i), value);
  }
  extraQuery.addQueryResult(res);
  return res;
}
 
Example #28
Source File: ProcessSynchronizerTest.java    From elasticsearch-reindex-tool with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldPollWhatHasPulled() throws Exception {
  //given
  ProcessSynchronizer processSynchronizer = new ProcessSynchronizer(QUERY_SEGMENT_SIZE);
  SearchResponse searchResponse = createSearchResponse();
  processSynchronizer.tryFillQueueWithSearchHits(searchResponse);
  //when
  SearchHits searchHits = processSynchronizer.pollDataToIndexed();
  //then
  assertEquals(searchResponse.getHits(), searchHits);
}
 
Example #29
Source File: QueryTest.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
@Test
public void geoPolygon() throws SQLFeatureNotSupportedException, SqlParseException, InterruptedException {
    SearchHits results = query(String.format("SELECT * FROM %s/location WHERE GEO_POLYGON(center,100,0,100.5,2,101.0,0)", TEST_INDEX_LOCATION));
    org.junit.Assert.assertEquals(1,results.getTotalHits().value);
    SearchHit result = results.getAt(0);
    Assert.assertEquals("square",result.getSourceAsMap().get("description"));
}
 
Example #30
Source File: DetectorMappingRepositoryImplTest.java    From adaptive-alerting with Apache License 2.0 5 votes vote down vote up
@Test(expected = RecordNotFoundException.class)
public void deleteMappingsByDetectorUUID_illegal_args() throws Exception {
    val detectorUuid = UUID.randomUUID();
    val searchIndex = "2";
    val lookUpTime = 100;
    val emptyHits = new SearchHits(new SearchHit[]{}, 0, 0);
    val searchResponse = mockSearchResponse(searchIndex, lookUpTime, detectorUuid.toString());
    Mockito.when(searchResponse.getHits()).thenReturn(emptyHits);
    when(legacyElasticSearchClient.search(any(SearchRequest.class), eq(RequestOptions.DEFAULT))).thenReturn(searchResponse);

    repoUnderTest.deleteMappingsByDetectorUUID(detectorUuid);
}