PHP code example of brokerexchange / elasticscout

1. Go to this page and download the library: Download brokerexchange/elasticscout library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

brokerexchange / elasticscout example snippets


    use ElasticScout\Searchable;
 

    //create search/query object
    $search = $article->search()
        ->boolean()
        ->should(DSL::match('title',$request->input('query')))
        ->should(DSL::match('body',$request->input('query')))
        ->highlight(['body','title'])
        ->filter(DSL::term('published', 1))
        ->aggregate(Agg::terms('categories', 'category.name'));
        
    //fire the search
    $articles = $search->paginate();
        
    //retrieve aggregation results
    $categories = $search->aggregation('categories');
    
    //retrieve highlight results for title field of first result article
    $firstArticleTitleHighlights = $articles->first()->highlight('title');
    
 

    public function mappings()
    {
        return [
            $this->searchableType() => [
                'properties' => [
                    'name' => [
                        'type' => 'text',
                        'fields' => [
                            'keyword' => [
                                'type' => 'keyword',
                            ],
                            'autocomplete' => [
                                'type' => 'text',
                                'analyzer' => 'autocomplete',
                                'search_analyzer' => 'autocomplete_search',
                            ],
                        ],
                    ],
                ]
            ]
        ]
    }
 

    public function settings()
    {
      return [
          'index' => [
              'analysis' => [
                  'analyzer' => [
                      'autocomplete' => [
                          'tokenizer' => 'autocomplete',
                          'filter' => [
                              'lowercase',
                          ],
                      ],
                      'autocomplete_search' => [
                          'tokenizer' => 'lowercase',
                      ],
                  ],
                  'tokenizer' => [
                      'autocomplete' => [
                          'type' => 'edge_ngram',
                          'min_gram' => 1,
                          'max_gram' => 15,
                          'token_chars' => [
                              'letter'
                          ]
                      ]
                  ]
              ],
          ],
      ];
    }