PHP code example of boyfoo / elasticsearch-query

1. Go to this page and download the library: Download boyfoo/elasticsearch-query 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/ */

    

boyfoo / elasticsearch-query example snippets


use Boyfoo\ElasticsearchSql\Search;
use Boyfoo\ElasticsearchSql\Query;

// 创建查询
$params = Search::create()
            ->index('goods')
            ->source(['no', 'price', 'category'])
            ->size(10)
            ->query(function (Query $query) {
                $query->mustMatch("小米手机")->mustTerm('category', '电子产品');
            });

[
  "index" => "goods",
  "type" => "_doc",
  "body" => [
    "_source" => ["no", "name", "price", "category"],
    "size" => 10,
    "query" => [
      "bool" => [
        "must" => [
          [
            "match" => ["name" => "小米手机"]
          ],
          [
            "term" => [
              "category" => ["value" => "电子产品"]
            ]
          ]
        ]
      ]
    ]
  ]
];

use Elasticsearch\ClientBuilder;

...

$client = ClientBuilder::create()->fromConfig($config);

$client->search($params);

use Boyfoo\ElasticsearchSql\Query;

$query = Query::create()
            ->mustMatch('字段1', '内容1')
            ->notTerm('字段1', '内容2')
            ->shouldRange('字段3', [
                '>=' => 2018, '<=' => 2019
            ]);

[
  "bool" => [
    "must" => [
      [
        "match" => [
          "字段1" => "内容1"
        ]
      ]
    ],
    "must_not" => [
      [
        "term" => [
          "字段1" => [
            "value" => "内容2"
          ]
        ]
      ]
    ],
    "should" => [
      [
        "range" => [
          "字段3" => [
            "gte" => 2018,
            "lte" => 2019
          ]
        ]
      ]
    ]
  ]
];

use Boyfoo\ElasticsearchSql\Search;
use Boyfoo\ElasticsearchSql\Query;

$query = Query::create()->mustTerms('key', 'value');

Search::create()->query($query);