代码之家  ›  专栏  ›  技术社区  ›  Scott C Wilson Joachim Kiørbye Bertelsen

带有has\u子项的PHP-ElasticSearch复合查询

  •  0
  • Scott C Wilson Joachim Kiørbye Bertelsen  · 技术社区  · 7 年前

      $params = ['index' => 'products',
         'type' => 'product',
         'body' => ['query' =>
            ['match' => ['manufacturers_id' => $query],],
         ],
      ];
    

    但是,当我还想添加一个条件,即产品为银色,我已将其作为子记录添加到产品记录中,我得到一个语法错误:

     $params = ['index' => 'products',
         'type' => 'product',
         'body' => ['query' =>
            ['match' => ['manufacturers_id' => $query],],
            ['query' =>
              ['has_child' =>
                ['type' => 'attributes',
                ['query' =>
                  ['color' => 'Silver'],],
                ],
                ],
            ],
         ],
      ];
    

    {
        "error": {
            "col": 49,
            "line": 1,
            "reason": "Unknown key for a START_OBJECT in [0].",
            "root_cause": [
                {
                    "col": 49,
                    "line": 1,
                    "reason": "Unknown key for a START_OBJECT in [0].",
                    "type": "parsing_exception"
                }
            ],
            "type": "parsing_exception"
        },
        "status": 400
    }
    

    也尝试过

      $params = ['index' => 'products',
         'type' => 'product',
         'body' => ["query"=> [
                        "match"=> [
                            "manufacturers_id"=> [11]
                        ],
                        "has_child"=> [
                            "type"=> "attributes",
                            "query"=> [
                                "match"=> [
                                    "color"=> "silver"
                                ],
                            ],
                        ],
                    ],
         ],
      ];
    

    我得到“无法在1:39在START\u数组上获取文本”

    2 回复  |  直到 7 年前
        1
  •  1
  •   pawle    7 年前

    试试这个:

    "query"=> [
        "match"=> [
            "manufacturers_id"=> [1,2,3]
        ],
        "has_child"=> [
            "type"=> "attributes",
            "query"=> [
                "match"=> [
                    "color"=> "silver"
                ]
            ]
        ]
    ]
    

    我还推荐Sense,它是一个用于Chrome浏览器的插件,可以帮助编写ES查询。 See the screenshot

        2
  •  0
  •   Scott C Wilson Joachim Kiørbye Bertelsen    7 年前

    终于成功了。非常感谢@pawle对Sense的建议,这真的很有帮助。

      $params = ['index' => 'products',
         'type' => 'product',
         'body' =>
            [
               "query" => [
                  "bool" => [
                     "must" => [[
                        "has_child" => [
                           "type" => "attributes",
                           "query" => [
                              "match" => [
                                 "attributes_value" => "silver"
                              ]
                           ]
                        ]
                     ],
                        [
                           "match" => [
                              "manufacturers_id" => 4
                           ]
                        ]
                     ]
                  ]
               ]
            ],
      ];