I am having hard times with Elastica and ruflin library for PHP to build the queries. There are no many examples I could follow.
I have User
entity in Symfony project which consists of the following fields:
firstName
, lastName
, emailAddress
, studio
and member
or client
(depended on what user it is)
So now I would like to filter out some results.
I want to display users only matching the certain studio:
$finder = $this->get('fos_elastica.finder.app.user'); $boolQuery = new BoolQuery(); $fieldQuery = new Match(); $fieldQuery->setFieldQuery('studio', 'StackOverflow studio'); $boolQuery->addMust($fieldQuery);
I want to display all users with name
John
:$fieldQuery = new MoreLikeThis(); $fieldQuery->setFields(['firstName', 'lastName', 'emailAddress']); $fieldQuery->setLikeText('John'); $boolQuery->addMust($fieldQuery);
I want to get members of that studio (not clients) Note: User has relation to
Client
andMember
entity. Depending on that, it will show either Member:Object or Client:Object. In this case I want to be sure that Member exists.$fieldQuery = new Filtered(); $fieldQuery->setFilter(new Exists('member')); $boolQuery->addMust($fieldQuery); $users = $finder->find($boolQuery);
The problem is that... it does not work. It shows me 0 results. No matter how I play with that it shows me 0 results or wrong results.
Can someone help me out how to build proper query using ruflin library for Elastica to get results based in the conditions I have mentioned above?
RAW QUERY:
{
"query":{
"bool":{
"must":[
{
"match":{
"studio":{
"query":"StackOverflow studio"
}
}
},
{
"filtered":{
"filter":{
"exists":{
"field":"member"
}
}
}
},
{
"more_like_this":{
"fields":[
"firstName",
"lastName",
"emailAddress"
],
"like_text":"John"
}
}
]
}
}
}
UPDATE 1:
I have manged to fix 3rd query (Exists('member')). It turned out that I have forgot to put client
and member
under mappings in config.yml
. Still 2nd query fails.