在Java elasticsearch中获取价格介于最小和最大之间的数据

时间:2018-08-02 11:05:57

标签: java elasticsearch

我是弹性搜索的新手,到目前为止,我正在阅读文档,但我无法编写一种通过范围获取方法,下面是一种通过ID获取的方法,该方法可以正常工作,但是我该怎么做以获得与价格范围相符的数据列表。

 public Map<String, Object> getTourById(String id){
        GetRequest getRequest = new GetRequest(INDEXTOUR, TYPETOUR, id);
        GetResponse getResponse = null;
        try {
            getResponse = restHighLevelClient.get(getRequest);
        } catch (java.io.IOException e){
            e.printStackTrace();
            e.getLocalizedMessage();
        }
        Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();
        return sourceAsMap;
    }

Above方法现在可以正常工作,下面是通过范围获取并返回与价格匹配的数据的方法

 public Map<String, Object> getTourByPriceRange(int minPrice, int maxPrice) {

        GetRequest getRequest = new GetRequest(INDEXTOUR, TYPETOUR, "requires an ID");

        QueryBuilder qb = QueryBuilders
                .rangeQuery("price")
                .from(minPrice)
                .to(maxPrice)
                .includeLower(true)
                .includeUpper(true);

        GetResponse getResponse = null;
        try {
            getResponse = restHighLevelClient.get(getRequest);
        } catch (java.io.IOException e) {
            e.printStackTrace();
            e.getLocalizedMessage();
        }
        Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();

        return sourceAsMap;
    }

只有查询生成器在上述方法中是准确的,我想要获得的结果是一堆价格在给定范围内的数据。

{
    "departure": {
      "city": "\u0438\u043d\u0441\u043a",
      "date": "2018-08-10"
    },
    "extras": [],
    "hotel": {
      "beach": {
        "distance": 0,
        "type": "\u041f\u0435\u0447\u0430\u043d\u044b\u0439"
      },
      "country": "\u0413\u0440\u0446\u0438\u044f",
      "distanceToAirport": 0,
      "facilities": [
        "Standard Without Balcony"
      ],
      "food": "\u0422\u043e\u043b\u044c\u043a\u043e \u0437\u0430\u0432\u0442\u0440\u0430\u043a\u0438",
      "photoUrl": "https://s1.tez-tour.com/hotel/7021893.jpg",
      "regionName": "\u0425\u0430\u043b\u043a\u0438\u0434\u0438\u043a\u0438 - \u041a\u0430\u0441\u0441\u0430\u043d\u0434\u0440\u0430",
      "stars": 4,
      "title": "KALLIKRATEIA"
    },
    "nights": 8,
    "people": 1,
    "price": 2595 // i want to use price as parameters for the search method
  }

1 个答案:

答案 0 :(得分:2)

到目前为止做得好!为了发出搜索请求,您需要使用SearchRequest而不是GetRequest,这是为了通过ID检索单个文档。

QueryBuilder qb = QueryBuilders
            .rangeQuery("price")
            .from(minPrice)
            .to(maxPrice)
            .includeLower(true)
            .includeUpper(true);

    SearchRequest searchRequest = new SearchRequest(INDEXTOUR); 
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); 
    searchSourceBuilder.query(qb); 
    searchRequest.types(TYPETOUR);
    searchRequest.source(searchSourceBuilder); 

    SearchResponse searchResponse = null;
    try {
        searchResponse = restHighLevelClient.search(searchRequest);
    } catch (java.io.IOException e) {
        e.printStackTrace();
        e.getLocalizedMessage();
    }