Java Code Examples for org.elasticsearch.action.search.SearchRequestBuilder#get()

The following examples show how to use org.elasticsearch.action.search.SearchRequestBuilder#get() . 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: ElasticsearchTransportFactory.java    From database-transform-tool with Apache License 2.0 6 votes vote down vote up
public String selectAll(String indexs,String types,String condition){
	try {
		if(client==null){
			init();
		}
		SearchRequestBuilder request = client.prepareSearch(indexs.split(",")).setTypes(types.split(","));
		request.setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
		request.setQuery(QueryBuilders.queryStringQuery(condition));
		request.setExplain(false);
		SearchResponse response = request.get();
		return response.toString();
	} catch (Exception e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	return null;
}
 
Example 2
Source File: TestElasticSearchDAOV5.java    From conductor with Apache License 2.0 6 votes vote down vote up
private SearchResponse search(String indexName, String structuredQuery, int start,
    int size, String freeTextQuery, String docType) throws ParserException {
    QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
    if (StringUtils.isNotEmpty(structuredQuery)) {
        Expression expression = Expression.fromString(structuredQuery);
        queryBuilder = expression.getFilterBuilder();
    }

    BoolQueryBuilder filterQuery = QueryBuilders.boolQuery().must(queryBuilder);
    QueryStringQueryBuilder stringQuery = QueryBuilders.queryStringQuery(freeTextQuery);
    BoolQueryBuilder fq = QueryBuilders.boolQuery().must(stringQuery).must(filterQuery);
    final SearchRequestBuilder srb = elasticSearchClient.prepareSearch(indexName)
        .setQuery(fq)
        .setTypes(docType)
        .storedFields("_id")
        .setFrom(start)
        .setSize(size);

    return srb.get();
}
 
Example 3
Source File: ElasticJoinExecutor.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
protected SearchResponse scrollOneTimeWithMax(Client client,TableInJoinRequestBuilder tableRequest) {
        SearchResponse responseWithHits;SearchRequestBuilder scrollRequest = tableRequest.getRequestBuilder()
                .setScroll(new TimeValue(60000))
                .setSize(MAX_RESULTS_ON_ONE_FETCH);
        boolean ordered = tableRequest.getOriginalSelect().isOrderdSelect();
        if(!ordered) scrollRequest.addSort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC);
        responseWithHits = scrollRequest.get();
        //on ordered select - not using SCAN , elastic returns hits on first scroll
        //es5.0 elastic always return docs on scan
//        if(!ordered)
//            responseWithHits = client.prepareSearchScroll(responseWithHits.getScrollId()).setScroll(new TimeValue(600000)).get();
        return responseWithHits;
    }
 
Example 4
Source File: ElasticUtils.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
public static SearchResponse scrollOneTimeWithHits(Client client, SearchRequestBuilder requestBuilder, Select originalSelect, int resultSize) {
        SearchResponse responseWithHits;SearchRequestBuilder scrollRequest = requestBuilder
                .setScroll(new TimeValue(60000))
                .setSize(resultSize);
        boolean ordered = originalSelect.isOrderdSelect();
        if(!ordered) scrollRequest.addSort(FieldSortBuilder.DOC_FIELD_NAME, SortOrder.ASC);
        responseWithHits = scrollRequest.get();
        //on ordered select - not using SCAN , elastic returns hits on first scroll
        //es5.0 elastic always return docs on scan
//        if(!ordered) {
//            responseWithHits = client.prepareSearchScroll(responseWithHits.getScrollId()).setScroll(new TimeValue(600000)).get();
//        }
        return responseWithHits;
    }
 
Example 5
Source File: HashJoinElasticExecutor.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
private List<SearchHit> fetchAllHits(TableInJoinRequestBuilder tableInJoinRequest) {
    Integer hintLimit = tableInJoinRequest.getHintLimit();
    SearchRequestBuilder requestBuilder = tableInJoinRequest.getRequestBuilder();
    if (hintLimit != null && hintLimit < MAX_RESULTS_ON_ONE_FETCH) {
        requestBuilder.setSize(hintLimit);
        SearchResponse searchResponse = requestBuilder.get();
        updateMetaSearchResults(searchResponse);
        return Arrays.asList(searchResponse.getHits().getHits());
    }
    return scrollTillLimit(tableInJoinRequest, hintLimit);
}
 
Example 6
Source File: ModelsAction.java    From zentity with Apache License 2.0 5 votes vote down vote up
/**
 * Retrieve all entity models.
 *
 * @param client The client that will communicate with Elasticsearch.
 * @return The response from Elasticsearch.
 * @throws ForbiddenException
 */
public static SearchResponse getEntityModels(NodeClient client) throws ForbiddenException {
    SearchRequestBuilder request = client.prepareSearch(INDEX_NAME);
    request.setSize(10000);
    try {
        return request.get();
    } catch (IndexNotFoundException e) {
        try {
            SetupAction.createIndex(client);
        } catch (ElasticsearchSecurityException se) {
            throw new ForbiddenException("The .zentity-models index does not exist and you do not have the 'create_index' privilege. An authorized user must create the index by submitting: POST _zentity/_setup");
        }
        return request.get();
    }
}
 
Example 7
Source File: StoredLtrQueryIT.java    From elasticsearch-learning-to-rank with Apache License 2.0 5 votes vote down vote up
public void testScriptFeatureUseCase() throws Exception {
    addElement(new StoredFeature("feature1", Collections.singletonList("query"), "mustache",
            QueryBuilders.matchQuery("field1", "{{query}}").toString()));
    addElement(new StoredFeature("feature6", Arrays.asList("query", "extra_multiplier_ltr"), ScriptFeature.TEMPLATE_LANGUAGE,
            "{\"lang\": \"native\", \"source\": \"feature_extractor\", \"params\": { \"dependent_feature\": \"feature1\"," +
                    " \"extra_script_params\" : {\"extra_multiplier_ltr\": \"extra_multiplier\"}}}"));
    AddFeaturesToSetRequestBuilder builder = new AddFeaturesToSetRequestBuilder(client());

    builder.request().setFeatureSet("my_set");
    builder.request().setFeatureNameQuery("feature1");
    builder.request().setStore(IndexFeatureStore.DEFAULT_STORE);
    builder.execute().get();
    builder.request().setFeatureNameQuery("feature6");
    long version = builder.get().getResponse().getVersion();

    CreateModelFromSetRequestBuilder createModelFromSetRequestBuilder = new CreateModelFromSetRequestBuilder(client());
    createModelFromSetRequestBuilder.withVersion(IndexFeatureStore.DEFAULT_STORE, "my_set", version,
            "my_model", new StoredLtrModel.LtrModelDefinition("model/linear", SIMPLE_SCRIPT_MODEL, true));
    createModelFromSetRequestBuilder.get();
    buildIndex();
    Map<String, Object> params = new HashMap<>();
    params.put("query", "hello");
    params.put("dependent_feature", new HashMap<>());
    params.put("extra_multiplier_ltr", 100.0d);
    SearchRequestBuilder sb = client().prepareSearch("test_index")
            .setQuery(QueryBuilders.matchQuery("field1", "world"))
            .setRescorer(new QueryRescorerBuilder(new WrapperQueryBuilder(new StoredLtrQueryBuilder(LtrTestUtils.nullLoader())
                    .modelName("my_model").params(params).toString()))
                    .setScoreMode(QueryRescoreMode.Total)
                    .setQueryWeight(0)
                    .setRescoreQueryWeight(1));

    SearchResponse sr = sb.get();
    assertEquals(1, sr.getHits().getTotalHits().value);
    assertThat(sr.getHits().getAt(0).getScore(), Matchers.greaterThanOrEqualTo(29.0f));
    assertThat(sr.getHits().getAt(0).getScore(), Matchers.lessThanOrEqualTo(30.0f));
}
 
Example 8
Source File: ElasticSearchController.java    From springboot-learn with MIT License 5 votes vote down vote up
@PostMapping("query/book/novel")
@ResponseBody
public ResponseEntity query(
        @RequestParam(name = "title", required = false) String title,
        @RequestParam(name = "author", required = false) String author,
        @RequestParam(name = "gtWordCount", defaultValue = "0") int gtWordCount,
        @RequestParam(name = "ltWordCount", required = false) Integer ltWordCount
) {
    BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
    if (author != null) {
        queryBuilder.must(QueryBuilders.matchQuery("author", author));
    }
    if (title != null) {
        queryBuilder.must(QueryBuilders.matchQuery("title", title));
    }
    // 大于
    RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("word_count").from(gtWordCount);
    // 小于
    if (ltWordCount != null && ltWordCount > 0) {
        rangeQueryBuilder.to(ltWordCount);
    }
    //过滤条件
    queryBuilder.filter(rangeQueryBuilder);
    SearchRequestBuilder builder = transportClient.prepareSearch(INDEX)
            .setTypes(TYPE)
            .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
            .setQuery(queryBuilder)
            .setFrom(0)
            .setSize(10);
    SearchResponse response = builder.get();
    List<Map<String, Object>> result = new ArrayList<>();
    //将命中的数据放入List
    for (SearchHit hit : response.getHits()) {
        result.add(hit.getSourceAsMap());
    }
    return new ResponseEntity(result, HttpStatus.OK);
}
 
Example 9
Source File: ESUtil.java    From ns4_gear_watchdog with Apache License 2.0 5 votes vote down vote up
public <T> List<T> queryDocumentByParam(String indices, String type, MonitorEventSearchParam param, Class<T> clazz) {
    SearchRequestBuilder builder = buildRequest(indices,type);
    builder.addSort("timeHappend", SortOrder.ASC);
    builder.setQuery(convertParam(param));
    builder.setFrom(0).setSize(10);
    SearchResponse resp = builder.get();
    return convertResponse(resp,clazz);
}
 
Example 10
Source File: ElasticSearchDAOV5.java    From conductor with Apache License 2.0 5 votes vote down vote up
private SearchResult<String> search(String indexName, String structuredQuery, int start, int size,
    List<String> sortOptions, String freeTextQuery, String docType) {
    try {
        QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
        if (StringUtils.isNotEmpty(structuredQuery)) {
            Expression expression = Expression.fromString(structuredQuery);
            queryBuilder = expression.getFilterBuilder();
        }

        BoolQueryBuilder filterQuery = QueryBuilders.boolQuery().must(queryBuilder);
        QueryStringQueryBuilder stringQuery = QueryBuilders.queryStringQuery(freeTextQuery);
        BoolQueryBuilder fq = QueryBuilders.boolQuery().must(stringQuery).must(filterQuery);
        final SearchRequestBuilder srb = elasticSearchClient.prepareSearch(indexName)
                .setQuery(fq)
                .setTypes(docType)
                .storedFields("_id")
                .setFrom(start)
                .setSize(size);

        if (sortOptions != null) {
            sortOptions.forEach(sortOption -> addSortOptionToSearchRequest(srb, sortOption));
        }

        SearchResponse response = srb.get();

        LinkedList<String> result = StreamSupport.stream(response.getHits().spliterator(), false)
                .map(SearchHit::getId)
                .collect(Collectors.toCollection(LinkedList::new));
        long count = response.getHits().getTotalHits();

        return new SearchResult<>(count, result);
    } catch (ParserException e) {
        String errorMsg = String.format("Error performing search on index:%s with docType:%s", indexName, docType);
        logger.error(errorMsg);
        throw new ApplicationException(Code.BACKEND_ERROR, errorMsg, e);
    }
}
 
Example 11
Source File: ThreadAnalysisQueryHandler.java    From uavstack with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("rawtypes")
private SearchResponse query(UAVHttpMessage data, QueryBuilder queryBuilder, QueryBuilder postFilter,
        List<SortBuilder> sorts) {

    String date = data.getRequest("indexdate");
    String currentIndex;
    if (date != null) {
        // 指定index
        currentIndex = this.indexMgr.getIndexByDate(date);
    }
    else {
        // current index
        currentIndex = this.indexMgr.getCurrentIndex();
    }

    SearchRequestBuilder srb = client.getClient().prepareSearch(currentIndex)
            .setTypes(ThreadAnalysisIndexMgr.JTA_TABLE).setSearchType(SearchType.DFS_QUERY_THEN_FETCH);

    int from = DataConvertHelper.toInt(data.getRequest("from"), -1);
    int size = DataConvertHelper.toInt(data.getRequest("size"), -1);

    if (from != -1 && size != -1) {
        srb = srb.setFrom(from).setSize(size);
    }

    srb.setQuery(queryBuilder);

    if (postFilter != null) {
        srb.setPostFilter(postFilter);
    }

    for (SortBuilder sb : sorts) {
        srb.addSort(sb);
    }

    SearchResponse sr = srb.get(TimeValue.timeValueMillis(timeout));

    return sr;
}
 
Example 12
Source File: SlowOperQueryHandler.java    From uavstack with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("rawtypes")
private SearchResponse query(UAVHttpMessage data, QueryBuilder queryBuilder, QueryBuilder postFilter,
        SortBuilder[] sorts) {

    String indexDate = data.getRequest("indexdate");
    String currentIndex;
    if (indexDate != null) {
        // 获得指定的index
        currentIndex = this.indexMgr.getIndexByDate(indexDate);
    }
    else {
        // get current index
        currentIndex = this.indexMgr.getCurrentIndex();
    }

    SearchRequestBuilder srb = client.getClient().prepareSearch(currentIndex).setTypes(buildTypes(data))
            .setSearchType(SearchType.DFS_QUERY_THEN_FETCH);

    int from = DataConvertHelper.toInt(data.getRequest("from"), -1);
    int size = DataConvertHelper.toInt(data.getRequest("size"), -1);

    if (from != -1 && size != -1) {
        srb = srb.setFrom(from).setSize(size);
    }

    srb.setQuery(queryBuilder);

    if (postFilter != null) {
        srb.setPostFilter(postFilter);
    }

    if (sorts != null && sorts.length > 0) {
        for (SortBuilder sb : sorts) {
            srb.addSort(sb);
        }
    }

    SearchResponse sr = srb.get(TimeValue.timeValueMillis(timeout));

    return sr;
}
 
Example 13
Source File: TransportBasedClient.java    From zeppelin with Apache License 2.0 4 votes vote down vote up
@Override
public ActionResponse search(String[] indices, String[] types, String query, int size) {
  final SearchRequestBuilder reqBuilder = new SearchRequestBuilder(
      client, SearchAction.INSTANCE);
  reqBuilder.setIndices();

  if (indices != null) {
    reqBuilder.setIndices(indices);
  }
  if (types != null) {
    reqBuilder.setTypes(types);
  }

  if (!StringUtils.isEmpty(query)) {
    // The query can be either JSON-formatted, nor a Lucene query
    // So, try to parse as a JSON => if there is an error, consider the query a Lucene one
    try {
      @SuppressWarnings("rawtypes")
      final Map source = gson.fromJson(query, Map.class);
      reqBuilder.setExtraSource(source);
    } catch (final JsonSyntaxException e) {
      // This is not a JSON (or maybe not well formatted...)
      reqBuilder.setQuery(QueryBuilders.queryStringQuery(query).analyzeWildcard(true));
    }
  }

  reqBuilder.setSize(size);

  final SearchResponse searchResp = reqBuilder.get();

  final ActionResponse actionResp = new ActionResponse()
      .succeeded(true)
      .totalHits(searchResp.getHits().getTotalHits());

  if (searchResp.getAggregations() != null) {
    setAggregations(searchResp.getAggregations(), actionResp);
  } else {
    for (final SearchHit hit: searchResp.getHits()) {
      // Fields can be found either in _source, or in fields (it depends on the query)
      // => specific for elasticsearch's version < 5
      //
      String src = hit.getSourceAsString();
      if (src == null) {
        final Map<String, Object> hitFields = new HashMap<>();
        for (final SearchHitField hitField : hit.getFields().values()) {
          hitFields.put(hitField.getName(), hitField.getValues());
        }
        src = gson.toJson(hitFields);
      }
      actionResp.addHit(new HitWrapper(hit.getIndex(), hit.getType(), hit.getId(), src));
    }
  }

  return actionResp;
}
 
Example 14
Source File: NewLogQueryHandler.java    From uavstack with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("rawtypes")
private SearchResponse query(UAVHttpMessage data, QueryBuilder queryBuilder, QueryBuilder postFilter,
        SortBuilder[] sorts) {

    String indexDate = data.getRequest("indexdate");
    String currentIndex;
    if (indexDate != null) {
        // 获得指定的index
        currentIndex = this.indexMgr.getIndexByDate(indexDate);
    }
    else {
        // get current index
        currentIndex = this.indexMgr.getCurrentIndex();
    }

    // get logtype for search
    SearchRequestBuilder srb = null;
    if (StringHelper.isEmpty(data.getRequest("logtype"))) {

        srb = client.getClient().prepareSearch(currentIndex).setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
    }
    else {
        String logType = data.getRequest("logtype").replace('.', '_');

        srb = client.getClient().prepareSearch(currentIndex).setTypes(logType)
                .setSearchType(SearchType.DFS_QUERY_THEN_FETCH);
    }

    int from = DataConvertHelper.toInt(data.getRequest("from"), -1);
    int size = DataConvertHelper.toInt(data.getRequest("size"), -1);

    if (from != -1 && size != -1) {
        long startLine = DataConvertHelper.toLong(data.getRequest("sline"), -1);
        long endLine = DataConvertHelper.toLong(data.getRequest("eline"), -1);
        // 判断如果只有endline则需要将结果倒序,取其前100行,然后在返回响应时再逆序回来,从而取到endline前的100行
        if (startLine == -1 && endLine > -1) {
            srb = srb.addSort("l_timestamp", SortOrder.DESC).addSort("l_num", SortOrder.DESC).setFrom(from)
                    .setSize(size);
        }
        else {
            srb = srb.setFrom(from).setSize(size);
        }
    }

    srb.setQuery(queryBuilder);

    if (postFilter != null) {
        srb.setPostFilter(postFilter);
    }

    if (sorts != null && sorts.length > 0) {
        for (SortBuilder sb : sorts) {
            srb.addSort(sb);
        }
    }

    SearchResponse sr = srb.get(TimeValue.timeValueMillis(timeout));

    return sr;
}
 
Example 15
Source File: InvokeChainQueryHandler.java    From uavstack with Apache License 2.0 4 votes vote down vote up
/**
 * 
 * @param data
 * @param queryBuilder
 * @param postFilter
 * @return
 */
@SuppressWarnings("rawtypes")
private SearchResponse query(UAVHttpMessage data, QueryBuilder queryBuilder, QueryBuilder postFilter,
        SortBuilder[] sorts) {

    String date = data.getRequest("indexdate");
    String currentIndex;
    if (date != null) {
        // 指定index
        currentIndex = this.indexMgr.getIndexByDate(date);
    }
    else {
        // current index
        currentIndex = this.indexMgr.getCurrentIndex();
    }

    SearchRequestBuilder srb = client.getClient().prepareSearch(currentIndex)
            .setTypes(InvokeChainIndexMgr.IVC_Table).setSearchType(SearchType.DFS_QUERY_THEN_FETCH);

    int from = DataConvertHelper.toInt(data.getRequest("from"), -1);
    int size = DataConvertHelper.toInt(data.getRequest("size"), -1);

    if (from != -1 && size != -1) {
        srb = srb.setFrom(from).setSize(size);
    }

    srb.setQuery(queryBuilder);

    if (postFilter != null) {
        srb.setPostFilter(postFilter);
    }

    if (sorts != null && sorts.length > 0) {
        for (SortBuilder sb : sorts) {
            srb.addSort(sb);
        }
    }

    SearchResponse sr = srb.get(TimeValue.timeValueMillis(timeout));

    return sr;
}
 
Example 16
Source File: HistoryEventsIndexingTest.java    From camunda-bpm-elasticsearch with Apache License 2.0 4 votes vote down vote up
@Test
  public void testIndexingSingleInvoice() throws IOException {
    HashMap<String,ProcessDataContainer> processesById = TestDataGenerator.startInvoiceProcess(processEngineRule.getProcessEngine(), 1);

    String[] pids = processesById.keySet().toArray(new String[0]);

    // elasticsearch //////////////////////////////

    flushAndRefresh();

    FilteredQueryBuilder query = QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), FilterBuilders.termFilter("processInstanceId", pids[0]));

    SearchRequestBuilder searchRequestBuilder = client.prepareSearch(ES_DEFAULT_INDEX_NAME_CAMUNDA_BPM)
        .setQuery(query);

    IoUtil.writeToFile(searchRequestBuilder.toString(), this.getClass().getSimpleName() + ".json", true);

    SearchResponse searchResponse = searchRequestBuilder.get();
    SearchHits hits = searchResponse.getHits();
    assertEquals(1, hits.totalHits());

    SearchHit hit = hits.getAt(0);
    assertEquals(pids[0], hit.getId());
    assertEquals(ES_DEFAULT_TYPE_NAME_CAMUNDA_BPM, hit.getType());

    Map<String,Object> source = hit.getSource();
    assertNotNull(source.get("startTime"));
    assertNotNull(source.get("endTime"));
    ArrayList variables = (ArrayList) source.get("variables");
    assertEquals(5, variables.size());
    ArrayList tasks = (ArrayList) source.get("tasks");
    assertEquals(9, tasks.size());
    ArrayList activities = (ArrayList) source.get("activities");
    assertEquals(19, activities.size());

    logger.info(searchResponse.toString());

//    for (SearchHit searchHit : searchResponse.getHits()) {
//      logger.info(searchHit.sourceAsString());
//    }
  }
 
Example 17
Source File: SearchServiceImpl.java    From emotional_analysis with Apache License 2.0 4 votes vote down vote up
@Override
public PageBean<Map<String,Object>> getSearchResult(int page, int rows,String content) throws Exception {

	PageBean<Map<String, Object>> searchListPageBean = new PageBean<Map<String, Object>>();
	BoolQueryBuilder boolBuilder = QueryBuilders.boolQuery();
	boolBuilder.must(QueryBuilders.matchQuery("content", content));
	PageHelper.startPage(page, rows);
	SearchRequestBuilder searchRequestBuilder = ESClientUtils.getClient().prepareSearch("hotcomments")
			.setTypes("logs").setSearchType(SearchType.DFS_QUERY_THEN_FETCH) 
			.setQuery(boolBuilder).setFrom(page).setSize(3);
	long totalHits = ESClientUtils.getClient().prepareSearch("hotcomments").setTypes("logs")
			.setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(boolBuilder).get().getHits().getTotalHits();
	searchRequestBuilder.setHighlighterPostTags("</span>");
	searchRequestBuilder.setHighlighterPreTags("<span style=\"color:red\">");
	searchRequestBuilder.addHighlightedField("content");
	// 执行结果
	SearchResponse response = searchRequestBuilder.get();
	// 接受结果
	List<Map<String, Object>> result = new ArrayList<>();
	// 遍历结果
	for (SearchHit hit : response.getHits()) {
		Map<String, Object> source = hit.getSource();
		String songName = null;
		String songUrl = null;
		Set<Entry<String, Object>> entrySet = source.entrySet();
		for (Entry<String, Object> entry : entrySet) {
			if (entry.getKey().equals("songId")) {
				Integer songId = (Integer) entry.getValue();
				songName = SearchUtils.getSongNameById(songId);
				songUrl = SearchUtils.getSongUrlById(songId);
			}
		}
		source.put("songName", songName);
		source.put("songUrl", songUrl);
		// 处理高亮片段
		Map<String, HighlightField> highlightFields = hit.getHighlightFields();
		HighlightField nameField = highlightFields.get("content");
		if (nameField != null) {
			Text[] fragments = nameField.fragments();
			String nameTmp = "";
			for (Text text : fragments) {
				nameTmp += text;
			}
			// 将高亮片段组装到结果中去
			source.put("content", nameTmp);
		}
		result.add(source);
	}
	long totalPage = (totalHits + 3 - 1) / 3;
	if(page > totalPage){
		page = 1;
	}
	searchListPageBean.setPage(page);
	searchListPageBean.setTotalPage(totalPage);
	searchListPageBean.setList(result);
	return searchListPageBean;
}
 
Example 18
Source File: ProcessInstanceHistogramResource.java    From camunda-bpm-elasticsearch with Apache License 2.0 4 votes vote down vote up
@GET
  public AggregationsResult getDateHistogramAggregrations(
      @QueryParam("interval") String interval,
      @QueryParam("timeframe") String timeframe
  ) {

    Client client = ElasticSearchClientProvider.getClient(getProcessEngine());

    DateHistogram.Interval dateInterval = null;
    switch (interval) {
      case "s":
      case "m":
      case "h":
      case "d":
      case "w":
      case "M":
      case "q":
      case "y":
      default:
        dateInterval = DateHistogram.Interval.SECOND;
        break;
    }

    // create buckets based on startTime
    DateHistogramBuilder histogramStartTime = AggregationBuilders.dateHistogram("dateHistogram")
        .minDocCount(0)
        .interval(dateInterval)
        .field("startTime");
    // only get the running process instances
    FilterAggregationBuilder runningPIsAgg = AggregationBuilders.filter("running")
        .filter(FilterBuilders.missingFilter("endTime"));
    runningPIsAgg.subAggregation(histogramStartTime);

    // create buckets based on endTime
    DateHistogramBuilder histogramEndTime = AggregationBuilders.dateHistogram("dateHistogram")
        .minDocCount(0)
        .interval(dateInterval)
        .field("endTime");
    // only get the ended process instances
    FilterAggregationBuilder endedPIsAgg = AggregationBuilders.filter("ended")
        .filter(FilterBuilders.existsFilter("endTime"));
    endedPIsAgg.subAggregation(histogramEndTime);


    SearchRequestBuilder searchRequestBuilder = client.prepareSearch(ES_DEFAULT_INDEX_NAME_CAMUNDA_BPM)
        .setQuery(QueryBuilders.matchAllQuery())
        .addAggregation(runningPIsAgg)
        .addAggregation(endedPIsAgg)
        .setSearchType(SearchType.COUNT);

    System.out.println(searchRequestBuilder);

    SearchResponse searchResponse = searchRequestBuilder.get();

    long totalHits = searchResponse.getHits().getTotalHits();

    Filter running = searchResponse.getAggregations().get("running");
//    long runningTotal = running.getDocCount();

    DateHistogram runningDateHistogram = running.getAggregations().get("dateHistogram");
    List<DateHistogramBucketPair> runningDateHistogramBuckets = parseDateHistogramAggregation(runningDateHistogram);


    Filter ended = searchResponse.getAggregations().get("ended");
//    long endedTotal = ended.getDocCount();

    DateHistogram endedDateHistogram = ended.getAggregations().get("dateHistogram");
    List<DateHistogramBucketPair> endedDateHistogramBuckets = parseDateHistogramAggregation(endedDateHistogram);

    HashMap<String, List<DateHistogramBucketPair>> dateHistogramBucketPairs = new HashMap<>();
    dateHistogramBucketPairs.put("running", runningDateHistogramBuckets);
    dateHistogramBucketPairs.put("ended", endedDateHistogramBuckets);

    AggregationsResult aggregationsResult = new AggregationsResult();
    aggregationsResult.setDateHistogramBuckets(dateHistogramBucketPairs);
    aggregationsResult.setTotalHits(totalHits);

    return aggregationsResult;
  }
 
Example 19
Source File: HistoryEventsMappingTest.java    From camunda-bpm-elasticsearch with Apache License 2.0 4 votes vote down vote up
@Test
//  @ElasticsearchIndex(
//      indexName = ES_DEFAULT_INDEX_NAME_CAMUNDA_BPM,
//      cleanAfter = true,
//      mappings = @ElasticsearchMapping(typeName = ES_DEFAULT_TYPE_NAME_CAMUNDA_BPM, properties = {
//          @Elasticsearch
//      }))
  public void testIndexingSingleInvoice() throws IOException {
    HashMap<String,ProcessDataContainer> processesById = TestDataGenerator.startInvoiceProcess(processEngineRule.getProcessEngine(), 1);

    String[] pids = processesById.keySet().toArray(new String[0]);

    // elasticsearch //////////////////////////////

    flushAndRefresh();

    showMappings(ES_DEFAULT_INDEX_NAME_CAMUNDA_BPM);

    FilteredQueryBuilder query = QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), FilterBuilders.termFilter("processInstanceId", pids[0]));

    SearchRequestBuilder searchRequestBuilder = client.prepareSearch(ES_DEFAULT_INDEX_NAME_CAMUNDA_BPM)
        .setQuery(query);

    IoUtil.writeToFile(searchRequestBuilder.toString(), this.getClass().getSimpleName() + ".json", true);

    SearchResponse searchResponse = searchRequestBuilder.get();
    SearchHits hits = searchResponse.getHits();
    assertEquals(1, hits.totalHits());


    SearchHit hit = hits.getAt(0);
    assertEquals(pids[0], hit.getId());
    assertEquals(ES_DEFAULT_TYPE_NAME_CAMUNDA_BPM, hit.getType());

    Map<String,Object> source = hit.getSource();
    assertNotNull(source.get("startTime"));
    assertNotNull(source.get("endTime"));
    ArrayList variables = (ArrayList) source.get("variables");
    assertEquals(5, variables.size());
    ArrayList tasks = (ArrayList) source.get("tasks");
    assertEquals(9, tasks.size());
    ArrayList activities = (ArrayList) source.get("activities");
    assertEquals(19, activities.size());

    logger.info(searchResponse.toString());

//    for (SearchHit searchHit : searchResponse.getHits()) {
//      logger.info(searchHit.sourceAsString());
//    }

    showMappings(ES_DEFAULT_INDEX_NAME_CAMUNDA_BPM);

    assertMappings(ES_DEFAULT_INDEX_NAME_CAMUNDA_BPM);
    // TODO: write assertions for mapping
  }
 
Example 20
Source File: Application.java    From ElasticSearch with MIT License 4 votes vote down vote up
@PostMapping("query/book/novel")
@ResponseBody
public ResponseEntity query(
        @RequestParam(name = "author", required = false) String author,
        @RequestParam(name = "title", required = false) String title,
        @RequestParam(name = "gt_word_count", defaultValue = "0") int gtWordCount,
        @RequestParam(name = "lt_word_count", required = false) Integer ltWordCount) {

    BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();

    if (author != null) {
        boolQuery.must(QueryBuilders.matchQuery("author", author));
    }

    if (title != null) {
        boolQuery.must(QueryBuilders.matchQuery("title", title));
    }

    RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("word_count").from(gtWordCount);
    if (ltWordCount != null && ltWordCount > 0) {
        rangeQuery.to(ltWordCount);
    }
    boolQuery.filter(rangeQuery);

    SearchRequestBuilder builder = client.prepareSearch(BOOK_INDEX).setTypes(BOOK_TYPE_NOVEL)
            .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
            .setQuery(boolQuery)
            .setFrom(0)
            .setSize(10);

    logger.debug(builder.toString());

    SearchResponse response = builder.get();

    List result = new ArrayList<Map<String, Object>>();
    for (SearchHit hit : response.getHits()) {
        result.add(hit.getSourceAsMap());
    }

    return new ResponseEntity(result, HttpStatus.OK);

}