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

The following examples show how to use org.elasticsearch.action.search.SearchRequestBuilder#addFields() . 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: QuestionElasticSearchIndexBuilder.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Override
protected  SearchRequestBuilder addSearchResultFields(SearchRequestBuilder searchRequestBuilder) {
    if (ArrayUtils.isEmpty(searchResultFieldNames)) {
        return searchRequestBuilder;
    }
    return searchRequestBuilder.addFields(searchResultFieldNames);
}
 
Example 2
Source File: QuestionElasticSearchIndexBuilder.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Override
protected  SearchRequestBuilder addSearchResultFields(SearchRequestBuilder searchRequestBuilder) {
    if (ArrayUtils.isEmpty(searchResultFieldNames)) {
        return searchRequestBuilder;
    }
    return searchRequestBuilder.addFields(searchResultFieldNames);
}
 
Example 3
Source File: EsSearch.java    From es-service-parent with Apache License 2.0 4 votes vote down vote up
/**
 * 搜索入口
 * 
 * @param request
 * @param timeout
 * @param timeUnit
 * @return
 */
public static EsResponse search(EsRequest request, int timeout, TimeUnit timeUnit) {
    // 索引
    String indexname = request.getIndexname();
    // 结束位置
    int size = request.getPs();
    // 开始位置
    int from = request.getPn() < 0 ? 0 : (request.getPn() - 1) * size;

    // 主查询体
    SearchRequestBuilder srb = ESClient.getClient().prepareSearch(indexname)
            .setTypes(request.getTypes().toArray(new String[] {})).setFrom(from).setSize(size)
            .setPreference(Constants.preference).setTimeout(new TimeValue(timeout, timeUnit));

    // 构造查询体
    EsQueryBuilder esQueryBuilder = new EsQueryBuilder(request);
    esQueryBuilder.makeFilterBuilder(srb);
    if (esQueryBuilder.makeQueryBuilder(srb) == null) {
        return new EsResponse();
    }

    // 处理返回字段
    if (!request.getSafeResultFileds().isEmpty()) {
        srb.addFields(request.getSafeResultFileds().toArray(new String[] {}));
    }
    // 高亮
    EsHighLight.setHighLight(srb, request.getSafeHighlightFields());
    // 排序
    EsSort.sortAdapter(srb, request.getSafeSortFileds());
    // 调试模式
    if (Constants.isDebug) {
        srb.setExplain(true);
        logger.info("\r\n" + srb.toString());
    }

    // 返回结果
    SearchResponse response = srb.execute().actionGet();
    List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
    for (SearchHit hit : response.getHits()) {
        Map<String, Object> record = hit.getSource();
        if (record == null && (record = new HashMap<String, Object>()) != null) {
            for (Map.Entry<String, SearchHitField> e : hit.fields().entrySet()) {
                record.put(e.getKey(), e.getValue().getValue());
            }
        }
        record.put("ESSCORE", hit.getScore());
        // 高亮返回
        if (request.getSafeHighlightFields() != null) {
            record.putAll(EsHighLight.getHighlight(hit, request.getSafeHighlightFields()
                    .getFields()));
        }
        result.add(record);
    }

    // 调试模式
    if (Constants.isDebug) {
        logger.info("search result :{}", JsonUtil.toJson(result));
        logger.info("search TotalHits :{}", response.getHits().getTotalHits());
        logger.info("search TookInMillis :{}", response.getTookInMillis());
    }

    logger.info("search log for index:{},keys:{}", indexname, request.getAllValue());
    return new EsResponse((int) response.getHits().getTotalHits(), JsonUtil.toJson(result), 0);
}
 
Example 4
Source File: BaseElasticSearchIndexBuilder.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
protected  SearchRequestBuilder addSearchResultFields(SearchRequestBuilder searchRequestBuilder) {
    if (ArrayUtils.isEmpty(searchResultFieldNames)) {
        return searchRequestBuilder;
    }
    return searchRequestBuilder.addFields(searchResultFieldNames);
}
 
Example 5
Source File: BaseElasticSearchIndexBuilder.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
protected SearchRequestBuilder addSearchSuggestionResultFields(SearchRequestBuilder searchRequestBuilder) {
    if (ArrayUtils.isEmpty(suggestionResultFieldNames)) {
        return searchRequestBuilder;
    }
    return searchRequestBuilder.addFields(suggestionResultFieldNames);
}
 
Example 6
Source File: ProductQueryServiceImpl.java    From elasticsearch-tutorial with MIT License 4 votes vote down vote up
@Override
public ProductSearchResult searchProducts(SearchCriteria searchCriteria)
{
    QueryBuilder queryBuilder = getQueryBuilder(searchCriteria);

    SearchRequestBuilder requestBuilder = getSearchRequestBuilder(searchCriteria.getIndexes(), 
                                                                    searchCriteria.getDocumentTypes(), 
                                                                    searchCriteria.getFrom(), 
                                                                    searchCriteria.getSize());
    requestBuilder.addFields(SearchDocumentFieldName.productQueryFields);
    
    if(searchCriteria.isRescoreOnSoldOut())
    {
        Rescorer rescorer = RescoreBuilder.queryRescorer(QueryBuilders.termQuery(SearchDocumentFieldName.SOLD_OUT.getFieldName(), false))
                                           .setQueryWeight(1.0f) //default
                                           .setRescoreQueryWeight(1.5f)
                                           ;
        requestBuilder.setRescorer(rescorer);
    }
    
    if (searchCriteria.hasFilters())
    {
        AndFilterBuilder andFilterBuilder = getFilterBuilderForSearchCriteria(searchCriteria);
        requestBuilder.setQuery(QueryBuilders.filteredQuery(queryBuilder, andFilterBuilder));
    } else
    {
        requestBuilder.setQuery(queryBuilder);
    }

    if (!searchCriteria.isNoFacets() && searchCriteria.getFacets().size() > 0)
    {
        addFacets(searchCriteria, requestBuilder);
    }

  //Add sorting
    if(searchCriteria.getSortOrder() !=null)
    {
        //First on given field
        requestBuilder.addSort(SortBuilders.fieldSort(SearchDocumentFieldName.AVAILABLE_DATE.getFieldName()).order(searchCriteria.getSortOrder()).missing("_last"));
        //then on score based
        requestBuilder.addSort(SortBuilders.scoreSort());
    }

    logger.debug("Executing following search request:" + requestBuilder.internalBuilder().toString());
    
    SearchResponse searchResponse = requestBuilder.execute().actionGet();
    
    printSearchResponseForDebug(searchResponse);
    
    return getProductSearchResults(searchResponse);
}
 
Example 7
Source File: ProductQueryServiceImpl.java    From searchanalytics-bigdata with MIT License 4 votes vote down vote up
@Override
public ProductSearchResult searchProducts(
		final SearchCriteria searchCriteria) {
	final QueryBuilder queryBuilder = getQueryBuilder(searchCriteria);
	final SearchRequestBuilder requestBuilder = getSearchRequestBuilder(
			searchCriteria.getIndexes(), searchCriteria.getDocumentTypes(),
			searchCriteria.getFrom(), searchCriteria.getSize());
	requestBuilder.addFields(SearchDocumentFieldName.productQueryFields);
	if (searchCriteria.isRescoreOnSoldOut()) {
		final Rescorer rescorer = RescoreBuilder
				.queryRescorer(
						QueryBuilders.termQuery(
								SearchDocumentFieldName.SOLD_OUT
										.getFieldName(), false))
				.setQueryWeight(1.0f) // default
				.setRescoreQueryWeight(1.5f);
		requestBuilder.setRescorer(rescorer);
	}
	if (searchCriteria.hasFilters()) {
		final AndFilterBuilder andFilterBuilder = getFilterBuilderForSearchCriteria(searchCriteria);
		requestBuilder.setQuery(QueryBuilders.filteredQuery(queryBuilder,
				andFilterBuilder));
	} else {
		requestBuilder.setQuery(queryBuilder);
	}
	if (!searchCriteria.isNoFacets()
			&& searchCriteria.getFacets().size() > 0) {
		addFacets(searchCriteria, requestBuilder);
	}
	// Add sorting
	if (searchCriteria.getSortOrder() != null) {
		// First on given field
		requestBuilder.addSort(SortBuilders
				.fieldSort(
						SearchDocumentFieldName.AVAILABLE_DATE
								.getFieldName())
				.order(searchCriteria.getSortOrder()).missing("_last"));
		// then on score based
		requestBuilder.addSort(SortBuilders.scoreSort());
	}
	logger.debug("Executing following search request:"
			+ requestBuilder.internalBuilder().toString());
	final SearchResponse searchResponse = requestBuilder.execute()
			.actionGet();
	printSearchResponseForDebug(searchResponse);
	return getProductSearchResults(searchResponse);
}
 
Example 8
Source File: BaseElasticSearchIndexBuilder.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
protected  SearchRequestBuilder addSearchResultFields(SearchRequestBuilder searchRequestBuilder) {
    if (ArrayUtils.isEmpty(searchResultFieldNames)) {
        return searchRequestBuilder;
    }
    return searchRequestBuilder.addFields(searchResultFieldNames);
}
 
Example 9
Source File: BaseElasticSearchIndexBuilder.java    From sakai with Educational Community License v2.0 4 votes vote down vote up
protected SearchRequestBuilder addSearchSuggestionResultFields(SearchRequestBuilder searchRequestBuilder) {
    if (ArrayUtils.isEmpty(suggestionResultFieldNames)) {
        return searchRequestBuilder;
    }
    return searchRequestBuilder.addFields(suggestionResultFieldNames);
}