Coder Social home page Coder Social logo

laravel-elasticsearch's Introduction

使用说明

生成 mapping properties

<?php
namespace App\Entities;

use Carbon\Carbon;
use Kabunx\Elastic\Hydrate\EsEntity;
use Kabunx\Elastic\Hydrate\EsProperty;
use Kabunx\Elastic\Exceptions\EsException;

class UserEntity extends EsEntity
{

    #[EsProperty("userId")]
    public int $id;

    public string $name;

    public ?Carbon $date;

    /**
     * @var array|UserAddressEntity[]
     */
     #[EsProperty(UserAddressEntity::class)]
    public $address = [];
}
<?php
use App\Entities\UserEntity;

(new UserEntity())->getProperties()

结果如下

---

如何检索

在orm中,可直接用DB门面类来执行MySQL操作,相当于实例化Illuminate\Database\Query\Builder类。

那么ES应该也可以这样操作,所以我也加入了一个Builder类,请参考kabunx/elasticsearch扩展

当然Model模型依赖可以按原来的方式操作,实例了EsBuilder,但最终都是通过Builder来构造查询参数

(new Builder())
    ->select(['id'])
    ->from('index')
    ->term('is_new', 1)
    ->should(function (Builder $builder) { // bool
        $builder->term('is_new', 1);
        $builder->gte('update_time', 1);
    }, true)
    ->should(function (Builder $builder) {
        $builder
            ->term('function_type', 20057)
            ->should(function (Builder $builder) {
                $builder->like('name', '销售工程师');
                $builder->like('name', '医疗');
            })
            ->should(function (Builder $builder) {
                $builder->like('name', '销售工程师xx');
            });
    })
    ->orderByDesc('id')
    ->limit(2)
    ->toSearchParams();

User::elasticsearch()
    ->term('is_new', 1)
    ->should(function (Builder $builder) { // bool
        $builder->term('is_new', 1);
        $builder->gte('update_time', 1);
    }, true)
    ->should(function (Builder $builder) {
        $builder
            ->term('function_type', 20057)
            ->should(function (Builder $builder) {
                $builder->like('name', '销售工程师');
                $builder->like('name', '医疗');
            })
            ->should(function (Builder $builder) {
                $builder->like('name', '销售工程师xx');
            });
    })
    ->orderByDesc('id')
    ->limit(2)
    ->toSearchParams();

最终打印的结果是(这里我仅贴了一个,唯一 的区别就是'index'不同)

array:3 [
  "index" => "gb_jd"
  "size" => 2
  "body" => array:2 [
    "query" => array:1 [
      "bool" => array:3 [
        "minimum_should_match" => 1
        "must" => array:1 [
          0 => array:1 [
            "term" => array:1 [
              "is_new" => array:1 [
                "value" => 1
              ]
            ]
          ]
        ]
        "should" => array:3 [
          0 => array:1 [
            "term" => array:1 [
              "is_new" => array:1 [
                "value" => 1
              ]
            ]
          ]
          1 => array:1 [
            "range" => array:1 [
              "update_time" => array:1 [
                "gte" => 1
              ]
            ]
          ]
          2 => array:1 [
            "bool" => array:3 [
              "minimum_should_match" => 1
              "must" => array:1 [
                0 => array:1 [
                  "term" => array:1 [
                    "function_type" => array:1 [
                      "value" => 20057
                    ]
                  ]
                ]
              ]
              "should" => array:2 [
                0 => array:1 [
                  "bool" => array:1 [
                    "must" => array:2 [
                      0 => array:1 [
                        "wildcard" => array:1 [
                          "name" => array:1 [
                            "value" => "*销售工程师*"
                          ]
                        ]
                      ]
                      1 => array:1 [
                        "wildcard" => array:1 [
                          "name" => array:1 [
                            "value" => "*医疗*"
                          ]
                        ]
                      ]
                    ]
                  ]
                ]
                1 => array:1 [
                  "wildcard" => array:1 [
                    "name" => array:1 [
                      "value" => "*销售工程师xx*"
                    ]
                  ]
                ]
              ]
            ]
          ]
        ]
      ]
    ]
    "sort" => array:1 [
      0 => array:1 [
        "id" => array:1 [
          "order" => "desc"
        ]
      ]
    ]
  ]
]

直接通过ES检索数据

User::elasticsearch()->rawQuery([])

获取数据

$esCollection = User::elasticsearch()->get()

laravel-elasticsearch's People

Contributors

libxj avatar

Stargazers

 avatar pengan avatar jianping lu avatar DanShu avatar Emily Johnson avatar Bruce Chu avatar anyforever avatar Talent Su avatar  avatar wilbur.yu avatar zhanghui avatar  avatar xxff avatar zengwei avatar Ewan Xiao avatar = - = avatar mengxw avatar Boyce avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.