弹性搜索PHP客户端日期范围查询

时间:2016-03-18 19:18:03

标签: php elasticsearch

我有这个代码执行查询。 我正在使用官方的php弹性搜索库。 https://github.com/elastic/elasticsearch-php

字段" 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);

我只是想知道在两个日期之间获得结果需要什么格式。

当我这样做时:

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

它工作正常,但我需要两个日期之间的结果!

我也尝试了这个但没有结果:

  $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);

我也尝试过映射:

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

如何在两个日期之间选择正确的语法?两种字符串格式(2016-01-30 00:00:00)(YY-MM-DD HH:MM:SS)

谢谢!

1 个答案:

答案 0 :(得分:0)

事实证明我导入的数据并没有以正确的方式进行映射。 日期字段被识别为字符串值。

当我在弹性中有正确的映射时,我可以进行以下查询:

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