elasticsearch使用script进行两个字段比较搜索范围

一般用elasticsearch进行简单的范围查找是容易的,

比如:查找期末考试分数>80的学生;查找期末考试分数在60到70的学生;
通过elasticsearch的range可方便的查询

GET /forum/article/_search
{
  "query": {
    "constant_score": {
      "filter": {
        "range": {
          "fengshu": {
            "gt": 60,
            "lt": 70
          }
        }
      }
    }
  }
}

gte 大于等于,gt大于
lte 小于等于,lt小于

但是,在实际中会遇到字段的比较,
比如,查找满足以下任一条件的文档:
1.资产峰值 >净资产 * 200%
2.市值 >持仓市值 * 150%

这个时候就需要用到脚本,
es查询语句如下:

POST /customer/_search
{
  "query": {
    "bool": {
      "should": {
        "script": {
          "script": {
            "inline": "doc['zichan_max'].value>doc['jinzichan'].value*2"
            , "lang": "painless"
          },          
          "script": {
            "inline": "doc['shizhi'].value>doc['holding_shizhi'].value*1.5"
            , "lang": "painless"
          }
        }
      }
    }
  }
}

核心代码,第一层script里 面可以套一个script, 多个script中间用,分隔即可,其中inline表示脚本写在查询语句内,“lang”:"painless"表示脚本语言为painless

java中如下:

 SearchRequest searchRequest = new SearchRequest("customer");
        searchRequest.types("customer_info");
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();

        Script script = new Script("doc['zichan_max'].value>doc['jinzichan'].value*2");
        Script script1 = new Script("doc['shizhi'].value>doc['holding_shizhi'].value*2");
        ScriptQueryBuilder filterBuilder = QueryBuilders.scriptQuery(script);
        ScriptQueryBuilder filterBuilder1 = QueryBuilders.scriptQuery(script1);
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
        boolQueryBuilder.should(filterBuilder).should(filterBuilder1);
        searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));
        searchSourceBuilder.query(boolQueryBuilder);
        searchSourceBuilder.fetchSource(new String[]{"customer_no", "customer_name", "sex_name", "customer_status_name",
                "department_name", "fund_account", "risk_tolerance_name", "risk_preference_name", "risk_characteristics_name"}, null);
        searchSourceBuilder.size(20);
        searchRequest.source(searchSourceBuilder);
        PotentialCustomersResponse.Builder builder = PotentialCustomersResponse.newBuilder();
        try {
            SearchResponse response = restHighLevelClient.search(searchRequest);
            EsResult<SearchHits> result = this.getResult(response);
            builder.setResult(JSONObject.toJSONString(result));
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            responseObserver.onError(new StatusException(Status.INTERNAL));
            return;
        }

相关资料:
es脚本的使用
es详解
elasticsearch painless最强教程
painless脚本应用及与elasticsearch,java的结合使用

你可能感兴趣的:(elasticsearch,elasticsearch,script)