floriansemm / SolrBundle

Solr-Integration into Symfony and Doctrine2
http://floriansemm.github.io/SolrBundle
MIT License
123 stars 73 forks source link

Getting results from multiple Solr documents ordered by relevance #135

Closed mrmadhav closed 8 years ago

mrmadhav commented 8 years ago

Hi,

Does this bundle allow the ability to search for results from multiple Entities and get the result in order of relevance ?

E.G.

$query = $this->get('solr.client')->createQuery(['AppBundle:Article', 'AppBundle:Projects', 'AppBundle:Tasks', 'AppBundle:Wiki']);
$query->queryAllFields($searchTerm);
$query->setHydrationMode(HydrationModes::HYDRATE_INDEX);
$query->setUseWildcard(true);
$results = $query->getResult();
dump($results);

The reason I was thinking of going this way was that since my Entities have a lot of relationships, then creating a view to handle all the joins would result in an extremely long and unmanageable query.

Thanks Madhav

floriansemm commented 8 years ago

it is not possible to search for multiple entities

mrmadhav commented 8 years ago

Hi @floriansemm,

Thanks for your response. I have since then created a fork of the repository to do just that, i.e. query multiple documents at the same time.

You can find the fork here: https://github.com/mrmadhav/SolrBundle

Please let me know if this would be interesting to integrate into the main codebase or if you would want me to improve things beforehand ?

Best Regards, Madhav

floriansemm commented 8 years ago

Thanks! Great work.

The idea behind queries is to create query-string which results in a set of hydrated objects from the same type. So the whole query thing in this bundle is based on doctrine-queries.

It is already possible to query multiple documents. The result is a set of documents, not entities.

$selectQuery = $this->get('solr.client.adapter')->createSelect();
// queries all documents with field `text`
$selectQuery->setQuery('text_t:relation');

$result = $this->get('solr.client.adapter')->select($selectQuery, 'core1');

foreach ($result as $document) {

    echo '<hr/><table>';

    // the documents are also iterable, to get all fields
    foreach ($document as $field => $value) {
        // this converts multivalue fields to a comma-separated string
        if (is_array($value)) {
            $value = implode(', ', $value);
        }

        echo '<tr><th>' . $field . '</th><td>' . $value . '</td></tr>';
    }

    echo '</table>';
}
mrmadhav commented 8 years ago

Thanks ! I'll try this format also to see if it is better in terms of performance...

Best Regards, Madhav