MontrealDevOne
MontrealDevOne

Reputation: 1044

Cakephp3 ElasticSearch Query Q

How do I make a query like

http://localhost:9200/index/businesses/_search?q=services

in Cakephp3 ElasticSearch

I have tried

$this->Businesses->find('all')->where(['*'=>'services']);

However I get no results.

Upvotes: 1

Views: 660

Answers (2)

MontrealDevOne
MontrealDevOne

Reputation: 1044

The more accurate answer is to use a builder

    $q = 'services'; 
    $businesses = $this->Businesses->find('all')->where(function ($builder) use($q) {
            return $builder->query(new \Elastica\Query\SimpleQueryString($q));
        });

The _all key might solve the problem

$this->Businesses->find('all')->where(['_all'=>'services']);

Upvotes: 1

Yosyp Schwab
Yosyp Schwab

Reputation: 171

Not sure what you're asking, but the asterisk in where(['*'=>'services']) should be a column name in your table schema/database.

Another common issue is that the result of find() is not the result of the query by design. See my answer to Cake PHP 3 needs limit option for find all method and also CakePHP 3 Cookbook: ElasticSearch — Searching Indexed Documents:

$query = $this->Articles->find()
    ->where([
        'title' => 'special',
        'or' => [
            'tags in' => ['cake', 'php'],
            'tags not in' => ['c#', 'java']
        ]
    ]);

// The query returns multiple rows which you can loop through
//  or alternatively you can call $query->all(); to get the object
//                                $query->toArray(); to get the array
foreach ($query as $article) {
    echo $article->title;
}

Upvotes: 0

Related Questions