java 操作es 实现俩个字段之间进行比对

sql 例如:

select * from user t where t.count = t.num;

es 查询语句如下:

{
"from": 0,
"size": 100,
"query": {
"bool": {
"filter": {
"script": {
"script": {
"inline": "doc['category_code'].value - doc['user_group_id'].value == 531",
"lang": "painless"
},
"boost": 1
}
}
}
}
}

javaApi 如下:

 Map params = new HashMap<>();
        Script script = new Script(ScriptType.INLINE,"painless","doc['category_code'].value - doc['user_group_id'].value == 864",params);
        ScriptQueryBuilder scriptQueryBuilder = new ScriptQueryBuilder(script);
        StringBuffer queryString = new StringBuffer();
//        queryString.append("goods_id:18506");
        QueryStringQueryBuilder queryStringQueryBuilder = new QueryStringQueryBuilder(queryString.toString());
        BoolQueryBuilder booleanQueryBuilder = QueryBuilders.boolQuery();
        booleanQueryBuilder.must(scriptQueryBuilder);
        if (queryString.length() > 0){
            booleanQueryBuilder.must(queryStringQueryBuilder);
        }
        System.out.println("查询条件:" + booleanQueryBuilder);
        SearchResponse searchResponse = getClient().prepareSearch("group_catagory_goods_recommend")
                .setTypes("goods_tags")
                .setQuery(booleanQueryBuilder)
                .get();
        SearchHit[] hits = searchResponse.getHits().getHits();
        List list = new ArrayList();
        for (int i = 0; i < hits.length; i++) {
            System.out.println(JSON.toJSON(hits[i]));
//            list.add(hits[i].getId());
        }

你可能感兴趣的:(java,es)