修复简单的Elasticsearch查询

时间:2017-12-25 20:34:19

标签: database elasticsearch search elasticsearch-dsl

我有以下数据:

{
   "results":[
      {
         "ID":"1",
         "products":[
            {
               "product":"car",
               "number":"5"
            },
            {
               "product":"computer",
               "number":"212"
            }
         ]
      },
      {
         "ID":"2",
         "products":[
            {
               "product":"car",
               "number":"9"
            },
            {
               "product":"computer",
               "number":"463"
            },
            {
               "product":"bicycle",
               "number":"5"
            }
         ]
      }
   ]
}

我的查询如下:

{  
   "query":{  
      "bool":{  
         "must":[  
            {  
               "wildcard":{  
                  "results.products.product":"*car*"
               }
            },
            {  
               "wildcard":{  
                  "results.products.number":"*5*"
               }
            }
         ]
      }
   }
}

我期望只获得ID1。因为只有它的产品有{"产品":"汽车","数字":" 5"记录。但我得到的是ID1和ID2,因为ID2的第一张唱片有"产品":" car"和第三个记录有"数字":" 5"单独记录。

如何修复此查询?

1 个答案:

答案 0 :(得分:3)

创建映射时,您需要将产品定义为add it as an output stream。尝试使用以下映射示例:

    try{
        $query =  $this->pdo->prepare("SELECT * FROM bookings WHERE TourID = ? AND dTourDate = ? and Status NOT LIKE 'Cancelled'");
        $query->execute(array($tourId,$date));
        $result = $query->fetchAll(PDO::FETCH_ASSOC);
        if(count($result)<1)
            $this->error("'Booking' Not Found.",$this->errCode->sqlNotBooking);
        $this->success("Booking List Success.",(array) $result);
    }

然后,您可以使用嵌套查询来匹配数组的整个元素 - 就像您需要的那样。

 TotalPrice":"0.00","GuestName":"Bryan Pedrochi<\/span>","ContactNumber":"042214"...