FOSElastica嵌套查询

时间:2018-02-06 15:52:14

标签: php elasticsearch symfony-2.8 foselasticabundle

我的收藏品是这样的:

  {
    "_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
        },

      ]
    }
  },

并尝试访问{"小时":" 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]  

1 个答案:

答案 0 :(得分:1)

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

我认为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);