简体   繁体   English

Elastic Search PHP客户端日期范围查询

[英]Elastic Search PHP Client Date Range Query

I have this code what executes an query. 我有这段代码执行查询。 I am using the official php elastic search library. 我正在使用官方的php弹性搜索库。 https://github.com/elastic/elasticsearch-php 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 ) 字段“ Tijdsperiode”的格式为:(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?. 如何在2个日期之间选择正确的语法? Both string formats ( 2016-01-30 00:00:00 ) ( YY-MM-DD HH:MM:SS ) 两种字符串格式(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 : 当我finnaly在elastic中具有正确的映射时,我可以执行以下查询:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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