0
votes

Currently, I am trying to query the elasticsearch with should clause on multiple fields along with must clause in one field.

with SQL I would write this query:

SELECT * FROM test where ( titleName='Business' OR titleName='Bear') AND (status='Draft' OR status='Void') AND creator='bob'

I tried this:

$params = [
    'index' => myindex,
    'type' => mytype,
    'body' => [
        "from" => 0,
        "size" => 1000,
        'query' => [
            'bool' => [
           'must' => [
            'bool' => [
              'should' => [
                   ['match' => ['titleName' => 'Business']],
                   ['match' => ['titleName' => 'Bear']]
              ]
             ],
             'should' => [
                   ['match' => ['status' => 'Draft']],
                   ['match' => ['status' => 'Void']]
                ]
              'must' => [
                   ['match'] => ['creator' => 'bob']
              ]

        ]
        ]
       ]
    ]
];

The above query string working with single status field or single title field. But it's not working with both the fields.

Does anyone have a solution?

2

2 Answers

1
votes

You need to AND both of your bool/should pairs. This should work:

$params = [
        'index' => myindex,
        'type' => mytype,
        'body' => [
            "from" => 0,
            "size" => 1000,
            'query' => [
                'bool' => [
                  'must' => [
                    [
                      'bool' => [
                        'should' => [
                           ['match' => ['titleName' => 'Business']],
                           ['match' => ['titleName' => 'Bear']]
                        ]
                      ]
                    ],
                    [
                      'bool' => [
                        'should' => [
                           ['match' => ['status' => 'Draft']],
                           ['match' => ['status' => 'Draft']]
                        ]
                      ]
                    ],
                    [
                       'match' => ['creator' => 'bob']
                    ]
                  ]
                ]
            ]
        ]
    ];
0
votes

You can write your query something like this. Add a Must inside that you add Should

{
    "query": {
        "filter": {
            "bool": {
                "must": [{
                        "bool": {
                            "should": [{
                                    "term": {
                                        "titleName": "business"
                                    }
                                },
                                {
                                    "term": {
                                        "titleName": "bear"
                                    }
                                }
                            ]
                        }
                    },
                    {
                        "bool": {
                            "should": [{
                                    "term": {
                                        "status": "draft"
                                    }
                                },
                                {
                                    "term": {
                                        "status": "void"
                                    }
                                }
                            ]
                        }
                    },
                    {
                        "bool": {
                            "must": [{
                                "term": {
                                    "creator": "bob"
                                }
                            }]
                        }
                    }
                ]
            }

        }
    },
    "from": 0,
    "size": 25
}