douzhouhan4618 2019-06-24 10:47
浏览 746
已采纳

Elasticsearch [Query Bool必须匹配]执行OR运算而不是AND

I'm trying to perform a basic login operation where my view (front end part) accepts a username and password through a form

So in SQL, I must have an example query:

SELECT * FROM users WHERE username = $_POST['username'] AND password = $_POST['password'];

According to the official documentation of Elasticsearch PHP API, it must go like this:

$params = [
'index' => 'myIndex',
'type'  => 'myType',
'body'  => [
        'query' => [
            "bool" => [
                "must" => [
                    "match" => [ 
                        "username" => 'email@email.com',
                    ],
                    "match" => [
                        "password" => 'mypassword',
                    ],
                ]
            ]
        ]
    ]
];

Unfortunately, it is displaying A LOT of documents so I presumed it's performing the OR operator instead of matching them together

enter image description here

FYI, if you would ever wonder why would there be so many documents displayed according to the "hits" property above, there are literally many user documents with the same password

Main Question
Is there any proper ES query to properly match my username AND password so I could only retrieve one document? I've been searching through with the official documentation, but nothing succeeds the desired output

Thank you very much!

  • 写回答

2条回答 默认 最新

  • drnycqxwz63508434 2019-06-24 11:02
    关注

    With the help of sir Val, I was able to formulate a technique to try work-arounds for my query, and was able to display the result with the following:

    $params = [
    'index' => $index,
    'type'  => 'index',
    'size'  => 250,
    'body'  => [
            'query' => [
                'bool' => [
                    'must' => [
                        [
                            "match" => [
                                "usr_username" => 
                                    [
                                        "query" => $username,
                                        "operator" => "and"
                                    ]
                            ]
                        ],
                        [
                            "match" => [
                                "usr_password" => [
                                    "query" => $password,
                                    "operator" => "and"
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ]
    ];
    

    enter image description here

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog