GET /user_info/_search
{
  "explain": true, 
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "should": [
            {"range": {"age": {"gte": 18,"lte": 38}}},
            {"range": {"height": {"gte": 160,"lte": 175}}},
            {"range": {"weight": {"gte": 45,"lte": 65}}},
            {"match_phrase": {"roley_type": "VIP用户"}},
            {"match_phrase": {"roley_type": "VIP用户"}},
            {"match": {"emotional_state": 4}},
            {"match": {"purpose": "聊天,朋友,约会"}}
          ],
          "filter": [
            {"term": {"gender": "女"}},
            {"term": {"status": 0}},
            {"term": {"user_status": 999}}
          ]
        }
      },
      "functions": [
        {
          "gauss": {
            "age": {
              "origin": 22,
              "scale": 4,
              "offset": 2
            }
          },
          "weight": 10
        },
        {
          "gauss": {
            "height": {
              "origin": 160,
              "scale": 10,
              "offset": 5
            }
          },
          "weight": 10
        },
        {
          "gauss": {
            "weight": {
              "origin": 60,
              "scale": 10,
              "offset": 3
            }
          },
          "weight": 10
        }
      ],
      "score_mode": "sum",
      "boost_mode": "sum"
    }
  }
}
   public void seltest(){
       try {
           SearchRequest searchRequest = new SearchRequest("user_info");
           SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
           BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
           boolQueryBuilder.should(QueryBuilders.rangeQuery("age").gte(18).lte(38));
           boolQueryBuilder.should(QueryBuilders.rangeQuery("height").gte(160).lte(175));
           boolQueryBuilder.should(QueryBuilders.rangeQuery("weight").gte(45).lte(65));
           boolQueryBuilder.should(QueryBuilders.matchPhraseQuery("roley_type","VIP"));
           boolQueryBuilder.should(QueryBuilders.matchPhraseQuery("roley_type","VIP"));
           boolQueryBuilder.should(QueryBuilders.matchQuery("emotional_state",4));
//           String[] purposes = new String[]{"聊天","朋友","约会"};
//           List<String> purposesList = Arrays.asList(purposes);
           boolQueryBuilder.should(QueryBuilders.matchQuery("purpose","聊天,朋友,约会").operator(Operator.AND));
           boolQueryBuilder.filter(QueryBuilders.termsQuery("gender","女"));
           boolQueryBuilder.filter(QueryBuilders.termsQuery("status","0"));
           boolQueryBuilder.filter(QueryBuilders.termsQuery("user_status","999"));


           GaussDecayFunctionBuilder age = ScoreFunctionBuilders.gaussDecayFunction("age", 22, 4, 2).setWeight(10);
           GaussDecayFunctionBuilder height = ScoreFunctionBuilders.gaussDecayFunction("height", 160, 10, 5).setWeight(10);
           GaussDecayFunctionBuilder weight = ScoreFunctionBuilders.gaussDecayFunction("weight", 60, 10, 3).setWeight(10);
           FunctionScoreQueryBuilder.FilterFunctionBuilder[] filterFunctionBuilders =
                   new FunctionScoreQueryBuilder.FilterFunctionBuilder[3];

           filterFunctionBuilders[0] = new FunctionScoreQueryBuilder.FilterFunctionBuilder(age);
           filterFunctionBuilders[1] = new FunctionScoreQueryBuilder.FilterFunctionBuilder(height);
           filterFunctionBuilders[2] = new FunctionScoreQueryBuilder.FilterFunctionBuilder(weight);


           FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(boolQueryBuilder, filterFunctionBuilders).scoreMode(FunctionScoreQuery.ScoreMode.SUM).boostMode(CombineFunction.SUM);
           searchSourceBuilder.query(functionScoreQueryBuilder);

           //向搜索请求对象中设置搜索源
           searchRequest.source(searchSourceBuilder);
           //发送搜索请求
           log.info("查询准备     ");

           SearchResponse searchResponse = null;
           //此处未做ES Client的吊起 所以做try catch
           try {
               log.info("client为"+client);
               searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
           } catch (Exception e) {
               log.info("重新发起查询------");
               searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
           }

           log.info("查询已完成  - 查询社区首页    -- searchResponse "+searchResponse);
           SearchHit[] searchHits = searchResponse.getHits().getHits();
           if (searchHits == null || searchHits.length <= 0) {
               log.info("--------------搜索结果为空 ---------- ");
           }
           for (SearchHit hit : searchHits) {
               String sourceAsString = hit.getSourceAsString();
               log.info("当前查询结果 :" + JSON.toJSON(sourceAsString));
           }
       } catch (Exception e) {
           e.printStackTrace();
       }
   }
Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐