Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch range query ( PHP Client )

I have this search query :

 $params = [
    'index' => 'veenendaal2',
    'type' => 'passanten2',
    'size' => $size,
    'body' => [
      'query' => [
        "match_all" => [],
        'filter' => [
          'range' => [
            'Tijdsperiode' => [
              'gte' => '2016-01-30 01:00:00',
              'lte' => '2016-01-30 08:00:00'
            ]
          ]
        ]
      ],
      'mappings' => [
        '_default_' => [
          'properties' => [
            'Tijdsperiode' => [
              'type' => 'date',
              'format' => 'yyyy-MM-dd HH:mm:ss'
            ]
          ]
        ]
      ]
    ]
  ];

But i can't get it to filter results between the 2 dates?

Any ideas how to fix this? DO i need to change the syntax?

like image 235
Thomas Crawford Avatar asked Jun 29 '26 12:06

Thomas Crawford


1 Answers

You probably need to remove the mappings section from your search query as it doesn't belong there (i.e. it is only needed when creating your index, not when searching). Then you're probably missing a filtered query and that should be enough to get you some docs.

 $params = [
    'index' => 'veenendaal2',
    'type' => 'passanten2',
    'size' => $size,
    'body' => [
      'query' => [
        'filtered' => [
          'filter' => [
            'range' => [
              'Tijdsperiode' => [
                'gte' => '2016-01-30 01:00:00',
                'lte' => '2016-01-30 08:00:00'
              ]
            ]
          ]
        ]
      ]
    ]
  ];
like image 131
Val Avatar answered Jul 01 '26 02:07

Val



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!