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

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条)

报告相同问题?

悬赏问题

  • ¥50 adb连接不到手机是怎么回事?
  • ¥20 抓取数据时发生错误: get_mooncake_data() missing 1 required positional argument: 'driver'的问题,怎么改出正确的爬虫代码?
  • ¥15 vs2022无法联网
  • ¥15 TCP的客户端和服务器的互联
  • ¥15 VB.NET操作免驱摄像头
  • ¥15 笔记本上移动热点开关状态查询
  • ¥85 类鸟群Boids——仿真鸟群避障的相关问题
  • ¥15 CFEDEM自带算例错误,如何解决?
  • ¥15 有没有会使用flac3d软件的家人
  • ¥20 360摄像头无法解绑使用,请教解绑当前账号绑定问题,