Spring数据elasticsearch返回重复的结果

时间:2015-08-28 04:05:49

标签: java spring elasticsearch spring-data elasticsearch-java-api

默认情况下,我无法在Elasticsearch查询中找到设置from属性的方法,因此我将ElasticsearchTemplate类扩展为包含一个。

现在返回考虑from属性的结果。但不知何故,它在结果中返回重复的文档。 这可能是由于扩展类应用了ResultMapping。

public class ElasticsearchTemplateImproved extends ElasticsearchTemplate {

@Autowired
private ElasticsearchTemplate elasticsearchTemplate;

private int counter;

private static final Logger logger = LoggerFactory.getLogger(ElasticsearchTemplateImproved.class);
@Autowired
private Client client;
@Autowired
private ElasticsearchConverter elasticsearchConverter;
private ResultsMapper resultsMapper;
private String searchTimeout;


public ElasticsearchTemplateImproved(Client client) {
    this(client, null, null);
}

public ElasticsearchTemplateImproved(Client client, ResultsMapper resultsMapper) {
    this(client, null, resultsMapper);
}

public ElasticsearchTemplateImproved(Client client, Object object,
        ResultsMapper resultsMapper) {
    super(client, null, null);

    this.client = client;
    this.elasticsearchConverter = (elasticsearchConverter == null) ? new MappingElasticsearchConverter(
            new SimpleElasticsearchMappingContext()) : elasticsearchConverter;
    this.resultsMapper = (resultsMapper == null) ? new DefaultResultMapper(this.elasticsearchConverter.getMappingContext()) : resultsMapper;
}

public <T> FacetedPage<T> queryForPage(NativeSearchQuery query, Class<T> clazz, int from) {
    FacetedPage<T> response = queryForPage(query, clazz, resultsMapper, from);
    logger.info("-------------------------------------------------------");
    Iterator iterator = response.getContent().iterator();
    int i = 0;
    while(iterator.hasNext()) {
        Product p = (Product) iterator.next();
        logger.info("product " + i + ": " + p.getProductId());
        i++;
    }
    logger.info("-------------------------------------------------------");
    return response;
}

public <T> FacetedPage<T> queryForPage(NativeSearchQuery query, Class<T> clazz, SearchResultMapper mapper, int from) {
    SearchResponse response = getSearchResponse(prepareSearch(query, clazz, from).setQuery(query.getQuery()).execute());
    return mapper.mapResults(response, clazz, query.getPageable());
}

private SearchResponse getSearchResponse(ListenableActionFuture<SearchResponse> response) {

    return searchTimeout == null ? response.actionGet() : response.actionGet(searchTimeout);
}

private <T> SearchRequestBuilder prepareSearch(NativeSearchQuery query, Class<T> clazz, int from) {

    return prepareSearch(query, from);
}

@SuppressWarnings("deprecation")
private SearchRequestBuilder prepareSearch(NativeSearchQuery query, int from) {
    Assert.notNull(query.getIndices(), "No index defined for Query");
    Assert.notNull(query.getTypes(), "No type defined for Query");

    int startRecord = 0;
    SearchRequestBuilder searchRequestBuilder = client.prepareSearch(toArray(query.getIndices()))
            .setSearchType(query.getSearchType()).setTypes(toArray(query.getTypes()));

    if (query.getPageable() != null) {
        startRecord = query.getPageable().getPageNumber() * query.getPageable().getPageSize();
        searchRequestBuilder.setSize(query.getPageable().getPageSize());
    }
    searchRequestBuilder.setFrom(startRecord + from);

    if (!query.getFields().isEmpty()) {
        searchRequestBuilder.addFields(toArray(query.getFields()));
    }

    if (query.getSort() != null) {
        for (Sort.Order order : query.getSort()) {
            searchRequestBuilder.addSort(order.getProperty(), order.getDirection() == Sort.Direction.DESC ? SortOrder.DESC
                    : SortOrder.ASC);
        }
    }
    if (CollectionUtils.isNotEmpty(query.getFacets())) {
        for (FacetRequest facetRequest : query.getFacets()) {
            FacetBuilder facet = facetRequest.getFacet();
            if (facetRequest.applyQueryFilter() && query.getFilter() != null) {
                facet.facetFilter(query.getFilter());
            }
            searchRequestBuilder.addFacet(facet);
        }
    }
    if (query.getMinScore() > 0) {
        searchRequestBuilder.setMinScore(query.getMinScore());
    }
    return searchRequestBuilder;
}

private static String[] toArray(List<String> values) {
    String[] valuesAsArray = new String[values.size()];
    return values.toArray(valuesAsArray);
}
}

0 个答案:

没有答案