简体   繁体   中英

Elastic Search PHP Client Date Range Query

I have this code what executes an query. I am using the official php elastic search library. https://github.com/elastic/elasticsearch-php

The field "Tijdsperiode" is in this format : ( 2016-01-30 00:00:00 ) ( YY-MM-DD HH:MM:SS )

  $params = [
    'index' => 'veenendaal2',
    'type' => 'passanten2',
    'body' => [
      'query' => [
        'match_all' => [],
        'filter' => [
          'range' => [
            'Tijdsperiode' => [
              'gte' => '2016-01-30 01:00:00',
              'lte' => '2016-01-30 08:00:00'
            ]
          ]
        ]
      ],
    ],
  ];

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

  var_dump($response);

I just wonder what the format need to be to get results between the 2 dates.

When i do this :

 $params = [
    'index' => 'veenendaal2',
    'type' => 'passanten2',
    'body' => [
        'query' => [
          'match_all' => [],
        ],
    ],
  ];

It's working fine but i need the results between the 2 dates!

I also tried this but with no result :

  $json = '{
    "query": {
      "bool": {
        "must": [
                {
                  "range": {
                  "Tijdsperiode": {
                    "gt": "2016-01-30 07:00:00",
                    "lt":  "2016-01-30 09:00:00"
                  }
                    }
                }
            ]
        }
    }
}';

  $client = ckan_graphmapper_client();

  $params = [
    'index' => 'veenendaal2',
    'type' => 'passanten2',
    'body' => $json
  ];

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

I also tried it with a mapping :

  $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'
            ]
          ]
        ]
      ]
    ]
  ];

How to do the right syntax for choosing between 2 dates?. Both string formats ( 2016-01-30 00:00:00 ) ( YY-MM-DD HH:MM:SS )

Thanks!

So it turns out my import of data was not mapping the correct way. The date field was recognised as a string value.

When i finnaly had the correct mapping in elastic i could do this query :

 $query = [
    'query' => [
      'filtered' => [
        'query' => [
          'match_all' => []
        ],
        'filter' => [
          'range' => [
            'Tijdsperiode' => [
              'gte' => $start,
              'lte' => $end
            ]
          ]
        ]
      ]
    ]
  ];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM