cakephp / elastic-search

Elastic search datasource for CakePHP
Other
88 stars 53 forks source link

Add method for getting elastica query object #123

Closed ankr closed 7 years ago

ankr commented 7 years ago

Possible to get a hold on the elastica query object without compiling it.

ankr commented 7 years ago

I believe the failing tests were already failing befor this PR.

lorenzo commented 7 years ago

I think the method you are looking for is compileQuery()

ankr commented 7 years ago

@lorenzo In my specific case I would like to get it before it's compiled. I'm passing the returned value to Cake\ElasticSearch\Query::setFullQuery() after wrapping it in a HasParent query.

lorenzo commented 7 years ago

you can get the compiled one, wrap it and set it again

ankr commented 7 years ago

You are right @lorenzo. Must have done something else wrong previously.

ankr commented 7 years ago

Actually no, this isn't going to work. If I use the result from compileQuery in a hasParent my filters will be applied to parent documents, and not my current Type.

public function findFoo(Query $query, array $options)
{
  $query = $query->where(['foo' => 'bar']);

  $builder = new QueryBuilder();
  $parent = $builder->hasParent($query->compileQuery()->toArray(), 'user');

  return $query->setFullQuery($parent);
}

In the above case the ['foo' => 'bar'] condition will be applied to the parent user documents.

lorenzo commented 7 years ago

@ankr a problem with this PR is that the _elasticaQuery object is empty until you call compileQuery

ankr commented 7 years ago

Gonna close this PR as I managed to resolve my issue without this method. I still think it could could be useful being able to access the Elastica query without compiling it. But let's burn that bridge when we get to it.