0

How I should sorting data in elasticsearch via URL ? I m find that http://www.stackoverflow.com/questions/13894553/elasticsearch-set-sort-order-using-querystring and this https://www.elastic.co/guide/en/elasticsearch/reference/current/search-uri-request.html but it doesnt work.

I try add sort mode to the URL like this

/transaction/search?seller=335&size=10&page=502&sort=seller:asc

But got failure:

No mapping found for [seller:asc] in order to sort on

My mapping looks like:

{
    "all": {
        "mappings": {
            "auctions": {
                "_all": { "enabled": false }, 
                "properties": {
                    "cat": { "store": true,  "type": "long" }, 
                    "curr": { "index": "not_analyzed",  "store": true,  "type": "string" }, 
                    "end_date": { "store": true,  "type": "long" }, 
                    "price": { "store": true,  "type": "long" }, 
                    "start_date": { "store": true,  "type": "long" }, 
                    "tcat": { "store": true,  "type": "long" }, 
                    "title": { "store": true,  "type": "string" }, 
                    "uid": { "store": true,  "type": "long" }
                }
            }, 
            "trans": {
                "_all": { "enabled": false }, 
                "properties": {
                    "buyer": { "store": true,  "type": "long" }, 
                    "cat": { "store": true,  "type": "long" }, 
                    "comment_text": { "store": true,  "type": "string" }, 
                    "comment_type": { "store": true,  "type": "long" }, 
                    "item": { "store": true,  "type": "long" }, 
                    "price": { "store": true,  "type": "long" }, 
                    "seller": { "store": true,  "type": "long" }, 
                    "title": { "store": true,  "type": "string" }, 
                    "tree_cat": { "store": true,  "type": "long" }, 
                    "ts": { "store": true,  "type": "long" }
                }
            }
        }
    }
}

I must catch this sort and add in code or what?

I try do it from spring boot.

My method Controller looks like:

@RequestMapping(value = "/auctions/search", produces = MediaType.APPLICATION_JSON_VALUE)
    private Map search(
            @RequestParam(value = "categoryId", required = false) Long categoryId,
            @RequestParam(value = "treeCategoryId", required = false) Long treeCategoryId,
            @RequestParam(value = "currency", required = false) String currency,
            @RequestParam(value = "priceFrom", required = false) Long priceFrom,
            @RequestParam(value = "priceTo", required = false) Long priceTo,
            @RequestParam(value = "startDateFrom", required = false) Long startDateFrom,
            @RequestParam(value = "startDateTo", required = false) Long startDateTo,
            @RequestParam(value = "endDateFrom", required = false) Long endDateFrom,
            @RequestParam(value = "endDateTo", required = false) Long endDateTo,
            @RequestParam(value = "title", required = false) String title,
            @RequestParam(value = "uid", required = false) Long uid,
            Pageable pageable) {

        final AuctionIndexSearchParams searchParams = AuctionIndexSearchParams.builder()
                .categoryId(categoryId)
                .treeCategoryId(treeCategoryId)
                .currency(currency)
                .priceFrom(priceFrom)
                .priceTo(priceTo)
                .startDateFrom(startDateFrom)
                .startDateTo(startDateTo)
                .endDateFrom(endDateFrom)
                .endDateTo(endDateTo)
                .title(title)
                .uid(uid)
                .build();

        return auctionService.searchByIndexParams(searchParams, pageable);
    }

Service:

 public Map searchByIndexParams(AuctionIndexSearchParams searchParams, Pageable pageable) {
        final List<FilterBuilder> filters = Lists.newArrayList();
        final NativeSearchQueryBuilder searchQuery = new NativeSearchQueryBuilder().withQuery(matchAllQuery());


        Optional.ofNullable(searchParams.getCategoryId()).ifPresent(v -> filters.add(boolFilter().must(termFilter("cat", v))));
        Optional.ofNullable(searchParams.getCurrency()).ifPresent(v -> filters.add(boolFilter().must(termFilter("curr", v))));
        Optional.ofNullable(searchParams.getTreeCategoryId()).ifPresent(v -> filters.add(boolFilter().must(termFilter("tcat", v))));
        Optional.ofNullable(searchParams.getUid()).ifPresent(v -> filters.add(boolFilter().must(termFilter("uid", v))));

       final BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();

        if (Optional.ofNullable(searchParams.getTitle()).isPresent()) {
            boolQueryBuilder.should(queryStringQuery(searchParams.getTitle()).analyzeWildcard(true).field("title"));
        }

        if (Optional.ofNullable(searchParams.getStartDateFrom()).isPresent()
                && Optional.ofNullable(searchParams.getStartDateTo()).isPresent()) {
            filters.add(rangeFilter("start_date").from(searchParams.getStartDateFrom()).to(searchParams.getStartDateTo()));
        }

        if (Optional.ofNullable(searchParams.getEndDateFrom()).isPresent()
                && Optional.ofNullable(searchParams.getEndDateTo()).isPresent()) {
            filters.add(rangeFilter("end_date").from(searchParams.getEndDateFrom()).to(searchParams.getEndDateTo()));
        }

        if (Optional.ofNullable(searchParams.getPriceFrom()).isPresent()
                && Optional.ofNullable(searchParams.getPriceTo()).isPresent()) {
            filters.add(rangeFilter("price").from(searchParams.getPriceFrom()).to(searchParams.getPriceTo()));
        }

        searchQuery.withPageable(pageable);
        searchQuery.withQuery(boolQueryBuilder);

        FilterBuilder[] filterArr = new FilterBuilder[filters.size()];
        filterArr = filters.toArray(filterArr);
        searchQuery.withFilter(andFilter(filterArr));

        final FacetedPage<AuctionIndex> search = auctionIndexRepository.search(searchQuery.build());

        Map response = new HashMap(); 


        response.put("data", search.map(index ->auctionRepository.findAuctionById(Long.valueOf(index.getId()))).getContent());
        response.put("totalElements", search.getTotalElements());
        response.put("totalPages", search.getTotalPages());
        response.put("currentPage", search.getNumber());
        return response;

    }
7
  • Can you show the exact URL you're using? Are you querying via curl? Sense? As far as I can see there is no /search endpoint in ES and you're missing the q=... parameter in order to filter on seller:335. Commented Mar 4, 2016 at 5:59
  • I don't see where you use size, page and sort in your code, i.e. how does your Pageable instance look like? Commented Mar 4, 2016 at 6:55
  • I added that example line to the service: searchQuery.withSort(SortBuilders.fieldSort("cat").order(SortOrder.ASC)); but it doesnt work result is the same like with out it. Commented Mar 4, 2016 at 7:28
  • Sorry after add this line I`v got error "Data too large, data for [cat] would be larger than limit" Commented Mar 4, 2016 at 7:35
  • Can you check if my other answer takes care of the problem? Commented Mar 4, 2016 at 7:40

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.