FOSElastica 嵌套查询

FOSElastica nested query

我的collections是这样的:

  {
    "_index" : "test_index",
    "_type" : "test_type",
    "_id" : "10000",
    "_score" : 1.0,
    "_source" : {
      "user_id" : 12,
      "index_date" : {
        "date" : "2018-02-06 14:25:49.816952",
        "timezone_type" : 3,
        "timezone" : "UTC"
      },
      "rating" : null,
      "orders" : [          
        {
          "hour" : "08",
          "count" : 1
        },
        {
          "hour" : "10",
          "count" : 1
        }
      ],
      "products" : [
        {
          "p_id" : 970111,
          "count" : 4
        },
        {
          "p_id" : 1280811,
          "count" : 1
        },

      ]
    }
  },

并尝试访问 {"hour":"10"}

我的查询是:

            $query = new Query\Nested();
            $query->setPath('orders');

            $term = new Term();
            $term->setTerm('orders.hour', $order->getCreatedAt()->format('H'));
            $query->setQuery($term);
            dump($finder->find($query));die;

但我收到以下错误:

  [Elastica\Exception\ResponseException]                           
  failed to create query: {                                        
    "nested" : {                                                   
      "query" : {                                                  
        "term" : {                                                 
          "orders.hour" : {                                        
            "value" : "12",                                        
            "boost" : 1.0                                          
          }                                                        
        }                                                          
      },                                                           
      "path" : "orders",                                           
      "ignore_unmapped" : false,                                   
      "score_mode" : "avg",                                        
      "boost" : 1.0                                                
    }                                                              
  } [index: test_index] [reason: all shards failed]  

您的文档看起来不像嵌套查询。

我假设 finder 是您的 repository manager,它被定义为 orders 存储库,您的代码应该看起来像这样

$finder = $this->get('fos_elastica.repository_manager')->getRepository('YourBundle:order');

$boolquery = new Query\BoolQuery();

$term = new Query\Term();
$term->setTerm('hour', $order->getCreatedAt()->format('H'));

$boolquery->addMust($term);

$finder->find($boolquery);