org.elasticsearch.action.search.SearchResponse Java Examples

The following examples show how to use org.elasticsearch.action.search.SearchResponse. 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: InternalEsClient.java    From io with Apache License 2.0 6 votes vote down vote up
/**
 * 非同期でドキュメントを検索.
 * @param index インデックス名
 * @param type タイプ名
 * @param routingId routingId
 * @param query クエリ情報
 * @return 非同期応答
 */
public ActionFuture<SearchResponse> asyncSearch(
        String index,
        String type,
        String routingId,
        Map<String, Object> query) {
    SearchRequest req = new SearchRequest(index).types(type).searchType(SearchType.DEFAULT);
    if (query != null) {
        req.source(query);
    }
    if (routingFlag) {
        req = req.routing(routingId);
    }
    ActionFuture<SearchResponse> ret = esTransportClient.search(req);
    this.fireEvent(Event.afterRequest, index, type, null, JSONObject.toJSONString(query), "Search");
    return ret;
}
 
Example #2
Source File: EsUtil.java    From bookmark with MIT License 6 votes vote down vote up
/**
 * Description: 搜索
 *
 * @param index   index
 * @param builder 查询参数
 * @param c       结果类对象
 * @return java.util.ArrayList
 * @author fanxb
 * @date 2019/7/25 13:46
 */
public <T> List<T> search(String index, SearchSourceBuilder builder, Class<T> c) {
    if (!status) {
        return null;
    }
    SearchRequest request = new SearchRequest(index);
    request.source(builder);
    try {
        SearchResponse response = client.search(request, RequestOptions.DEFAULT);
        SearchHit[] hits = response.getHits().getHits();
        List<T> res = new ArrayList<>(hits.length);
        for (SearchHit hit : hits) {
            res.add(JSON.parseObject(hit.getSourceAsString(), c));
        }
        return res;
    } catch (Exception e) {
        throw new EsException(e);
    }
}
 
Example #3
Source File: SiteElasticSearchIndexBuilder.java    From sakai with Educational Community License v2.0 6 votes vote down vote up
/**
 * Get all indexed resources for a site
 *
 * @param siteId Site containing indexed resources
 * @return a collection of resource references or an empty collection if no resource was found
 */
protected Collection<String> getResourceNames(String siteId) {
    getLog().debug("Obtaining indexed elements for site: '" + siteId + "'");

    SearchResponse response = client.prepareSearch(indexName)
            .setSearchType(SearchType.QUERY_THEN_FETCH)
            .setQuery(termQuery(SearchService.FIELD_SITEID, siteId))
            .setTypes(indexedDocumentType)
            .setSize(Integer.MAX_VALUE)
            .addFields(SearchService.FIELD_REFERENCE)
            .execute()
            .actionGet();

    Collection<String> resourceNames = new ArrayList<String>();
    for (SearchHit hit : response.getHits().hits()) {
        resourceNames.add(getFieldFromSearchHit(SearchService.FIELD_REFERENCE, hit));
    }
    return resourceNames;

}
 
Example #4
Source File: MatchPhrasePrefixQueryDemo.java    From elasticsearch-full with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws Exception {
    String key = "this is a";
    MatchPhrasePrefixQueryBuilder matchPhrasePrefixQueryBuilder = QueryBuilders.matchPhrasePrefixQuery("title",key);

    matchPhrasePrefixQueryBuilder.boost(10);
    matchPhrasePrefixQueryBuilder.analyzer("standard");
    matchPhrasePrefixQueryBuilder.slop(2);
    matchPhrasePrefixQueryBuilder.maxExpansions(100);

    SearchResponse searchResponse = client.prepareSearch()
            .setIndices("my_index")
            .setTypes("my_type")
            .setQuery(matchPhrasePrefixQueryBuilder)
            .execute()
            .actionGet();
}
 
Example #5
Source File: MetadataBackendKV.java    From heroic with Apache License 2.0 6 votes vote down vote up
@Override
public AsyncFuture<CountSeries> countSeries(final CountSeries.Request filter) {
    return doto(c -> {
        final OptionalLimit limit = filter.getLimit();

        if (limit.isZero()) {
            return async.resolved(new CountSeries());
        }

        final QueryBuilder f = filter(filter.getFilter());

        SearchRequest request = c.getIndex().count(METADATA_TYPE);
        SearchSourceBuilder sourceBuilder = request.source();
        limit.asInteger().ifPresent(sourceBuilder::terminateAfter);
        sourceBuilder.query(new BoolQueryBuilder().must(f));

        final ResolvableFuture<SearchResponse> future = async.future();
        c.execute(request, bind(future));

        return future.directTransform(
            r -> new CountSeries(r.getHits().getTotalHits().value, false));

    });
}
 
Example #6
Source File: UITemplateManagementEsDAO.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@Override
public List<DashboardConfiguration> getAllTemplates(final Boolean includingDisabled) throws IOException {
    SearchSourceBuilder sourceBuilder = SearchSourceBuilder.searchSource();

    BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
    if (!includingDisabled) {
        boolQueryBuilder.must().add(QueryBuilders.termQuery(UITemplate.DISABLED, BooleanUtils.booleanToValue(includingDisabled)));
    }

    sourceBuilder.query(boolQueryBuilder);
    //It is impossible we have 10000+ templates.
    sourceBuilder.size(10000);

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

    List<DashboardConfiguration> configs = new ArrayList<>();
    final UITemplate.Builder builder = new UITemplate.Builder();
    for (SearchHit searchHit : response.getHits()) {
        Map<String, Object> sourceAsMap = searchHit.getSourceAsMap();

        final UITemplate uiTemplate = builder.map2Data(sourceAsMap);
        configs.add(new DashboardConfiguration().fromEntity(uiTemplate));
    }
    return configs;
}
 
Example #7
Source File: SearchES.java    From Transwarp-Sample-Code with MIT License 6 votes vote down vote up
/**
 * 多字段查询
 */
public static void multisearch() {
    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","clients");
        SearchResponse searchResponse = searchRequestBuilder.
                setQuery(QueryBuilders.boolQuery()
                        .should(QueryBuilders.termQuery("id","5"))
                        .should(QueryBuilders.prefixQuery("content","oracle")))
                .setFrom(0).setSize(100).setExplain(true).execute().actionGet();
        SearchHits searchHits = searchResponse.getHits();
        System.out.println();
        System.out.println("Total Hits is " + searchHits.totalHits());
        System.out.println();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #8
Source File: ElasticsearchIndex.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * Evaluates the given query and returns the results as a TopDocs instance.
 */
public SearchHits search(SearchRequestBuilder request, QueryBuilder query) {
	String[] types = getTypes();
	int nDocs;
	if (maxDocs > 0) {
		nDocs = maxDocs;
	} else {
		long docCount = client.prepareSearch(indexName)
				.setTypes(types)
				.setSource(new SearchSourceBuilder().size(0).query(query))
				.get()
				.getHits()
				.getTotalHits();
		nDocs = Math.max((int) Math.min(docCount, Integer.MAX_VALUE), 1);
	}
	SearchResponse response = request.setIndices(indexName)
			.setTypes(types)
			.setVersion(true)
			.setQuery(query)
			.setSize(nDocs)
			.execute()
			.actionGet();
	return response.getHits();
}
 
Example #9
Source File: MongoElasticsearchSyncIT.java    From streams with Apache License 2.0 6 votes vote down vote up
@Test
public void testSync() throws Exception {

  MongoElasticsearchSync sync = new MongoElasticsearchSync(testConfiguration);

  sync.run();

  IndicesExistsRequest indicesExistsRequest = Requests.indicesExistsRequest(testConfiguration.getDestination().getIndex());
  IndicesExistsResponse indicesExistsResponse = testClient.admin().indices().exists(indicesExistsRequest).actionGet();
  assertTrue(indicesExistsResponse.isExists());

  // assert lines in file
  SearchRequestBuilder countRequest = testClient
      .prepareSearch(testConfiguration.getDestination().getIndex())
      .setTypes(testConfiguration.getDestination().getType());
  SearchResponse countResponse = countRequest.execute().actionGet();

  assertEquals((int)countResponse.getHits().getTotalHits(), 89);

}
 
Example #10
Source File: ElasticsearchRequestSubmitterTest.java    From metron with Apache License 2.0 6 votes vote down vote up
@Test
public void searchShouldFailWhenNotOK() throws IOException {

  // mocks
  SearchResponse response = mock(SearchResponse.class);
  SearchRequest request = new SearchRequest();

  // response will have status of OK
  when(response.status()).thenReturn(RestStatus.PARTIAL_CONTENT);
  when(response.getFailedShards()).thenReturn(0);
  when(response.getTotalShards()).thenReturn(2);

  // search should succeed
  ElasticsearchRequestSubmitter submitter = setup(response);
  assertThrows(InvalidSearchException.class, () -> submitter.submitSearch(request));
}
 
Example #11
Source File: RunDao.java    From usergrid with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a map of all Runs with queried commitId, runNumber and testName.
 * <p>
 * <ul>
 *     <li>Key of the map is Run's id in elastic search</li>
 *     <li>Value of the map is Run itself</li>
 * </ul>
 *
 * @param commitId    commit id of the Run
 * @param runNumber   Run number to filter queried Runs
 * @param testName    Test class name that resulting Run is about
 * @return            Map satisfying given parameters. The map is empty if there are no Runs.
 */
public Map<String, Run> getMap( String commitId, int runNumber, String testName ) {

    BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery()
            .must( termQuery( "commitId", commitId.toLowerCase() ) )
            .must( termQuery( "runNumber", runNumber ) )
            .must( termQuery( "testName", testName.toLowerCase() ) );

    SearchResponse response = getRequest( DAO_INDEX_KEY, DAO_TYPE_KEY )
            .setQuery( queryBuilder )
            .setSize( MAX_RESULT_SIZE )
            .execute()
            .actionGet();

    HashMap<String, Run> runs = new HashMap<String, Run>();

    for ( SearchHit hit : response.getHits().hits() ) {
        runs.put( hit.getId(), toRun( hit ) );
    }

    return runs;
}
 
Example #12
Source File: CommonWebpageDAO.java    From Gather-Platform with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 获取query的关联信息
 *
 * @param query 查询queryString
 * @param size  结果集数量
 * @return 相关信息
 */
public Pair<Map<String, List<Terms.Bucket>>, List<Webpage>> relatedInfo(String query, int size) {
    SearchRequestBuilder searchRequestBuilder = client.prepareSearch(INDEX_NAME)
            .setTypes(TYPE_NAME)
            .setQuery(QueryBuilders.queryStringQuery(query))
            .addSort("gatherTime", SortOrder.DESC)
            .addAggregation(AggregationBuilders.terms("relatedPeople").field("namedEntity.nr"))
            .addAggregation(AggregationBuilders.terms("relatedLocation").field("namedEntity.ns"))
            .addAggregation(AggregationBuilders.terms("relatedInstitution").field("namedEntity.nt"))
            .addAggregation(AggregationBuilders.terms("relatedKeywords").field("keywords"))
            .setSize(size);
    SearchResponse response = searchRequestBuilder.execute().actionGet();
    Map<String, List<Terms.Bucket>> info = Maps.newHashMap();
    info.put("relatedPeople", ((Terms) response.getAggregations().get("relatedPeople")).getBuckets());
    info.put("relatedLocation", ((Terms) response.getAggregations().get("relatedLocation")).getBuckets());
    info.put("relatedInstitution", ((Terms) response.getAggregations().get("relatedInstitution")).getBuckets());
    info.put("relatedKeywords", ((Terms) response.getAggregations().get("relatedKeywords")).getBuckets());
    return Pair.of(info, warpHits2List(response.getHits()));
}
 
Example #13
Source File: ResultUtils.java    From vind with Apache License 2.0 6 votes vote down vote up
public static HashMap<FieldDescriptor, TermFacetResult<?>> buildSuggestionResults(SearchResponse response, DocumentFactory factory, String context) {

        if(Objects.nonNull(response)
                && Objects.nonNull(response.getHits())
                && Objects.nonNull(response.getHits().getHits())){

            //TODO: if nested doc search is implemented

            final HashMap<FieldDescriptor, TermFacetResult<?>> suggestionValues = new HashMap<>();

            response.getAggregations().asList().stream()
                    .map(aggregation -> getTermFacetResults(aggregation, new Facet.TermFacet(factory.getField(aggregation.getName())), factory))
                    .filter(pair -> CollectionUtils.isNotEmpty(pair.getValue().getValues()))
                    .forEach(pair -> suggestionValues.put(pair.getKey(), pair.getValue()));


            return suggestionValues;
        } else {
            throw new ElasticsearchException("Empty result from ElasticClient");
        }
    }
 
Example #14
Source File: ESIndex.java    From pyramid with Apache License 2.0 6 votes vote down vote up
/**
     * phrase query
     * use whitespace analyzer in the query
     * @param field
     * @param phrase already stemmed
     * @param ids
     * @param slop
     * @return
     */
    public SearchResponse matchPhrase(String field, String phrase,
                                      String[] ids, int slop) {
        IdsQueryBuilder idsFilterBuilder = new IdsQueryBuilder(documentType);
        idsFilterBuilder.addIds(ids);
        
        SearchResponse response = client.prepareSearch(indexName).setSize(ids.length)
        		.setTrackScores(false)
                .setFetchSource(false).setExplain(false).setFetchSource(false).
                setQuery(QueryBuilders.boolQuery().must(QueryBuilders.matchPhraseQuery(field, phrase)
                                .slop(slop).analyzer("whitespace")).filter(idsFilterBuilder))
                .execute().actionGet();


//        debug
//        XContentBuilder builder = XContentFactory.jsonBuilder();
//        builder.startObject();
//        System.out.println(response.toXContent(builder, ToXContent.EMPTY_PARAMS));
//        builder.endObject();
//        System.out.println(builder.string());

        return response;
    }
 
Example #15
Source File: EsAbstractBehavior.java    From fess with Apache License 2.0 5 votes vote down vote up
protected SearchHits getSearchHits(final SearchResponse response) {
    SearchHits hits = response.getHits();
    if (hits == null) {
        throw new IllegalBehaviorStateException("hits is null: " + response);
    }
    return hits;
}
 
Example #16
Source File: RestCountAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
private Table buildTable(RestRequest request, SearchResponse response) {
    Table table = getTableWithHeader(request);
    long time = System.currentTimeMillis();
    table.startRow();
    table.addCell(TimeUnit.SECONDS.convert(time, TimeUnit.MILLISECONDS));
    table.addCell(dateFormat.print(time));
    table.addCell(response.getHits().totalHits());
    table.endRow();

    return table;
}
 
Example #17
Source File: TestESResponse.java    From blue-marlin with Apache License 2.0 5 votes vote down vote up
@Test
/**
 * Coverage purpose only. Data not verified.
 */
public void getUpdateResponse() {
    SearchResponse srchResp = new SearchResponse(DEF_RESSEC, "id1", 1, 1, 1, 1, DEF_SHFAIL, DEF_CLUSTER);
    ESResponse esResp = new ESResponse(srchResp, Collections.emptyMap());
    UpdateResponse res = esResp.getUpdateResponse();
    assertNull(res);
}
 
Example #18
Source File: ES7xAdapter.java    From canal with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, Object> count(String task) {
    ESSyncConfig config = esSyncConfig.get(task);
    ESSyncConfig.ESMapping mapping = config.getEsMapping();
    SearchResponse response = this.esConnection.new ESSearchRequest(mapping.get_index()).size(0).getResponse();

    long rowCount = response.getHits().getTotalHits().value;
    Map<String, Object> res = new LinkedHashMap<>();
    res.put("esIndex", mapping.get_index());
    res.put("count", rowCount);
    return res;
}
 
Example #19
Source File: DataServiceImpl.java    From java-11-examples with Apache License 2.0 5 votes vote down vote up
@Override
public Collection<EventData> getData(SearchSourceBuilder searchSourceBuilder) throws IOException {
    SearchRequest searchRequest = new SearchRequest(INDEX_NAME);
    searchRequest.source(searchSourceBuilder);
    SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
    searchResponse.getHits();
    List<EventData> results = new ArrayList<>();
    Iterator<SearchHit> iterator = searchResponse.getHits().iterator();
    while (iterator.hasNext()) {
        SearchHit hit = iterator.next();
        EventData eventData = Utils.createFromSource(hit.getId(), hit.getSourceAsMap());
        results.add(eventData);
    }
    return results;
}
 
Example #20
Source File: KibanaUtilsTest.java    From openshift-elasticsearch-plugin with Apache License 2.0 5 votes vote down vote up
public static void givenSearchResultForDocuments(PluginClient client, String indexPattern, Map<String, BytesReference> docs) {
    List<SearchHit> hits = new ArrayList<>(docs.size());
    for (Map.Entry<String, BytesReference> entry : docs.entrySet()) {
        SearchHit hit = new SearchHit(1, entry.getKey(), null, null);
        hit.sourceRef(entry.getValue());
        hits.add(hit);
    }
    SearchHits searchHits = new SearchHits(hits.toArray(new SearchHit[hits.size()]), hits.size(), 1.0f);
    SearchResponseSections sections = new SearchResponseSections(searchHits, null, null, false, Boolean.FALSE, null,
            0);
    ShardSearchFailure[] failures = null;
    SearchResponse response = new SearchResponse(sections, "", 0, 0, 0, 0L, failures);

    when(client.search(anyString(), anyString(),anyInt(), anyBoolean())).thenReturn(response);
}
 
Example #21
Source File: Search.java    From elasticsearch-rest-command with The Unlicense 5 votes vote down vote up
public static void buildTimeline(XContentBuilder builder,
		SearchResponse response, ESLogger logger) throws IOException {
	logger.info("Report took in millseconds:" + response.getTookInMillis());
	DateHistogram timeline = response.getAggregations().get(
			"data_over_time");

	// 格式化结果后输出

	builder.startObject();
	builder.field("took", response.getTookInMillis());
	builder.field("total", timeline.getBuckets().size());

	builder.startArray("fields");
	builder.value("_bucket_timevalue");
	builder.value("_doc_count");
	builder.endArray();

	builder.startArray("rows");
	for (Bucket bucket : timeline.getBuckets()) {
		builder.startArray();
		builder.value(bucket.getKey());
		builder.value(bucket.getDocCount());
		builder.endArray();
	}
	builder.endArray().endObject();

}
 
Example #22
Source File: ElasticsearchRequestSubmitterTest.java    From metron with Apache License 2.0 5 votes vote down vote up
@Test
public void searchShouldHandleShardFailure() throws InvalidSearchException, IOException {
  // mocks
  SearchResponse response = mock(SearchResponse.class);
  SearchRequest request = new SearchRequest();
  ShardSearchFailure fail = mock(ShardSearchFailure.class);
  SearchShardTarget target = new SearchShardTarget("node1", mock(Index.class), 1, "metron");

  // response will have status of OK
  when(response.status()).thenReturn(RestStatus.OK);

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

  // the response will report shard failures
  when(response.getFailedShards()).thenReturn(1);
  when(response.getTotalShards()).thenReturn(2);
  when(response.getHits()).thenReturn(hits);

  // the response will return the failures
  ShardSearchFailure[] failures = { fail };
  when(response.getShardFailures()).thenReturn(failures);

  // shard failure needs to report the node
  when(fail.shard()).thenReturn(target);

  // shard failure needs to report details of failure
  when(fail.index()).thenReturn("bro_index_2017-10-11");
  when(fail.shardId()).thenReturn(1);

  // search should succeed, even with failed shards
  ElasticsearchRequestSubmitter submitter = setup(response);
  SearchResponse actual = submitter.submitSearch(request);
  assertNotNull(actual);
}
 
Example #23
Source File: ElasticsearchSearchQueryBase.java    From vertexium with Apache License 2.0 5 votes vote down vote up
private QueryInfiniteScrollIterable<SearchHit> searchScrollHits(EnumSet<VertexiumObjectType> objectTypes, FetchHints fetchHints) {
    return new QueryInfiniteScrollIterable<SearchHit>(objectTypes, fetchHints, getParameters().getLimit()) {
        @Override
        protected ElasticsearchGraphQueryIterable<SearchHit> searchResponseToIterable(SearchResponse searchResponse) {
            return ElasticsearchSearchQueryBase.this.searchResponseToSearchHitsIterable(searchResponse);
        }

        @Override
        protected IdStrategy getIdStrategy() {
            return getSearchIndex().getIdStrategy();
        }
    };
}
 
Example #24
Source File: ElasticDocumentSearch.java    From BioSolr with Apache License 2.0 5 votes vote down vote up
private void lookupAnnotationFields(Map<String, Document> idMap) {
	QueryBuilder qb = QueryBuilders.idsQuery(getDocumentType()).addIds(idMap.keySet());
	SearchRequestBuilder srb = getClient().prepareSearch(getIndexName())
			.addFields("*")
			.setQuery(qb)
			.setSize(idMap.size());
	LOGGER.debug("Annotation field lookup query: {}", srb.toString());

	SearchResponse response = srb.execute().actionGet();
	for (SearchHit hit : response.getHits().getHits()) {
		populateAnnotationFields(hit, idMap.get(hit.getId()));
	}
}
 
Example #25
Source File: EsStore.java    From soundwave with Apache License 2.0 5 votes vote down vote up
protected <E extends EsDocument> ScrollableResponse<List<E>> retrieveAll(
    String[] includeFields, int size, ThrowingFunction<String, E> createFunc)
    throws Exception {

  Preconditions.checkArgument(size > 0);

  SearchRequestBuilder builder = esClient.prepareSearch()
      .setIndices(getIndexName()).setTypes(getDocTypeName())
      .setScroll(new TimeValue(SCROLLDEFAULTTIMEOUT))
      .setSize(size)
      .setFetchSource(includeFields, null).setVersion(true);

  SearchResponse response = builder.execute().actionGet();
  return convertToScrollableResponse(response, createFunc);
}
 
Example #26
Source File: Application.java    From htwplus with MIT License 5 votes vote down vote up
@Security.Authenticated(Secured.class)
public Result searchSuggestions(String query) throws ExecutionException, InterruptedException {
    Account currentAccount = Component.currentAccount();
    if (currentAccount == null) {
        return forbidden();
    }
    SearchResponse response = elasticsearchService.doSearch("searchSuggestions", query, "all", null, 1, currentAccount.id.toString(), asList("name", "title", "filename"), asList("friends", "member", "viewable"));
    return ok(response.toString());
}
 
Example #27
Source File: ElasticsearchTemplate.java    From summerframework with Apache License 2.0 5 votes vote down vote up
public List<Map<String, Object>> highLightResultSet(ESBasicInfo esBasicInfo, QueryCondition queryCondition,
    HighLight highLight) {
    SearchResponse response = esClient.prepareSearch(esBasicInfo.getIndex()).setTypes(esBasicInfo.getType())
        .setSearchType(queryCondition.getSearchType()).setScroll(new TimeValue(queryCondition.getMillis()))
        .setQuery(queryCondition.getQueryBuilder()).setSize(queryCondition.getSize())
        .highlighter(highLight.getBuilder()).execute().actionGet();

    String highlightField = highLight.getField();
    List<Map<String, Object>> sourceList = new ArrayList<>();

    for (SearchHit searchHit : response.getHits()) {
        Map<String, Object> element = new HashMap<>();
        StringBuilder stringBuilder = new StringBuilder();
        if (StringUtils.isNotEmpty(highlightField)) {
            Text[] text = searchHit.getHighlightFields().get(highlightField).getFragments();

            if (text != null) {
                for (Text str : text) {
                    stringBuilder.append(str.string());
                }

                log.info("遍历 高亮结果集{}", stringBuilder.toString());
                element.put(highlightField, stringBuilder.toString());
            }
        }
        sourceList.add(element);
    }

    return sourceList;
}
 
Example #28
Source File: ElasticsearchTemplate.java    From summerframework with Apache License 2.0 5 votes vote down vote up
public <T> List<T> analyzeSearchResponse(Class<T> clazz, SearchResponse response) throws IOException {
    SearchHits searchHits = response.getHits();

    List<T> result = new ArrayList<>();
    for (SearchHit hit : searchHits) {
        result.add(mapper.readValue(hit.getSourceAsString(), clazz));
    }
    return result;
}
 
Example #29
Source File: ES6xTemplate.java    From canal with Apache License 2.0 5 votes vote down vote up
@Override
public void insert(ESSyncConfig.ESMapping mapping, Object pkVal, Map<String, Object> esFieldData) {
    if (mapping.get_id() != null) {
        String parentVal = (String) esFieldData.remove("$parent_routing");
        if (mapping.isUpsert()) {
            ESUpdateRequest updateRequest = esConnection.new ES6xUpdateRequest(mapping.get_index(),
                mapping.get_type(),
                pkVal.toString()).setDoc(esFieldData).setDocAsUpsert(true);
            if (StringUtils.isNotEmpty(parentVal)) {
                updateRequest.setRouting(parentVal);
            }
            getBulk().add(updateRequest);
        } else {
            ESIndexRequest indexRequest = esConnection.new ES6xIndexRequest(mapping.get_index(),
                mapping.get_type(),
                pkVal.toString()).setSource(esFieldData);
            if (StringUtils.isNotEmpty(parentVal)) {
                indexRequest.setRouting(parentVal);
            }
            getBulk().add(indexRequest);
        }
        commitBulk();
    } else {
        ESSearchRequest esSearchRequest = this.esConnection.new ESSearchRequest(mapping.get_index(),
            mapping.get_type()).setQuery(QueryBuilders.termQuery(mapping.getPk(), pkVal)).size(10000);
        SearchResponse response = esSearchRequest.getResponse();

        for (SearchHit hit : response.getHits()) {
            ESUpdateRequest esUpdateRequest = this.esConnection.new ES6xUpdateRequest(mapping.get_index(),
                mapping.get_type(),
                hit.getId()).setDoc(esFieldData);
            getBulk().add(esUpdateRequest);
            commitBulk();
        }
    }
}
 
Example #30
Source File: ElasticSearchTargetIT.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Test
public void testWriteRecordsOnErrorDiscard() throws Exception {
  Target target = createTarget();
  TargetRunner runner = new TargetRunner.Builder(ElasticSearchDTarget.class, target).setOnRecordError(OnRecordError.DISCARD).build();
  try {
    runner.runInit();
    List<Record> records = new ArrayList<>();
    Record record1 = RecordCreator.create();
    record1.set(Field.create(ImmutableMap.of("a", Field.create("Hello"),
                                             "index", Field.create("II"), "type", Field.create("t"))));
    Record record2 = RecordCreator.create();
    record2.set(Field.create(ImmutableMap.of("a", Field.create("Bye"),
                                             "index", Field.create("ii"), "type", Field.create("t"))));
    records.add(record1);
    records.add(record2);
    runner.runWrite(records);
    Assert.assertTrue(runner.getErrorRecords().isEmpty());
    Assert.assertTrue(runner.getErrors().isEmpty());


    prepareElasticSearchServerForQueries();

    Set<Map> expected = new HashSet<>();
    expected.add(ImmutableMap.of("a", "Bye", "index", "ii", "type", "t"));

    SearchResponse response = esServer.client().prepareSearch("ii").setTypes("t")
                                      .setSearchType(SearchType.DEFAULT).execute().actionGet();
    SearchHit[] hits = response.getHits().getHits();
    Assert.assertEquals(1, hits.length);
    Set<Map> got = new HashSet<>();
    got.add(hits[0].getSource());

    Assert.assertEquals(expected, got);

  } finally {
    runner.runDestroy();
  }
}