1

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.

  1. 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);
    
  2. I want to display all users with name John:

    $fieldQuery = new MoreLikeThis();
    $fieldQuery->setFields(['firstName', 'lastName', 'emailAddress']);
    $fieldQuery->setLikeText('John');
    $boolQuery->addMust($fieldQuery);
    
  3. I want to get members of that studio (not clients) Note: User has relation to Client and Member 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.

undefinedman
  • 620
  • 1
  • 11
  • 25
  • @ruflin this one is for you ;-) – Val Sep 06 '16 at 11:18
  • There are no stupid questions so I'm going to ask one: "Did you index users?" – malcolm Sep 06 '16 at 12:17
  • Yes :) I have indexed users. It properly filters on studio (first query) but on MoreLikeThis and Exists does not, gives 0 results even though John does exist and member propery as well. – undefinedman Sep 06 '16 at 12:53

1 Answers1

1

Since there's no answer, I will answer myself. Instead of using MoreLikeThis query to find a piece of text, I have used QueryString and thing started to work

Here's how whole thing looks like now:

config.yml:

fos_elastica:
    ...
    indexes:
        app:
            types:
                user:
                    mappings:
                        emailAddress: ~
                        firstName: ~
                        lastName: ~
                        studio: ~
                        member: ~
                        client: ~
                    persistence:
                        driver: orm
                        model: AppBundle\Entity\User
                        provider: ~
                        listener:
                        finder: ~

Controller:

    $finder = $this->get('fos_elastica.finder.app.user');

    $boolQuery = new BoolQuery();

    $fieldQuery = new Match();
    $fieldQuery->setFieldQuery('studio', 'StackOverflow studio');
    $boolQuery->addMust($fieldQuery);

    $fieldQuery = new Filtered();
    $fieldQuery->setFilter(new Exists('member'));
    $boolQuery->addMust($fieldQuery);

    $fieldQuery = new QueryString();
    $fieldQuery->setFields(['firstName', 'lastName', 'emailAddress']);
    $fieldQuery->setQuery('John*'); // * wildcard will filter look a like results for you.
    $boolQuery->addMust($fieldQuery);

    $query = new Query();
    $query->setQuery($boolQuery);
    $query->addSort(['firstName' => 'asc']);

    $users = $finder->find($query);
undefinedman
  • 620
  • 1
  • 11
  • 25