Java Code Examples for org.elasticsearch.action.search.SearchResponse#getHits()

The following examples show how to use org.elasticsearch.action.search.SearchResponse#getHits() . 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: ESTemplate.java    From canal-1.1.3 with Apache License 2.0 6 votes vote down vote up
/**
 * 通过主键删除数据
 *
 * @param mapping 配置对象
 * @param pkVal 主键值
 * @param esFieldData 数据Map
 */
public void delete(ESMapping mapping, Object pkVal, Map<String, Object> esFieldData) {
    if (mapping.get_id() != null) {
        getBulk().add(transportClient.prepareDelete(mapping.get_index(), mapping.get_type(), pkVal.toString()));
        commitBulk();
    } else {
        SearchResponse response = transportClient.prepareSearch(mapping.get_index())
            .setTypes(mapping.get_type())
            .setQuery(QueryBuilders.termQuery(mapping.getPk(), pkVal))
            .setSize(10000)
            .get();
        for (SearchHit hit : response.getHits()) {
            getBulk().add(transportClient.prepareUpdate(mapping.get_index(), mapping.get_type(), hit.getId())
                .setDoc(esFieldData));
            commitBulk();
        }
    }

}
 
Example 2
Source File: ResultMapperExt.java    From roncoo-education with MIT License 6 votes vote down vote up
@SuppressWarnings("deprecation")
@Override
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
	long totalHits = response.getHits().totalHits();
	List<T> results = new ArrayList<>();
	for (SearchHit hit : response.getHits()) {
		if (hit != null) {
			T result = null;
			if (StringUtils.hasText(hit.sourceAsString())) {
				result = mapEntity(hit.sourceAsString(), clazz);
			} else {
				result = mapEntity(hit.getFields().values(), clazz);
			}
			setPersistentEntityId(result, hit.getId(), clazz);
			setPersistentEntityVersion(result, hit.getVersion(), clazz);
			populateScriptFields(result, hit);

			// 高亮查询
			populateHighLightedFields(result, hit.getHighlightFields());
			results.add(result);
		}
	}

	return new AggregatedPageImpl<T>(results, pageable, totalHits, response.getAggregations(), response.getScrollId());
}
 
Example 3
Source File: UKResultMapper.java    From youkefu with Apache License 2.0 6 votes vote down vote up
@Override
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
	long totalHits = response.getHits().totalHits();
	List<T> results = new ArrayList<T>();
	for (SearchHit hit : response.getHits()) {
		if (hit != null) {
			T result = null;
			if (StringUtils.isNotBlank(hit.sourceAsString())) {
				result = mapEntity(hit.sourceAsString() , hit , clazz);
			} else {
				result = mapEntity(hit.getFields().values() , hit , clazz);
			}
			setPersistentEntityId(result, hit.getId(), clazz);
			populateScriptFields(result, hit);
			results.add(result);
		}
	}

	return new AggregatedPageImpl<T>(results, pageable, totalHits);
}
 
Example 4
Source File: ConfService.java    From SkaETL with Apache License 2.0 5 votes vote down vote up
private void treatResponse(SearchResponse searchResponse) {
    for (SearchHit searchHit : searchResponse.getHits()) {
        String res = searchHit.getSourceAsString();
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            ConfEsSkalogs conf = objectMapper.readValue(res, ConfEsSkalogs.class);
            map.put(conf.getConfigurationLogstash().getIdConfiguration(),conf.getConfigurationLogstash());
            log.info("Add configuration {}",conf.getConfigurationLogstash());
        }catch(Exception e){
            log.error("Pwoblem during parsing {}",e);
        }
    }
}
 
Example 5
Source File: ESTemplate.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
/**
 * 插入数据
 *
 * @param mapping 配置对象
 * @param pkVal 主键值
 * @param esFieldData 数据Map
 */
public void insert(ESMapping mapping, Object pkVal, Map<String, Object> esFieldData) {
    if (mapping.get_id() != null) {
        String parentVal = (String) esFieldData.remove("$parent_routing");
        if (mapping.isUpsert()) {
            UpdateRequestBuilder updateRequestBuilder = transportClient
                .prepareUpdate(mapping.get_index(), mapping.get_type(), pkVal.toString())
                .setDoc(esFieldData)
                .setDocAsUpsert(true);
            if (StringUtils.isNotEmpty(parentVal)) {
                updateRequestBuilder.setRouting(parentVal);
            }
            getBulk().add(updateRequestBuilder);
        } else {
            IndexRequestBuilder indexRequestBuilder = transportClient
                .prepareIndex(mapping.get_index(), mapping.get_type(), pkVal.toString())
                .setSource(esFieldData);
            if (StringUtils.isNotEmpty(parentVal)) {
                indexRequestBuilder.setRouting(parentVal);
            }
            getBulk().add(indexRequestBuilder);
        }
        commitBulk();
    } else {
        SearchResponse response = transportClient.prepareSearch(mapping.get_index())
            .setTypes(mapping.get_type())
            .setQuery(QueryBuilders.termQuery(mapping.getPk(), pkVal))
            .setSize(10000)
            .get();
        for (SearchHit hit : response.getHits()) {
            getBulk().add(transportClient.prepareUpdate(mapping.get_index(), mapping.get_type(), hit.getId())
                .setDoc(esFieldData));
            commitBulk();
        }
    }

}
 
Example 6
Source File: SearchES.java    From Transwarp-Sample-Code with MIT License 5 votes vote down vote up
/**
 * json查询
 */
public static void jsonquery() {
    try {
        Settings settings = Settings.settingsBuilder().put("cluster.name", "elasticsearch1").build();
        TransportClient transportClient = TransportClient.builder().
                settings(settings).build().addTransportAddress(
                new InetSocketTransportAddress(InetAddress.getByName("172.16.2.93"), 9300));
        SearchRequestBuilder searchRequestBuilder = transportClient.prepareSearch("service2");
        SearchResponse searchResponse = searchRequestBuilder.setSource("{\n" +
                "\"query\": {\n" +
                "\"bool\": {\n" +
                "\"must\": [\n" +
                "{\n" +
                "\"prefix\": {\n" +
                "\"content\": \"oracle\"\n" +
                "}\n" +
                "}\n" +
                "],\n" +
                "\"must_not\": [ ],\n" +
                "\"should\": [ ]\n" +
                "}\n" +
                "},\n" +
                "\"from\": 0,\n" +
                "\"size\": 10,\n" +
                "\"sort\": [ ],\n" +
                "\"aggs\": { }\n" +
                "}")
                .get();
        SearchHits searchHits = searchResponse.getHits();
        System.out.println();
        System.out.println("Total Hits is " + searchHits.totalHits());
        System.out.println();
        for (int i = 0; i < searchHits.getHits().length; ++i) {
            System.out.println("content is "
                    + searchHits.getHits()[i].getSource().get("content"));
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 7
Source File: AbstractSearchAction.java    From anomaly-detection with Apache License 2.0 5 votes vote down vote up
private RestResponseListener<SearchResponse> search(RestChannel channel, Class<T> clazz) {
    return new RestResponseListener<SearchResponse>(channel) {
        @Override
        public RestResponse buildResponse(SearchResponse response) throws Exception {
            if (response.isTimedOut()) {
                return new BytesRestResponse(RestStatus.REQUEST_TIMEOUT, response.toString());
            }

            if (clazz == AnomalyDetector.class) {
                for (SearchHit hit : response.getHits()) {
                    XContentParser parser = XContentType.JSON
                        .xContent()
                        .createParser(
                            channel.request().getXContentRegistry(),
                            LoggingDeprecationHandler.INSTANCE,
                            hit.getSourceAsString()
                        );
                    ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);

                    // write back id and version to anomaly detector object
                    ToXContentObject xContentObject = AnomalyDetector.parse(parser, hit.getId(), hit.getVersion());
                    XContentBuilder builder = xContentObject.toXContent(jsonBuilder(), EMPTY_PARAMS);
                    hit.sourceRef(BytesReference.bytes(builder));
                }
            }

            return new BytesRestResponse(RestStatus.OK, response.toXContent(channel.newBuilder(), EMPTY_PARAMS));
        }
    };
}
 
Example 8
Source File: IndexService.java    From disthene-reader with MIT License 5 votes vote down vote up
public String getPathsAsJsonArray(String tenant, String wildcard) throws TooMuchDataExpectedException {
    String regEx = WildcardUtil.getPathsRegExFromWildcard(wildcard);

    SearchResponse response = client.prepareSearch(indexConfiguration.getIndex())
            .setScroll(new TimeValue(indexConfiguration.getTimeout()))
            .setSize(indexConfiguration.getScroll())
            .setQuery(QueryBuilders.filteredQuery(
                    QueryBuilders.regexpQuery("path", regEx),
                    FilterBuilders.termFilter("tenant", tenant)))
            .execute().actionGet();

    // if total hits exceeds maximum - abort right away returning empty array
    if (response.getHits().totalHits() > indexConfiguration.getMaxPaths()) {
        logger.debug("Total number of paths exceeds the limit: " + response.getHits().totalHits());
        throw new TooMuchDataExpectedException("Total number of paths exceeds the limit: " + response.getHits().totalHits() + " (the limit is " + indexConfiguration.getMaxPaths() + ")");
    }

    List<String> paths = new ArrayList<>();
    while (response.getHits().getHits().length > 0) {
        for (SearchHit hit : response.getHits()) {
            paths.add(hit.getSourceAsString());
        }
        response = client.prepareSearchScroll(response.getScrollId())
                .setScroll(new TimeValue(indexConfiguration.getTimeout()))
                .execute().actionGet();
    }

    return "[" + joiner.join(paths) + "]";
}
 
Example 9
Source File: MetadataQueryEsDAO.java    From skywalking with Apache License 2.0 5 votes vote down vote up
@Override
public List<Endpoint> searchEndpoint(String keyword, String serviceId, int limit) throws IOException {
    SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource();

    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    boolQueryBuilder.must().add(QueryBuilders.termQuery(EndpointTraffic.SERVICE_ID, serviceId));

    if (!Strings.isNullOrEmpty(keyword)) {
        String matchCName = MatchCNameBuilder.INSTANCE.build(EndpointTraffic.NAME);
        boolQueryBuilder.must().add(QueryBuilders.matchQuery(matchCName, keyword));
    }

    sourceBuilder.query(boolQueryBuilder);
    sourceBuilder.size(limit);

    SearchResponse response = getClient().search(EndpointTraffic.INDEX_NAME, sourceBuilder);

    List<Endpoint> endpoints = new ArrayList<>();
    for (SearchHit searchHit : response.getHits()) {
        Map<String, Object> sourceAsMap = searchHit.getSourceAsMap();

        final EndpointTraffic endpointTraffic = new EndpointTraffic.Builder().map2Data(sourceAsMap);

        Endpoint endpoint = new Endpoint();
        endpoint.setId(endpointTraffic.id());
        endpoint.setName((String) sourceAsMap.get(EndpointTraffic.NAME));
        endpoints.add(endpoint);
    }

    return endpoints;
}
 
Example 10
Source File: ESSearchTest.java    From summerframework with Apache License 2.0 5 votes vote down vote up
@Test
public void scrollQuery() throws IOException {
    QueryCondition queryCondition = new QueryCondition();
    queryCondition.setQueryBuilder(QueryBuilders.matchAllQuery());
    queryCondition.setSize(10);

    SearchResponse scrollResponse = elasticsearchTemplate.executeQuery("es_test", queryCondition, "type");

    String scrollId = scrollResponse.getScrollId();
    long total = scrollResponse.getHits().totalHits;
    log.info("\nscrollId is:{}\n", scrollId);
    log.info("\ntotal is:{}\n", total);

    List<Spu> content;
    if (total < 10) {
        content = elasticsearchTemplate.analyzeSearchResponse(Spu.class, scrollResponse);
        assertThat(content.size(), is(3));
    } else {
        for (int i = 0, sum = 0; sum < total; i++) {
            content = elasticsearchTemplate.analyzeSearchResponse(Spu.class, esClient.prepareSearchScroll(scrollId)
                .setScroll(TimeValue.timeValueMinutes(8)).execute().actionGet());

            sum += 10;
            log.info("\n总量{} 已经查到{}", total, sum);
            assertThat(content.size(), is(10));
        }
    }
}
 
Example 11
Source File: MinusExecutor.java    From elasticsearch-sql with Apache License 2.0 4 votes vote down vote up
private MinusOneFieldAndOptimizationResult runWithScrollingAndAddFilter(String firstFieldName ,String secondFieldName) throws SqlParseException {
    SearchResponse scrollResp = ElasticUtils.scrollOneTimeWithHits(this.client, this.builder.getFirstSearchRequest(),
            builder.getOriginalSelect(true), this.maxDocsToFetchOnEachScrollShard);
    Set<Object> results = new HashSet<>();
    int currentNumOfResults = 0;
    SearchHit[] hits = scrollResp.getHits().getHits();
    SearchHit someHit = null;
    if(hits.length!=0){
        //we need some hit for creating InnerResults.
        someHit = hits[0];
    }
    int totalDocsFetchedFromFirstTable = 0;
    int totalDocsFetchedFromSecondTable = 0;
    Where originalWhereSecondTable = this.builder.getOriginalSelect(false).getWhere();
    while (hits.length != 0 ) {
        totalDocsFetchedFromFirstTable+=hits.length;
        Set<Object> currentSetFromResults = new HashSet<>();
        fillSetFromHits(firstFieldName, hits, currentSetFromResults);
        //fetch from second
        Select secondQuerySelect = this.builder.getOriginalSelect(false);
        Where where = createWhereWithOrigianlAndTermsFilter(secondFieldName, originalWhereSecondTable, currentSetFromResults);
        secondQuerySelect.setWhere(where);
        DefaultQueryAction queryAction = new DefaultQueryAction(this.client, secondQuerySelect);
        queryAction.explain();
        if(totalDocsFetchedFromSecondTable > this.maxDocsToFetchOnSecondTable){
            break;
        }
        SearchResponse responseForSecondTable = ElasticUtils.scrollOneTimeWithHits(this.client, queryAction.getRequestBuilder(),secondQuerySelect,this.maxDocsToFetchOnEachScrollShard);
        SearchHits secondQuerySearchHits = responseForSecondTable.getHits();

        SearchHit[] secondQueryHits = secondQuerySearchHits.getHits();
        while(secondQueryHits.length > 0){
            totalDocsFetchedFromSecondTable+=secondQueryHits.length;
            removeValuesFromSetAccordingToHits(secondFieldName, currentSetFromResults, secondQueryHits);
            if(totalDocsFetchedFromSecondTable > this.maxDocsToFetchOnSecondTable){
                break;
            }
            responseForSecondTable = client.prepareSearchScroll(responseForSecondTable.getScrollId()).setScroll(new TimeValue(600000)).execute().actionGet();
            secondQueryHits = responseForSecondTable.getHits().getHits();
        }
        results.addAll(currentSetFromResults);
        if(totalDocsFetchedFromFirstTable > this.maxDocsToFetchOnFirstTable){
            System.out.println("too many results for first table, stoping at:" + totalDocsFetchedFromFirstTable);
            break;
        }

        scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(600000)).execute().actionGet();
        hits = scrollResp.getHits().getHits();
    }
    return new MinusOneFieldAndOptimizationResult(results,someHit);


}
 
Example 12
Source File: ElasticsearchRetrieveLatestDao.java    From metron with Apache License 2.0 4 votes vote down vote up
/**
 * Return the search hit based on the UUID and sensor type.
 * A callback can be specified to transform the hit into a type T.
 * If more than one hit happens, the first one will be returned.
 */
<T> List<T> searchByGuids(Collection<String> guids, Collection<String> sensorTypes,
    Function<SearchHit, Optional<T>> callback) throws IOException {
  if (guids == null || guids.isEmpty()) {
    return Collections.emptyList();
  }

  // should match any of the guids
  // the 'guid' field must be of type 'keyword' or this term query will not match
  BoolQueryBuilder guidQuery = boolQuery().must(termsQuery(Constants.GUID, guids));

  // should match any of the sensor types
  BoolQueryBuilder sensorQuery = boolQuery();
  sensorTypes.forEach(sensorType -> sensorQuery.should(typeQuery(sensorType + "_doc")));

  // must have a match for both guid and sensor
  BoolQueryBuilder query = boolQuery()
          .must(guidQuery)
          .must(sensorQuery);

  // submit the search
  SearchResponse response;
  try {
    SearchSourceBuilder source = new SearchSourceBuilder()
            .query(query)
            .size(guids.size());
    SearchRequest request = new SearchRequest().source(source);
    response = submitter.submitSearch(request);

  } catch(InvalidSearchException e) {
    throw new IOException(e);
  }

  // transform the search hits to results using the callback
  List<T> results = new ArrayList<>();
  for(SearchHit hit: response.getHits()) {
    Optional<T> result = callback.apply(hit);
    result.ifPresent(r -> results.add(r));
  }

  return results;
}
 
Example 13
Source File: ElasticsearchMailDestination.java    From elasticsearch-imap with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public Set getCurrentlyStoredMessageUids(final Folder folder) throws IOException, MessagingException {

    createIndexIfNotExists();
    
    client.admin().indices().refresh(new RefreshRequest()).actionGet();

    final Set uids = new HashSet();

    final TermQueryBuilder b = QueryBuilders.termQuery("folderUri", folder.getURLName().toString());

    logger.debug("Term query: " + b.buildAsBytes().toUtf8());

    SearchResponse scrollResp = client.prepareSearch().setIndices(index).setTypes(type).setSearchType(SearchType.SCAN).setQuery(b)
            .setScroll(new TimeValue(1000)).setSize(1000).execute().actionGet();

    while (true) {
        scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(1000)).execute().actionGet();
        boolean hitsRead = false;
        for (final SearchHit hit : scrollResp.getHits()) {
            hitsRead = true;

            if (folder instanceof IMAPFolder) {
                uids.add(Long.parseLong(hit.getId().split("::")[0]));
            } else {
                uids.add(hit.getId().split("::")[0]);
            }

            logger.debug("Local: " + hit.getId());
        }
        if (!hitsRead) {
            break;
        }
    }

    logger.debug("Currently locally stored messages for folder {}: {}", folder.getURLName(), uids.size());

    return uids;

}
 
Example 14
Source File: IDAO.java    From spider with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 根据query删除数据
 *
 * @param queryBuilder query
 * @param task         任务实体
 * @return 是否全部数据删除成功
 */
protected boolean deleteByQuery(QueryBuilder queryBuilder, Task task) {
    BulkRequestBuilder bulkRequestBuilder = client.prepareBulk();
    SearchRequestBuilder searchRequestBuilder = client.prepareSearch(INDEX_NAME)
            .setTypes(TYPE_NAME)
            .setQuery(queryBuilder)
            .setSize(100)
            .setScroll(TimeValue.timeValueMinutes(SCROLL_TIMEOUT));
    SearchResponse response = searchRequestBuilder.execute().actionGet();
    while (true) {
        for (SearchHit hit : response.getHits()) {
            bulkRequestBuilder.add(new DeleteRequest(INDEX_NAME, TYPE_NAME, hit.id()));
            if (task != null) {
                task.increaseCount();
            }
        }
        response = client.prepareSearchScroll(response.getScrollId())
                .setScroll(TimeValue.timeValueMinutes(SCROLL_TIMEOUT))
                .execute().actionGet();
        if (response.getHits().getHits().length == 0) {
            if (task != null) {
                task.setDescription("按query%s删除数据ID添加完毕,已经添加%s条,准备执行删除", queryBuilder.toString(), bulkRequestBuilder.numberOfActions());
            }
            LOG.debug("按query{}删除数据ID添加完毕,准备执行删除", queryBuilder.toString());
            break;
        } else {
            if (task != null) {
                task.setDescription("按query%s删除数据已经添加%s条", queryBuilder.toString(), bulkRequestBuilder.numberOfActions());
            }
            LOG.debug("按query{}删除数据已经添加{}条", queryBuilder.toString(), bulkRequestBuilder.numberOfActions());
        }
    }
    if (bulkRequestBuilder.numberOfActions() <= 0) {
        if (task != null) {
            task.setDescription("按query%s删除数据时未找到数据,请检查参数", queryBuilder.toString());
        }
        LOG.debug("按query{}删除数据时未找到数据,请检查参数", queryBuilder.toString());
        return false;
    }
    BulkResponse bulkResponse = bulkRequestBuilder.get();
    if (bulkResponse.hasFailures()) {
        if (task != null) {
            task.setDescription("按query%s删除数据部分失败,%s", queryBuilder.toString(), bulkResponse.buildFailureMessage());
        }
        LOG.error("按query{}删除数据部分失败,{}", queryBuilder.toString(), bulkResponse.buildFailureMessage());
    } else {
        if (task != null) {
            task.setDescription("按query%s删除数据成功,耗时:%s毫秒", queryBuilder.toString(), bulkResponse.getTookInMillis());
        }
        LOG.info("按query{}删除数据成功,耗时:{}毫秒", queryBuilder.toString(), bulkResponse.getTookInMillis());
    }
    return bulkResponse.hasFailures();
}
 
Example 15
Source File: SearchServiceImpl.java    From dk-fitting with Apache License 2.0 4 votes vote down vote up
/**
 * 模糊查询,通配符查询,一个问号代表一个字符,*号代表通配
 * @param hostIps ES集群的ip地址
 * @param clusterName ES集群集群名称
 * @param indexName ES集群的索引名称,可使用多个索引 indexName="test2,test1,test";
 * @param typeName 索引类型,可多个 typeName="doc,pdf,test";
 * @param port ES集群的端口号
 * @param start 记录偏移 , null-默认为0
 * @param size 记录偏移 , null-默认为10
 * @param sentence 需要搜索的词语,
 * @return
 * @throws TException
 */
@Override
public Map<String, String> FuzzyQuery(String hostIps, String clusterName, String indexName, String typeName, int port, int start, int size, String sentence, String field) throws TException {

    Client client=null;
    try {
        client = ESUtils.getClient( hostIps, port, clusterName );
    } catch (Exception e) {
        e.printStackTrace();
    }
    String[] index;
    if (indexName.contains( "," )){
        index = indexName.split( "," );
    }else {
        index = new String[]{indexName};
    }
    String[] type;
    if (typeName.contains( "," )){
        type = typeName.split( "," );
    }else {
        type = new String[]{typeName};
    }
    int start2 = (Integer)start == null ? 0 : start;
    int size2 = (Integer)size == null ? 10 : size;
    Map map=new LinkedHashMap(  );
    long totalHits = 0;
    QueryBuilder qb=null;
    if ((sentence.contains( "?" )||sentence.contains( "*" )||sentence.contains( "?" ))&&(field!=null&&!field.equals( " " ))) {
        if(sentence.contains( "?" )){
            StringBuilder sb=new StringBuilder(  );
            sb.append( sentence.replaceAll( "?","?" ) );
            sentence=sb.toString();
        }
         qb = wildcardQuery( field, sentence );
    }else if (field!=null&&!field.equals( " " )){
        qb = fuzzyQuery( field, sentence).fuzziness(Fuzziness.TWO);
    }
    SearchResponse response = client.prepareSearch(index)//可以是多个index
            .setTypes(type)//可以是多个类型
            .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)//精确查询
            //.setQuery(QueryBuilders.matchAllQuery())    // Query 查询全部
            .setQuery(  qb )
            .setFrom(start2).setSize(size2).setExplain(true)////设置是否按查询匹配度排序
            .setTerminateAfter(1000)//达到1000提前结束
            .get();
    SearchHit[] searchHits = response.getHits().getHits();
    String resp=null;

    for (SearchHit searchHit : searchHits) {
        resp = JSON.toJSONString( searchHit.getSource(), SerializerFeature.PrettyFormat );
        totalHits++;
        map.put( String.valueOf(totalHits ),resp );
    }
    long totalHits1 = response.getHits().totalHits;
    map.put( "count",String.valueOf(totalHits1 ) );

    return map;
}
 
Example 16
Source File: FessEsClient.java    From fess with Apache License 2.0 4 votes vote down vote up
public long deleteByQuery(final String index, final QueryBuilder queryBuilder) {

        final FessConfig fessConfig = ComponentUtil.getFessConfig();
        SearchResponse response =
                client.prepareSearch(index).setScroll(scrollForDelete).setSize(sizeForDelete)
                        .setFetchSource(new String[] { fessConfig.getIndexFieldId() }, null).setQuery(queryBuilder)
                        .setPreference(Constants.SEARCH_PREFERENCE_LOCAL).execute().actionGet(fessConfig.getIndexScrollSearchTimeout());

        int count = 0;
        String scrollId = response.getScrollId();
        try {
            while (scrollId != null) {
                final SearchHits searchHits = response.getHits();
                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(index).setId(hit.getId()));
                    count++;
                }
                final BulkResponse bulkResponse = bulkRequest.execute().actionGet(fessConfig.getIndexBulkTimeout());
                if (bulkResponse.hasFailures()) {
                    throw new IllegalBehaviorStateException(bulkResponse.buildFailureMessage());
                }

                response =
                        client.prepareSearchScroll(scrollId).setScroll(scrollForDelete).execute()
                                .actionGet(fessConfig.getIndexBulkTimeout());
                if (!scrollId.equals(response.getScrollId())) {
                    deleteScrollContext(scrollId);
                }
                scrollId = response.getScrollId();
            }
        } finally {
            deleteScrollContext(scrollId);
        }
        return count;
    }
 
Example 17
Source File: ElasticsearchDataModel.java    From elasticsearch-taste with Apache License 2.0 4 votes vote down vote up
@Override
public PreferenceArray getPreferencesFromUser(final long userID) {
    if (cache != null) {
        final DmValue dmValue = cache.getIfPresent(DmKey.key(
                DmKey.PREFERENCES_FROM_USER, userID));
        if (dmValue != null) {
            return dmValue.getValue();
        }
    }

    final SearchResponse response = getPreferenceSearchResponse(
            userIdField, userID, itemIdField, valueField);

    long totalHits = response.getHits().getTotalHits();
    if (totalHits > maxPreferenceSize) {
        logger.warn("UserID {} has {} items over {}.", userID, totalHits,
                maxPreferenceSize);
        totalHits = maxPreferenceSize;
    }

    long oldId = -1;
    final int size = (int) totalHits;
    final List<Preference> prefList = new ArrayList<>(size);
    for (final SearchHit hit : response.getHits()) {
        final long itemID = getLongValue(hit, itemIdField);
        if (itemID != oldId && existsItemID(itemID)) {
            final float value = getFloatValue(hit, valueField);
            prefList.add(new GenericPreference(userID, itemID, value));
            oldId = itemID;
        }
    }

    final PreferenceArray preferenceArray = new GenericUserPreferenceArray(
            prefList);

    if (cache != null) {
        cache.put(DmKey.create(DmKey.PREFERENCES_FROM_USER, userID),
                new DmValue(preferenceArray, size * 4 * 8 + 100));
    }
    return preferenceArray;
}
 
Example 18
Source File: ElasticsearchDataModel.java    From elasticsearch-taste with Apache License 2.0 4 votes vote down vote up
protected synchronized void loadItemIDs() {
    if (itemIDs != null) {
        return;
    }

    SearchResponse response = null;
    int size = 0;
    long[] ids = new long[size];
    int index = 0;
    try {
        while (true) {
            if (response == null) {
                response = client.prepareSearch(itemIndex)
                        .setTypes(itemType)
                        .setScroll(scrollKeepAlive)
                        .setQuery(QueryBuilders.boolQuery()
                                .must(itemQueryBuilder)
                                .filter(getLastAccessedFilterQuery()))
                        .addFields(itemIdField).setSize(scrollSize)
                        .execute().actionGet();
                long totalHits = response.getHits().getTotalHits();
                if (totalHits > Integer.MAX_VALUE) {
                    logger.warn("The number of items is {} > {}.",
                            totalHits, Integer.MAX_VALUE);
                    totalHits = Integer.MAX_VALUE;
                }
                size = (int) totalHits;
                ids = new long[size];
            } else {
                response = client
                        .prepareSearchScroll(response.getScrollId())
                        .setScroll(scrollKeepAlive).execute().actionGet();
            }
            if (response.getHits().getHits().length == 0) {
                break;
            }
            for (final SearchHit hit : response.getHits()) {
                ids[index] = getLongValue(hit, itemIdField);
                index++;
            }
        }
    } catch (final ElasticsearchException e) {
        throw new TasteException("Failed to scroll the result by itemIDs.",
                e);
    }

    if (index != size) {
        throw new TasteException("The total size " + size
                + " and the result " + index + " are not matched");
    }
    Arrays.sort(ids);
    itemIDs = ids;
}
 
Example 19
Source File: SearchApiMain.java    From elasticsearch-pool with Apache License 2.0 4 votes vote down vote up
public static void searchApi() throws IOException {

        RestHighLevelClient client = HighLevelClient.getInstance();

        try {
            SearchRequest searchRequest = new SearchRequest("jingma2_test");//限定index
            searchRequest.types("testlog");//限定type

            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            /*查询所有记录*/

//        searchSourceBuilder.query(QueryBuilders.matchAllQuery());


            /*根据匹配查询*/
            QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name", "风雷");
            /*设置中文分词器*/
//            ((MatchQueryBuilder) matchQueryBuilder).analyzer("ik");
//            ((MatchQueryBuilder) matchQueryBuilder).analyzer("ik_max_word");
//            ((MatchQueryBuilder) matchQueryBuilder).analyzer("ik_smart");
//            ((MatchQueryBuilder) matchQueryBuilder).analyzer("standard");
            searchSourceBuilder.query(matchQueryBuilder);

            /*限定查询条件和查询条数*/
//            searchSourceBuilder.query(QueryBuilders.termQuery("name", "风雷"));
            searchSourceBuilder.from(0);
            searchSourceBuilder.size(5);
//            searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));

            /*限定查询结果排序*/
//            searchSourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC));
//            searchSourceBuilder.sort(new FieldSortBuilder("age").order(SortOrder.ASC));

            searchRequest.source(searchSourceBuilder);

            SearchResponse searchResponse = client.search(searchRequest);
            System.out.println(searchResponse);

            SearchHits hits = searchResponse.getHits();
            long totalHits = hits.getTotalHits();
            float maxScore = hits.getMaxScore();

            SearchHit[] searchHits = hits.getHits();
            for (SearchHit hit : searchHits) {
                String index = hit.getIndex();
                String type = hit.getType();
                String id = hit.getId();
                float score = hit.getScore();
                String sourceAsString = hit.getSourceAsString();
                System.out.println(sourceAsString);
//                Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            }

        }finally {
            HighLevelClient.close();
        }

    }
 
Example 20
Source File: AbstractIMAPRiverUnitTest.java    From elasticsearch-imap with Apache License 2.0 3 votes vote down vote up
protected SearchHit statusRiver(final String index) throws ElasticsearchException, IOException {
    SearchResponse res = esSetup.client().prepareSearch(index).setTypes("imapriverstate").execute().actionGet();
    if(res.getHits() != null && res.getHits().getHits() != null && res.getHits().getHits().length > 0) {
        return res.getHits().getHits()[0];
    }
    
    return null;
    
    
}