elasticsearch在查询条件中查询两个字段值相同的数据

背景

在查询条件中计算两个字段相等的条件用SQL查出来的方式如下:

select * from area t where area_id = upper_area_id;

es 查询语句如下

示例:

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

javaApi如下

Map<String, Object> params = new HashMap<>();
String idOrCode = "doc['area_id'].value - doc['upper_area_id'].value == 0";
Script script = new Script(ScriptType.INLINE, "painless", idOrCode, params);
ScriptQueryBuilder scriptQueryBuilder = new ScriptQueryBuilder(script);

BoolQueryBuilder builder = QueryBuilders.boolQuery();
builder.must(scriptQueryBuilder);

SearchResponse searchResponse = getClient().prepareSearch("goods_recommend")
        .setTypes("tags")
        .setQuery(builder)
        .get();

  

本篇文章如有帮助到您,请给「翎野君」点个赞,感谢您的支持。

首发链接:https://www.cnblogs.com/lingyejun/p/17557154.html

posted @ 2023-07-15 22:43  翎野君  阅读(1022)  评论(0编辑  收藏  举报