Skip to content

Instantly share code, notes, and snippets.

@ahmed-bhs
Last active October 12, 2018 22:59
Show Gist options
  • Save ahmed-bhs/1c91151b40edbf23bec78606a83d0c00 to your computer and use it in GitHub Desktop.
Save ahmed-bhs/1c91151b40edbf23bec78606a83d0c00 to your computer and use it in GitHub Desktop.

OFF DOC: https://github.com/Exercise/FOQElasticaBundle

Exemples utiles: https://hotexamples.com/examples/-/Elastica%255CQuery%255CMatch/setFieldFuzziness/php-elastica%255cquery%255cmatch-setfieldfuzziness-method-examples.html

    $finder = $this->container->get('fos_elastica.finder.app.job');
    // filter only activated and not expired
    $filter = new \Elastica\Query\BoolQuery();
    $filter->addMust(new \Elastica\Query\Term(array('isActivated' => true)));
    $rangeQuery = new \Elastica\Query\Range();
    $rangeQuery->addField('expiresAt', array('gt' => \Elastica\Util::convertDateTimeObject(new \DateTime())));
    $filter->addMust($rangeQuery);
    $query = \Elastica\Query::create($query);
    $query->setPostFilter($filter);
    $jobs = $finder->find($query);
    return $this->render('job/search.html.twig', array(
        'jobs' => $jobs,
    ));

$completion = new Suggest\Completion('search', 'name_suggest');
    $completion->setText($text);
    $completion->setFuzzy(array('fuzziness' => 2));
    $resultSet = $this->get('fos_elastica.index.app.restaurant')->search(Query::create($completion)
    

https://stackoverflow.com/questions/13285250/how-to-query-using-elastica http://elastica.io/getting-started/search-documents.html https://stackoverflow.com/questions/21909763/symfony2-foselasticabundle-update-index-for-all-entities-related-to-the-entity-u https://medium.com/@dragosholban/symfony-2-8-jobeet-day-16-search-321506e494e7 https://habr.com/post/229905/ https://devblog.lexik.fr/symfony2/autocompletion-avec-elasticsearch-2525 https://github.com/alfonmga/symfony.demo.on.roids

$elasticaClient = new \Elastica\Client(); // localhost:9200

/** @var $festivalIndex \Elastica\Index */
$festivalIndex = $elasticaClient->getIndex('festival');

/** @var $hellfestType \Elastica\Type */
$hellfestType = $festivalIndex->getType('hellfest');


Construire et lancer la requête

$matchQuery = new \Elastica\Query\Match();
$matchQuery->setField('name', 'Slayer');

// Not mandatory
$searchQuery = new \Elastica\Query();
$searchQuery->setQuery($matchQuery);

$resultSet = $hellfestType->search($matchQuery);


ET festival/hellfest/_search // getType()->search()

{
    "query": { // new \Elastica\Query()
        "match": { // new \Elastica\Query\Match()
            "name": "slayer" // setField()
        }
    }
}```

progressiv = new \Elastica\Query\Term(array( 'tags' => array('value' => 'progressive metal') ));```

    "bool": {
        "must": [
            // Queries to match
        ],
        "must_not": [
            // Queries to not match
        ]
    }
}
$year = new \Elastica\Query\Term(array(
    'year' => array('value' => 2014)
));

$grindcore = new \Elastica\Query\Term(array(
    'tags' => array('value' => 'grindcore')
));

$boolQuery = new \Elastica\Query\Bool();

$boolQuery->addMust($progressiv);
$boolQuery->addMust($year);
$boolQuery->addMustNot($grindcore);

$resultSet = $hellfestMappingType->search($boolQuery);
echo sprintf("Il y a %d groupes que tu aime au Hellfest %d\n", $resultSet->getTotalHits(), 2014);

{
    "size": 0,
    "aggs": {
        "by_year": {
            "terms": {
                "field": "year",
                "size": 50
            }
        }
    }
}

SELECT COUNT(*) FROM gigs GROUP BY year

$yearsAgg = new \Elastica\Aggregation\Terms("by_year");
$yearsAgg->setSize(50);
$yearsAgg->setField('year');

$search = new \Elastica\Query();
$search->setSize(0);
$search->addAggregation($yearsAgg);

$results = $type->search($search);
$years   = $results->getAggregation('by_year');

$matching = new Query\MultiMatch();
$matching->setQuery($terms);
$matching->setParam('use_dis_max', false);
$matching->setFields(array(
    'name',
    'bio',
    'pays',
    'job',
));

http://genieblog.ch/tag/elasticsearch/ http://obtao.com/blog/2014/02/indexation-et-recherche-simple-sur-elasticsearch-et-symfony/


http://www.christophe-meneses.fr/article/autocompletion-avec-elasticsearch-dans-une-application-symfony

https://www.foulquier.info/tutoriaux/installation-et-mise-en-place-du-bundle-foselastica-pour-symfony-2-3-et-elasticsearch-1-3

https://www.sodifrance.fr/blog/configurer-elasticsearch-sur-un-projet-symfony-2/

Sort

        $match = new MultiMatch;
        $match->setQuery($searchText);
        $match->setFields(['name', 'short_name']);
        $query = new Query($match);
        $query->setFrom(($page-1)*$limit)->setSize($limit);
        //PHP_INT_MAX used because '_first' triggers an integer overflow in json_decode on 32 bit...
        $query->setSort([
            'rank' => ['order' => 'desc', 'missing' => PHP_INT_MAX-1]
        ]);

https://github.com/thekudryash/prodvor.git Pagerfanata: https://github.com/serg-temchenko/homework_10/blob/b932ab94913c77a7f65dff462e3c0997d734832a/src/GeekHub/SpaceBlogBundle/Search/QuickSearch.php

Published true or False: https://github.com/devDzign/outletSF3/blob/79bef76d528002c9cbfce0189b70b284cc3072e7/src/UserBundle/SearchRepository/ArticleRepository.php

https://github.com/Lesbraslongs/Obtao/blob/c51d21974754bfc14ea660183773c81ec8266b22/src/Obtao/BlogBundle/Entity/SearchRepository/ArticleRepository.php

Sort: FriendsOfSymfony/FOSElasticaBundle#325

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment