ruflin / Elastica

Elastica is a PHP client for elasticsearch
http://elastica.io/
MIT License
2.26k stars 736 forks source link

the sort search is incorrect, I need help #2141

Closed taobali32 closed 1 year ago

taobali32 commented 1 year ago

[code]

class TestListIndex extends BaseIndex
{
    public $NAME = '';

    public function __construct(Client $client = null)
    {
        parent::__construct($client);
        $this->NAME = $this->getName();
    }

    public function getName($index = 'test_list_index')
    {
        return $this->getIndexName($index);
    }

    protected $mapping = [
        '_source' => [
            'enabled' => true
        ],

        'properties' => [
            'id' => [
                'type' => self::TYPE_INTEGER,
            ],
            'user_id' => [
                'type' => self::TYPE_INTEGER,
            ],
            'type' => [
                'type' => self::TYPE_INTEGER,
            ],
            'num' => [
                'type' => self::TYPE_DOUBLE,
            ],
            'before' => [
                'type' => self::TYPE_DOUBLE,
            ],
            'after' => [
                'type' => self::TYPE_DOUBLE,
            ],
            'created_at' => [
                'type' => 'date',
                "format" => self::FORMAT_DATETIME
            ],
            'updated_at' => [
                'type' => 'date',
                "format" => self::FORMAT_DATETIME
            ]
        ]
    ];
}
    #[GetMapping]
    public function list2()
    {
        $size = (int)$this->request->input('size', 2);
        $page = (int)$this->request->input('page', 0);

        $builder = jtarApp()->get(ClientBuilderFactory::class)->create();

        $client = $builder->setHosts([env('ES_HOST')])->build();

        $params = [
            'index' => 'shiyan_test_list_index', 
            'body' => [
                'sort' => [
                    'id' => [
                        'order' => 'desc'
                    ]
                ],
                'from' => $page,
                'size' => $size,
                'query' => [
                    'bool' => [
                        'filter' => [
                            'term' => [
                                'type' => 0
                            ]
                        ]
                    ]
                ],
//                'query' => [
//                    'bool' => [
//                        'must' => [
//                            'match' => [
//                                'type' => 0
//                            ]
//                        ]
//                    ]
//                ],
//                'track_scores' => true,
//                'preference' => '_local',
            ],
        ];

        $result = $client->search($params);

        return $this->success('list', $result);
    }

course

page = 10, size = 0 or page = 1,size = 100 The sorting is correct

page = 1, size = 10 The sorting is not right

expect

  1. How to print dsl
  2. What's my question