简体   繁体   中英

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,

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.

How can I get the results for such query. Im using 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. 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"
                  ]
                }
              }
            ]
          }
        }
      }
    ]
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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