2
votes

I have created a small dataset (currently only 8 items) in an Elastic Search index.

The entries are structured as follows

{
    "value_a": "Foo",
    "value_b": "Bar",
    "value_c": "Baz"
}

In Elastic Search this then looks as follows (taken from querying my Elastic Search endpoint directly) :

{
    "_index": "foo_bar_bazindex",
    "_type": "foo_bar_baz",
    "_id": "4",
    "_score": 1,
    "_source": {
        "value_a": "Foo",
        "value_b": "Bar",
        "value_c": "Baz"
    }
}

The combinations of value a, b and c are unique.

I want to find the values of "value_c" by performing a bool filtered search with the values a and b.

In my code I have been trying this as follows:

    $filter = new \Elastica\Filter\Bool();
    $query = new \Elastica\Query();

    $aFilter = new \Elastica\Filter\Term(['value_a' => "Foo"]);
    $bFilter = new \Elastica\Filter\Term(['value_b' => "Bar"]);

    $filter->addMust($aFilter);
    $filter->addMust($bFilter);

    $query->setFilter($filter);

    $results = $this->bookingPeriodIndex->search($query)->getResults();

    var_dump($results);

However this returns no results (the var dump outputs an empty array) - also manually trying this query by posting this query directly to the server:

{
    "query": {
        "filtered": {
            "filter": {
                "bool": {
                    "must": [
                        {
                            "term": {
                                "value_a": "Foo"
                            }
                        },
                        {
                            "term": {
                                "value_b": "Bar"
                            }
                        }
                    ]
                }
            }
        }
    }
}

This also yields no results - I would however expect it to return one result as I have the following entry:

{
    "value_a": "Foo",
    "value_b": "Bar",
    "value_c": "Baz"
}

Am I missing something? Could it be due to the small dataset? We use the same Bool Filter Elastica queries elsewhere in our codebase and these work as expected, however I cannot seem to get any data to return from this index.

Any help would be appreciated.

1

1 Answers

1
votes

In the end I got the results I was looking for by using a bool query with two match statements:

The raw query:

{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "value_a": "Foo"
                    }
                },
                {
                    "match": {
                        "value_b": "Bar"
                    }
                }
            ]
        }
    }
}

The query using Elastica:

$query = new Query;
$booleanQuery = new Bool();

$fooMatch = new Match();
$fooMatch->setField('value_a', 'Foo');
$booleanQuery->addMust($fooMatch);

$barMatch = new Match();
$barMatch->setField('value_b', 'Bar');
$booleanQuery->addMust($barMatch);

$query->setQuery($booleanQuery);

$results = $this->index->search($query)->getResults();