FOSElasticaBundle icon indicating copy to clipboard operation
FOSElasticaBundle copied to clipboard

Query return objet with all field

Open JimmyJMA opened this issue 7 years ago • 16 comments

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 ?

JimmyJMA avatar Dec 21 '17 19:12 JimmyJMA

That can be done by using source filtering https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-source-filtering.html

XWB avatar Dec 22 '17 14:12 XWB

How can i disable source filtering in This bundle ?

JimmyJMA avatar Dec 22 '17 14:12 JimmyJMA

With terms and filter ?

JimmyJMA avatar Dec 22 '17 14:12 JimmyJMA

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 ?

JimmyJMA avatar Dec 22 '17 15:12 JimmyJMA

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

JimmyJMA avatar Dec 22 '17 21:12 JimmyJMA

Anyone for help ?

JimmyJMA avatar Dec 29 '17 18:12 JimmyJMA

$query->setSource('id') should do the trick. What results are you getting?

XWB avatar Dec 31 '17 19:12 XWB

`
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 ? :/

JimmyJMA avatar Dec 31 '17 19:12 JimmyJMA

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 ?

JimmyJMA avatar Dec 31 '17 19:12 JimmyJMA

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

JimmyJMA avatar Dec 31 '17 20:12 JimmyJMA

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.

XWB avatar Jan 02 '18 08:01 XWB

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);

JimmyJMA avatar Jan 02 '18 09:01 JimmyJMA

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

XWB avatar Jan 02 '18 15:01 XWB

Did you resolve the problem?

paoloBirdOffice avatar Apr 09 '18 12:04 paoloBirdOffice

Hi, is there any solution for this? We struggle with the same problem.

konradja100 avatar Aug 01 '19 09:08 konradja100

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?

Evgeny1973 avatar May 05 '23 05:05 Evgeny1973