简体   繁体   English

AND过滤弹性搜索PHP

[英]AND filter elastic search PHP

I'm trying to have multiple queries in ELASTIC SEARCH However I just want to have an operator AND to separate them as condition, for example as in SQL, 我正在尝试在ELASTIC SEARCH进行多个查询,但是我只想拥有一个运算符AND并将其作为条件分开,例如在SQL中,

SELECT * FROM table where P=1 AND Q=2

In such query, we find only those results which will fulfil both conditions. 在这种查询中,我们仅找到将同时满足这两个条件的结果。 In my case, P=1 is my first query and Q=2 is my second one. 在我的情况下,P = 1是我的第一个查询,而Q = 2是我的第二个查询。

How can I get the results for such query. 如何获得此类查询的结果。 Im using PHP. 我正在使用PHP。 Here's my Code: 这是我的代码:

// MY QUERY 1
$query["bool"]["must"][]["multi_match"] = array(
            "query"=>$string,
            "operator" => "and",
            "fields" => array("firstname", "lastname")
            );

// MY QUERY 2

$query["bool"]["must"][]["multi_match"] = array(
                 'query'  =>$string2,
                 'fields' => array("firstname1", "firstname2")
            );
// SEARCH

$return = $client->search(
                array(
                    'index' => 'my_index',
                    'type'  =>  'typ',
                    'body'  => array(
                        'query' => $query,
                        'size' => 100,
                    ),
                )
            );

You can use a filter query with an and operator like this. 您可以像这样使用带有and运算符的过滤查询。 Try it out. 试试看。

{
  "filter": {
    "and": [
      {
        "query": {
          "bool": {
            "must": [
              {
                "multi_match": {
                  "query": "searchTerm1",
                  "fields": [
                    "firstname",
                    "lastname"
                  ]
                }
              }
            ]
          }
        }
      },
      {
        "query": {
          "bool": {
            "must": [
              {
                "multi_match": {
                  "query": "searchTerm2",
                  "fields": [
                    "firstname1",
                    "firstname2"
                  ]
                }
              }
            ]
          }
        }
      }
    ]
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM