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

The following examples show how to use org.elasticsearch.action.search.SearchRequestBuilder#setTypes() . 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: ElasticsearchUtil.java    From SpringBootLearn with Apache License 2.0 5 votes vote down vote up
/**
 * 使用分词查询  排序 高亮
 * @param index          索引名称
 * @param type           类型名称,可传入多个type逗号分隔
 * @param query          查询条件
 * @param size           文档大小限制
 * @param fields         需要显示的字段,逗号分隔(缺省为全部字段)
 * @param sortField      排序字段
 * @param highlightField 高亮字段
 * @return 结果
 */
public static List<Map<String, Object>> searchListData(String index, String type, QueryBuilder query, Integer size, String fields, String sortField, String highlightField) {
    SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index);
    if (StringUtils.isNotEmpty(type)) {
        searchRequestBuilder.setTypes(type.split(","));
    }
    if (StringUtils.isNotEmpty(highlightField)) {
        HighlightBuilder highlightBuilder = new HighlightBuilder();
        // 设置高亮字段
        highlightBuilder.field(highlightField);
        searchRequestBuilder.highlighter(highlightBuilder);
    }
    searchRequestBuilder.setQuery(query);
    if (StringUtils.isNotEmpty(fields)) {
        searchRequestBuilder.setFetchSource(fields.split(","), null);
    }
    searchRequestBuilder.setFetchSource(true);
    if (StringUtils.isNotEmpty(sortField)) {
        searchRequestBuilder.addSort(sortField, SortOrder.ASC);
    }
    if (size != null && size > 0) {
        searchRequestBuilder.setSize(size);
    }//打印的内容 可以在 Elasticsearch head 和 Kibana  上执行查询
    log.info("\n{}", searchRequestBuilder);
    SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();
    long totalHits = searchResponse.getHits().totalHits;
    long length = searchResponse.getHits().getHits().length;
    log.info("共查询到[{}]条数据,处理数据条数[{}]", totalHits, length);
    if (searchResponse.status().getStatus() == 200) {
        // 解析对象
        return setSearchResponse(searchResponse, highlightField);
    }
    return null;
}
 
Example 2
Source File: AnnotationRepository.java    From cicada with MIT License 5 votes vote down vote up
/**
 * 根据traceId和spanId获取annotationModel列表.
 */
public List<AnnotationModel> getSpanAnnotations(final String traceId, final String spanId) {
  // 声明SearchRequestBuilder实例
  final String indice = getDefaultIndice();
  final SearchRequestBuilder builder = client.prepareSearch(indice);
  builder.setTypes(props.getEsTypeName());

  // 设置查询条件
  final BoolQueryBuilder query = new BoolQueryBuilder();
  query.must(QueryBuilders.termQuery("traceId", traceId)) //
      .must(QueryBuilders.termQuery("spanId", spanId));

  // 执行查询
  final SearchResponse response = builder.setQuery(query).execute().actionGet();

  // 处理返回结果
  final List<AnnotationModel> annos = new LinkedList<AnnotationModel>();
  for (final SearchHit hit : response.getHits().hits()) {
    final String docStr = hit.getSourceAsString();
    try {
      final AnnotationModel model = JSON.parseObject(docStr, AnnotationModel.class);
      annos.add(model);
    } catch (JSONException ex) {
      log.error("failed load data {} to AnnotationModel, error {}", docStr, ex);
      continue;
    }
  }

  return annos;
}
 
Example 3
Source File: SpanRepository.java    From cicada with MIT License 5 votes vote down vote up
/**
 * 根据TraceId获取所有的SpanModel信息.
 */
public List<SpanModel> getTraceSpanModels(final String traceId) {
  // 创建SearchRequestBuilder实例,设置builder属性
  final String indice = getDefaultIndice();
  final SearchRequestBuilder builder = client.prepareSearch(indice);
  builder.setTypes(props.getEsTypeName());

  // 设置查询条件
  final BoolQueryBuilder query = new BoolQueryBuilder();
  query.must(QueryBuilders.termQuery("traceId", traceId));

  // 执行查询
  final SearchResponse response = builder.setQuery(query).execute().actionGet();
  return parseResponse(response);
}
 
Example 4
Source File: Elasticsearch.java    From jlogstash-input-plugin with Apache License 2.0 5 votes vote down vote up
public void emit() {
    // 构建查询
    Scroll esScroll = new Scroll(new TimeValue(scroll, TimeUnit.MINUTES));
    SearchRequestBuilder searchRequestBuilder = client.prepareSearch(index).setScroll(esScroll).setQuery(query).setSize(size);
    if (StringUtils.isNotEmpty(type)) {
        searchRequestBuilder.setTypes(type);
    }

    if (logger.isDebugEnabled()) {
        logger.debug(searchRequestBuilder.toString());
    }

    String scrollId = null;
    try {
        SearchResponse searchResponse = searchRequestBuilder.execute().actionGet();
        scrollId = searchResponse.getScrollId();

        long total = searchResponse.getHits().getTotalHits();
        // 处理第一次search查询
        int messageCount = submitMessage(searchResponse);

        // 计算剩余的批次
        long remainTotal = total - messageCount;
        long batchNum = remainTotal % size == 0 ? remainTotal / size : (remainTotal / size) + 1;

        for (long i = 0; !stop && i < batchNum; i++) {
            // 按批查询数据
            SearchScrollRequestBuilder scrollRequestBuilder = client.prepareSearchScroll(scrollId).setScroll(esScroll);
            searchResponse = scrollRequestBuilder.execute().actionGet();
            submitMessage(searchResponse);
        }
    } catch (Exception e) {
        logger.error("query error", e);
    } finally {
        if (StringUtils.isNotEmpty(scrollId)) {
            client.prepareClearScroll().addScrollId(scrollId).execute().actionGet();
        }
    }
}
 
Example 5
Source File: DeleteTest.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
@Test
public void deleteAllTest() throws SqlParseException, SQLFeatureNotSupportedException {
	delete(String.format("DELETE FROM %s/temp_account", TEST_INDEX_ACCOUNT_TEMP), TEST_INDEX_ACCOUNT_TEMP);

	// Assert no results exist for this type.
	SearchRequestBuilder request = MainTestSuite.getClient().prepareSearch(TEST_INDEX_ACCOUNT_TEMP);
	request.setTypes("temp_account");
	SearchResponse response = request.setQuery(QueryBuilders.matchAllQuery()).get();
	assertThat(response.getHits().getTotalHits().value, equalTo(0L));
}
 
Example 6
Source File: DeleteTest.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
@Test
public void deleteWithConditionTest() throws SqlParseException, SQLFeatureNotSupportedException {
	delete(String.format("DELETE FROM %s/phrase WHERE phrase = 'quick fox here' ", TEST_INDEX_PHRASE), TEST_INDEX_PHRASE);
	// Assert no results exist for this type.
	SearchRequestBuilder request = MainTestSuite.getClient().prepareSearch(TEST_INDEX_PHRASE);
	request.setTypes("phrase");
	SearchResponse response = request.setQuery(QueryBuilders.matchAllQuery()).get();
	assertThat(response.getHits().getTotalHits().value, equalTo(5L));
}
 
Example 7
Source File: ReindexingService.java    From elasticsearch-reindexing with Apache License 2.0 5 votes vote down vote up
/**
 * Execute the reindexing
 *
 * @param params   Rest request
 * @param content  Content of rest request in {}
 * @param listener is to receive the response back
 * @return
 */
public String execute(final Params params, final BytesReference content, final ActionListener<Void> listener) {

    final String url = params.param("url");
    // set scroll to 1m if there is no
    final String scroll = params.param("scroll", "1m");
    final String fromIndex = params.param("index");
    final String fromType = params.param("type");
    final String toIndex = params.param("toindex");
    final String toType = params.param("totype");
    final String[] fields = params.paramAsBoolean("parent", true) ? new String[]{"_source", "_parent"} : new String[]{"_source"};
    final boolean deletion = params.paramAsBoolean("deletion", false);

    final ReindexingListener reindexingListener = new ReindexingListener(url, fromIndex, fromType, toIndex, toType, scroll, deletion, listener);

    // Create search request builder
    final SearchRequestBuilder builder = client.prepareSearch(fromIndex)
            .setScroll(scroll).addFields(fields);
    if (fromType != null && fromType.trim().length() > 0) {
        builder.setTypes(fromType.split(","));
    }
    if (content == null || content.length() == 0) {
        builder.setQuery(QueryBuilders.matchAllQuery()).setSize(
                Integer.parseInt(params.param("size", "1000")));
    } else {
        builder.setExtraSource(content);
    }
    builder.execute(reindexingListener);  // async
    reindexingListenerMap.put(reindexingListener.getName(), reindexingListener);
    return reindexingListener.getName();
}
 
Example 8
Source File: EsQuery.java    From AsuraFramework with Apache License 2.0 4 votes vote down vote up
private SearchRequestBuilder buildSearchRequest (EsQueryDo esQueryObj) throws  EsException {
    if (Check.NuNStrStrict(esClientFactory.getIndexs(esQueryObj.getIndexName()))) {
        throw new EsException("没有指定要搜索的索引名称(indexName)");
    }
    for (ThreadPoolStats.Stats stats : esClientFactory.getClient().threadPool().stats()) {
        logger.info(JSON.toJSONString(stats));
    }
    //加载要搜索索引
    SearchRequestBuilder searchRequestBuilder = esClientFactory.getClient().prepareSearch(esClientFactory.getIndexs(esQueryObj.getIndexName()));
    //由spring从配置加载要搜索的index的类型
    searchRequestBuilder.setTypes(esQueryObj.getTypeName());
    //由spring从配置加载要搜索的类型
    searchRequestBuilder.setSearchType(SearchType.fromId(esQueryObj.getSearchType()));
    //查询可以为null
    searchRequestBuilder.setQuery(esQueryObj.getQueryBuilder());

    if (!Check.NuNCollection(esQueryObj.getSortBuilders())) {
        for (SortBuilder sortBuilder : esQueryObj.getSortBuilders()) {
            searchRequestBuilder.addSort(sortBuilder);
        }
    }
    if (!Check.NuNCollection(esQueryObj.getAggregationBuilders())) {
        for (AbstractAggregationBuilder aggregationBuilder : esQueryObj.getAggregationBuilders()) {
            searchRequestBuilder.addAggregation(aggregationBuilder);
        }

    }
    //设置高亮域
   if (esQueryObj.isHighLigth()) {
        if (!Check.NuNObject(esQueryObj.highLigthFields())) {
            for (String hlFieldName : esQueryObj.highLigthFields()) {
                searchRequestBuilder.addHighlightedField(hlFieldName).setHighlighterPreTags(esQueryObj.getHighLigthPreTag())
                        .setHighlighterPostTags(esQueryObj.getHighLigthPostTag());
            }
        }
    }
    //分页
    searchRequestBuilder.setFrom(esQueryObj.getFromIndex()).setSize(esQueryObj.getSize());
    searchRequestBuilder.setExplain(esQueryObj.isExplain());
    return searchRequestBuilder;
}
 
Example 9
Source File: ESQueryState.java    From sql4es with Apache License 2.0 4 votes vote down vote up
/**
 * Builds the Elasticsearch query object based on the parsed information from the SQL query
 * @param searchReq
 * @param info
 */
private void buildQuery(SearchRequestBuilder searchReq, ParseResult info) {
	String[] types = new String[info.getSources().size()];
	for(int i=0; i<info.getSources().size(); i++) types[i] = info.getSources().get(i).getSource(); 
	SearchRequestBuilder req = searchReq.setTypes(types);
	
	// add filters and aggregations
	if(info.getAggregation() != null){
		// when aggregating the query must be a query and not a filter
		if(info.getQuery() != null)	req.setQuery(info.getQuery());
		req.addAggregation(info.getAggregation());
		
	// ordering does not work on aggregations (has to be done in client)
	}else if(info.getQuery() != null){
		if(info.getRequestScore()) req.setQuery(info.getQuery()); // use query instead of filter to get a score
		else req.setPostFilter(info.getQuery());
		
		// add order
		for(OrderBy ob : info.getSorts()){
			req.addSort(ob.getField(), ob.getOrder());
		}
	} else req.setQuery(QueryBuilders.matchAllQuery());
	
	this.limit = info.getLimit();
	if(splitRS) maxRowsRS = fetchSize;
	
	//System.out.println("fetch: "+fetchSize+" limit: "+limit+" split: "+splitRS);
	
	// add limit and determine to use scroll
	if(info.getAggregation() != null) {
		req = req.setSize(0);
	} else{
		if(limit > 0 && limit < fetchSize){ // no scroll needed
			req.setSize(limit);
		} else{ // use scrolling
			req.setSize(fetchSize);
			req.setScroll(new TimeValue(Utils.getIntProp(props, Utils.PROP_SCROLL_TIMEOUT_SEC, 60)*1000));
			if (info.getSorts().isEmpty()) req.addSort("_doc", SortOrder.ASC); // scroll works fast with sort on _doc
		}
	}
	
	// use query cache when this was indicated in FROM clause
	if(info.getUseCache()) req.setRequestCache(true);
	req.setTimeout(TimeValue.timeValueMillis(Utils.getIntProp(props, Utils.PROP_QUERY_TIMEOUT_MS, 10000)));
}
 
Example 10
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;
}