FOSElasticaBundle
FOSElasticaBundle copied to clipboard
Query return objet with all field
Hello,
For example i launch query on object "USER" and user contain name, firstname, password(crypted). The result of elasticsearch is good, but i get my object with all field. I need for exemple to get just name and firstname in my object user ? How can i do that ?
That can be done by using source filtering https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-source-filtering.html
How can i disable source filtering in This bundle ?
With terms and filter ?
I tried this :
$query = new Query('toto'); $query->setSource('id');
But it doesn't work, i get all field. In elasticsearch console i try this :
{
"_source": "id",
"query":{
"bool":{
"must":[
{
"match_all":{
}
}
],
"must_not":[
],
"should":[
]
}
},
"from":0,
"size":10,
"sort":[
],
"aggs":{
}
}
And it's working, why it's the problem ?
I try to add this in my config _source: { enabled: true, includes: ['firstname']}
It's working if i launch query in console with elasticsearch but with the bundle.. doesn't work
Anyone for help ?
$query->setSource('id') should do the trick. What results are you getting?
`
public function searchUser(UserSearch $userSearch) {
if($userSearch->getRange() == null) {
$userSearch->setRange(30);
}
$boolFilter = new BoolQuery();
$queryAll = new Query\MatchAll();
$boolFilter->addMust($queryAll);
if($userSearch->getLat() !== null && $userSearch->getLng() !== null) {
$boolFilter->addMust(new GeoDistance("address.location", array('lat' => $userSearch->getLat(), 'lon' => $userSearch->getLng()), $userSearch->getRange()));
$query = new Query($boolFilter);
$query->setSource("id");
return $this->finder->find($query);
}
$query = new Query();
$query->setSource("id");
$query->setQuery($queryAll);
return $this->finder->find($query);
}`
With this two request, i get all field of my entity.. Any good answer for help ? :/
And with debug my request have exactly the same body with elasticsearch in console :
object(Elastica\Query)#914 (3) { ["_suggest":protected]=> int(0) ["_params":protected]=> array(2) { ["_source"]=> string(2) "id" ["query"]=> object(Elastica\Query\MatchAll)#638 (2) { ["_params":protected]=> object(stdClass)#878 (0) { } ["_rawParams":protected]=> array(0) { } } } ["_rawParams":protected]=> array(0) { } }
Maybe with not working with this bundle ? We need to fix that ?
Okay i have checking this method in TransformerFinder.php :
public function find($query, $limit = null, $options = [])
{
$results = $this->search($query, $limit, $options);
die(var_dump($results));
return $this->transformer->transform($results);
}
And the result it's ok i get only the ID of my entities. But if i use the transformer i get all field again... Maybe the transformer doesn't work ? Why i can fix that ? @XWB
Doctrine fetches all fields from the database. You should not use transformers if you want partial results. This can be done by using the the search() instead of the find() method.
Hello thank for answer, i have another problem :
I have this error if i use this search method :
<response>
<code>500</code>
<message>Param _version does not exist</message>
</response>
My code is :
$enableQuery = new Query\Match();
$enableQuery->setFieldQuery('enabled', true);
$query = new Query();
$query->setQuery($enableQuery);
$query->setSource('id');
return $this->get('fos_elastica.index.app.user')->search($query);
I'm not familiar with that error, though it seems to be coming from one of the libraries:
https://github.com/ruflin/Elastica/blob/master/lib/Elastica/Param.php#L156
Did you resolve the problem?
Hi, is there any solution for this? We struggle with the same problem.
As I see it, for 4 years the issue has not been resolved? I still cannot specify the names of the fields that should be returned?