【问题标题】:FOSElastica nested queryFOSElastica 嵌套查询
【发布时间】:2018-07-16 17:55:46
【问题描述】:

我的收藏是这样的:

  {
    "_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]  

【问题讨论】:

    标签: php elasticsearch symfony-2.8 foselasticabundle


    【解决方案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);
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-12-25
    • 1970-01-01
    • 2019-07-18
    • 2021-01-29
    • 2020-08-01
    • 2014-07-19
    • 1970-01-01
    • 2023-04-11
    相关资源
    最近更新 更多