Java Code Examples for org.elasticsearch.search.SearchHits

The following examples show how to use org.elasticsearch.search.SearchHits. These examples are extracted from open source projects. 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 Project: dht-spider   Source File: ESClient.java    License: MIT License 7 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 Project: jakduk-api   Source File: SearchService.java    License: 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 3
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 4
Source Project: camel-kafka-connector   Source File: ElasticSearchClient.java    License: 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 5
Source Project: storm-crawler   Source File: ScrollSpout.java    License: 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 6
Source Project: anomaly-detection   Source File: TestHelpers.java    License: 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 7
Source Project: foxtrot   Source File: ConsoleHistoryManager.java    License: 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 8
Source Project: vertexium   Source File: ElasticsearchSearchQueryBase.java    License: 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 9
Source Project: foxtrot   Source File: ElasticsearchConsolePersistence.java    License: 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 10
@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 11
Source Project: elasticsearch-jest-example   Source File: TransportClient.java    License: 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 12
Source Project: elasticsearch-sql   Source File: MinusExecutor.java    License: 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 13
Source Project: sanshanblog   Source File: ElasticSearchService.java    License: 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 14
Source Project: sanshanblog   Source File: ElasticSearchService.java    License: 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 15
Source Project: elasticsearch-sql   Source File: QueryTest.java    License: 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 16
Source Project: ingestion   Source File: AbstractElasticSearchSinkTest.java    License: 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 17
Source Project: metron   Source File: ElasticsearchRequestSubmitterTest.java    License: 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 18
Source Project: occurrence   Source File: OccurrenceSearchEsImpl.java    License: 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 19
Source Project: scava   Source File: ElasticSearchClient.java    License: 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 20
Source Project: conductor   Source File: ElasticSearchDAOV5.java    License: 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 21
@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 22
Source Project: elasticsearch-sql   Source File: AggregationTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void topHitTest_WithInclude() throws IOException, SqlParseException, SQLFeatureNotSupportedException {
    Aggregations result = query(String.format("select topHits('size'=3,age='desc',include=age) from %s/account group by gender ", TEST_INDEX_ACCOUNT));
    List<? extends Terms.Bucket> buckets = ((Terms) (result.asList().get(0))).getBuckets();
    for (Terms.Bucket bucket : buckets){
        SearchHits hits = ((InternalTopHits) bucket.getAggregations().asList().get(0)).getHits();
        for(SearchHit hit: hits ){
            Set<String> fields = hit.getSourceAsMap().keySet();
            Assert.assertEquals(1,fields.size());
            Assert.assertEquals("age",fields.toArray()[0]);
        }
    }
}
 
Example 23
Source Project: ProjectStudy   Source File: HighLevelRestController.java    License: MIT License 5 votes vote down vote up
/**
   * 列表查询
   *
   * @param page
* @param rows
* @param keyword
   * @return com.example.common.ResponseBean
   * @throws
   * @author wliduo[[email protected]]
   * @date 2019/8/15 16:01
   */
  @GetMapping("/book")
  public ResponseBean list(@RequestParam(defaultValue = "1") Integer page,
                           @RequestParam(defaultValue = "10") Integer rows,
                           String keyword) throws IOException {
      SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
      // 分页采用简单的from + size分页,适用数据量小的,了解更多分页方式可自行查阅资料
      searchSourceBuilder.from((page - 1) * rows);
      searchSourceBuilder.size(rows);
      // 查询条件,只有查询关键字不为空才带查询条件
      if (StringUtils.isNoneBlank(keyword)) {
          QueryBuilder queryBuilder = QueryBuilders.multiMatchQuery(keyword, "name", "desc");
          searchSourceBuilder.query(queryBuilder);
      }
      // 排序,根据ID倒叙
      searchSourceBuilder.sort("id", SortOrder.DESC);
      // SearchRequest
      SearchRequest searchRequest = new SearchRequest();
      searchRequest.source(searchSourceBuilder);
      // 查询ES
      SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
      SearchHits hits = searchResponse.getHits();
      // 获取总数
      Long total = hits.getTotalHits().value;
      // 遍历封装列表对象
      List<BookDto> bookDtoList = new ArrayList<>();
      SearchHit[] searchHits = hits.getHits();
      for (SearchHit searchHit : searchHits) {
          bookDtoList.add(JSON.parseObject(searchHit.getSourceAsString(), BookDto.class));
      }
      // 封装Map参数返回
      Map<String, Object> result = new HashMap<String, Object>(16);
      result.put("count", total);
      result.put("data", bookDtoList);
      return new ResponseBean(HttpStatus.OK.value(), "查询成功", result);
  }
 
Example 24
Source Project: fess   Source File: EsAbstractBehavior.java    License: Apache License 2.0 5 votes vote down vote up
@Override
protected int delegateQueryDelete(final ConditionBean cb, final DeleteOption<? extends ConditionBean> option) {
    final SearchRequestBuilder builder = client.prepareSearch(asEsIndex()).setScroll(scrollForDelete).setSize(sizeForDelete);
    final EsAbstractConditionBean esCb = (EsAbstractConditionBean) cb;
    if (esCb.getPreference() != null) {
        esCb.setPreference(esCb.getPreference());
    }
    esCb.request().build(builder);
    SearchResponse response = esCb.build(builder).execute().actionGet(scrollSearchTimeout);
    String scrollId = response.getScrollId();
    int count = 0;
    try {
        while (scrollId != null) {
            final SearchHits searchHits = getSearchHits(response);
            final SearchHit[] hits = searchHits.getHits();
            if (hits.length == 0) {
                break;
            }

            final BulkRequestBuilder bulkRequest = client.prepareBulk();
            for (final SearchHit hit : hits) {
                bulkRequest.add(client.prepareDelete().setIndex(asEsIndex()).setId(hit.getId()));
            }
            count += hits.length;
            final BulkResponse bulkResponse = bulkRequest.execute().actionGet(bulkTimeout);
            if (bulkResponse.hasFailures()) {
                throw new IllegalBehaviorStateException(bulkResponse.buildFailureMessage());
            }

            response = client.prepareSearchScroll(scrollId).setScroll(scrollForDelete).execute().actionGet(scrollSearchTimeout);
            if (!scrollId.equals(response.getScrollId())) {
                deleteScrollContext(scrollId);
            }
        }
    } finally {
        deleteScrollContext(scrollId);
    }
    return count;
}
 
Example 25
Source Project: elasticsearch-sql   Source File: QueryTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void complexObjectReutrnField() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
    SearchHits response = query(String.format("SELECT parents.father FROM %s/gotCharacters where name.firstname = 'Brandon' LIMIT 1000", TEST_INDEX_GAME_OF_THRONES));
    Assert.assertEquals(1, response.getTotalHits().value);
    Map<String, Object> sourceAsMap = response.getHits()[0].getSourceAsMap();
    Assert.assertEquals("Eddard",((HashMap<String,Object>)sourceAsMap.get("parents")).get("father"));
}
 
Example 26
Source Project: elasticsearch-sql   Source File: QueryTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void lessThanTest() 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, lessThan(someAge));
	}
}
 
Example 27
Source Project: vertexium   Source File: ElasticsearchSearchQueryBase.java    License: 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.getField(Elasticsearch5SearchIndex.ELEMENT_ID_FIELD_NAME).getValue();
            return new ElasticsearchVertex(
                getGraph(),
                elementId,
                fetchHints,
                authorizations
            );
        }).collect(Collectors.toList());
}
 
Example 28
Source Project: elasticsearch-sql   Source File: ElasticJoinExecutor.java    License: Apache License 2.0 5 votes vote down vote up
public void run() throws IOException, SqlParseException {
    long timeBefore = System.currentTimeMillis();
    List<SearchHit> combinedSearchHits =  innerRun();
    int resultsSize = combinedSearchHits.size();
    SearchHit[] hits = combinedSearchHits.toArray(new SearchHit[resultsSize]);
    this.results = new SearchHits(hits, new TotalHits(resultsSize, TotalHits.Relation.EQUAL_TO), 1.0f);
    long joinTimeInMilli = System.currentTimeMillis() - timeBefore;
    this.metaResults.setTookImMilli(joinTimeInMilli);
}
 
Example 29
Source Project: elasticsearch-sql   Source File: QueryTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void notMissFilterSearch() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
	SearchHits response = query(String.format("SELECT * FROM %s/phrase WHERE insert_time2 IS NOT missing", TEST_INDEX_PHRASE));
	SearchHit[] hits = response.getHits();

	// should be 2 according to the data.
	Assert.assertEquals(response.getTotalHits().value, 2);
	for(SearchHit hit : hits) {
		assertThat(hit.getSourceAsMap(), hasKey("insert_time2"));
	}
}
 
Example 30
Source Project: elasticsearch-sql   Source File: QueryTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void notLikeTests() throws IOException, SqlParseException, SQLFeatureNotSupportedException{
    //cant use string.format cause of %d
    SearchHits response = query("SELECT name FROM " +TEST_INDEX_GAME_OF_THRONES + "/gotCharacters where name.firstname not like '%d' and name is not null LIMIT 1000");
    Assert.assertEquals(3, response.getTotalHits().value);
    for(SearchHit hit : response.getHits()) {
        Map<String, Object> sourceAsMap = hit.getSourceAsMap();
        String name = ((HashMap<String, Object>) sourceAsMap.get("name")).get("firstname").toString();
        Assert.assertFalse(name+" was in not like %d",name.startsWith("d"));
    }
}