laravel elasticseach和自定义字段映射

时间:2016-08-13 07:19:17

标签: php laravel elasticsearch

我正在使用laravel 4.1(相当古老的项目)和弹性搜索的Elasticquent插件。我有自定义字段映射的问题。我在管理面板中使用elasticsearch进行订单。这是问题所在:

<?php 

namespace App\Modules\Order\Models;

use Elasticquent\ElasticquentTrait;
class Order extends \Eloquent
{
  use ElasticquentTrait;

    /**
     * Table name
     * @var string
     */
    protected $table = 'order';

    protected $mappingProperties = array(
        'original_id' => [
          'type' => 'string',
        ],
        'client_id' => [
          'type' => 'string',
        ],
        'created' => [
          'type' => 'datetime'
        ],
        'login' => [
          'type' => 'string',
        ],
    );


    public function getIndexDocumentData()
    {
        $login = $this->client_id ? $this->client->where('id','=',$this->client_id)->get(['login'])[0]->login : null;
        return array(
            'id' => $this->id,
            'original_id'        => $this->original_id,
            'client_id'      => (int)$this->client_id,
            'created'      => $this->created,
            'login' => $login ? $login : 'niezarejestrowany',
        );
    }

    /**
     * Fields that can by field
     * @var array
     */
    protected $fillable = array('original_id', 'client_id', 'created', 'login');

    public function client()
    {
        return $this->belongsTo('App\Epro\Modules\Order\Models\Client');
    }
}

在我添加everthing到index:

之后
Order::addAllToIndex();

我使用searchByQuery方法获取所有订单:

$orders = Order::searchByQuery(['match_all' => []], null, null, 10, null, ['created'=>'asc']);

并尝试使用已创建的字段对所有内容进行排序。但是有一个问题 - 我的所有字段都是字符串类型。这是$ orders的var_dump

object(Elasticquent\ElasticquentResultCollection)[1846]
protected 'took' => int 20
protected 'timed_out' => boolean false
protected 'shards' => 
  array (size=3)
    'total' => int 5
    'successful' => int 5
    'failed' => int 0
protected 'hits' => 
  array (size=3)
    'total' => int 221
    'max_score' => null
    'hits' => 
      array (size=10)
        0 => 
          array (size=6)
            '_index' => string 'tim' (length=3)
            '_type' => string 'order' (length=5)
            '_id' => string '3011' (length=4)
            '_score' => null
            '_source' => 
              array (size=10)
                'id' => string '3011' (length=4)
                'original_id' => string '665885' (length=9)
                'shipment_id' => null
                'dealdate' => string '2016-07-25 11:56:00' (length=19)
                'login' => string 'gldka_76' (length=9)
                'account' => string 'carpets' (length=16)
                'document_status' => string '0' (length=1)
                'is_special' => null
                'payu_status' => string 'finished' (length=11)
                'allegro_payment' => string 'tt' (length=2)
            'sort' => 
              array (size=1)
                0 => string '00' (length=2)
        1 => 
          array (size=6)
            '_index' => string 'tim' (length=3)
            '_type' => string 'order' (length=5)
            '_id' => string '3092' (length=4)
            '_score' => null
            '_source' => 
              array (size=10)
                'id' => string '3092' (length=4)
                'original_id' => string '622334271' (length=9)
                'shipment_id' => null
                'dealdate' => string '2016-07-27 13:00:44' (length=19)
                'login' => string 'Klfa01' (length=9)
                'account' => string 'account2' (length=13)
                'document_status' => string '0' (length=1)
                'is_special' => null
                'payu_status' => string '' (length=0)
                'allegro_payment' => string 'odp' (length=21)
            'sort' => 
              array (size=1)
                0 => string '00' (length=2)

以下是订单按订单排序的示例:创建asc:

2016-07-25 11:56
2016-07-27 13:00
2016-07-25 00:39
2016-07-22 22:00
2016-07-28 00:16
2016-07-28 13:35

我认为这个错误的排序是因为var_dump字段中的_source中的这个字符串类型,但实际上不知道如何更改它 - 是否真的可以通过datetime字段进行排序?

0 个答案:

没有答案