Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用or()发现DSL被篡改了 #52

Closed
Dreamroute opened this issue Jan 19, 2023 · 1 comment
Closed

使用or()发现DSL被篡改了 #52

Dreamroute opened this issue Jan 19, 2023 · 1 comment

Comments

@Dreamroute
Copy link

Dreamroute commented Jan 19, 2023

  • 目的:实现类似这样的sql:where (id = 1) and (id = 1 and id in (1, 2)) and (id = 1 or id in (1, 2, 3))

  • 单元测试如下:

        @Test
        void m1() {
    
            // String sql = "where (id = 1) and (id = 1 and id in (1, 2)) and (id = 1 or id in (1, 2, 3))";
    
            LambdaEsQueryWrapper<User> w = lambdaQuery(User.class)
                    .eq(User::getId, 1L)
                    .and(i -> i
                            .eq(User::getId, 1L)
                            .in(User::getId, newArrayList(1L, 2L)))
                    .and(i -> i
                            .eq(User::getId, 2L)
                            .or()
                            .in(User::getId, newArrayList(1L, 2L, 3L)));
    
            List<User> users = userMapper.selectList(w);
            System.err.println(users);
        }
  • 控制台打印的DSL(json较长,去掉了不影响说明问题的部分),下方的should和must错乱了,出问题的地方下方我使用了注释标记

    {
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "_id": {
                  "value": 1,
                  "boost": 1.0
                }
              }
            },
            {          
              "bool": {
                // 理论上此处应该是must
                "should": [
                  {
                    "term": {
                      "_id": {
                        "value": 1,
                        "boost": 1.0
                      }
                    }
                  },
                  {
                    "terms": {
                      "_id": [
                        1,
                        2
                      ],
                      "boost": 1.0
                    }
                  }
                ],
                "adjust_pure_negative": true,
                "boost": 1.0
              }
            },
            {
              "bool": {
                // 理论上此处应该是should
                "must": [
                  {
                    "term": {
                      "_id": {
                        "value": 1,
                        "boost": 1.0
                      }
                    }
                  },
                  {
                    "terms": {
                      "_id": [
                        1,
                        2,
                        3
                      ],
                      "boost": 1.0
                    }
                  }
                ],
                "adjust_pure_negative": true,
                "boost": 1.0
              }
            }
          ]
        }
      }
    }
  • 假如我把wrapper中的第一个and删掉,变成如下,那打印出来的DSL语句又变成正确的了

        @Test
        void m2() {
    
            // String sql = "where (id = 1) and (id = 1 or id in (1, 2, 3))";
    
            LambdaEsQueryWrapper<User> w = lambdaQuery(User.class)
                    .eq(User::getId, 1L)
                    .and(i -> i
                            .eq(User::getId, 2L)
                            .or()
                            .in(User::getId, newArrayList(1L, 2L, 3L)));
    
            List<User> users = userMapper.selectList(w);
            System.err.println(users);
        }
@xpc1024
Copy link
Collaborator

xpc1024 commented May 17, 2023

https://www.easy-es.cn/pages/17ea0a/
已修复并提供四种嵌套查询

@xpc1024 xpc1024 closed this as completed May 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants