【问题标题】:How to combine bool must and sort for elasticsearch如何结合 bool must 和 sort 进行弹性搜索
【发布时间】:2015-11-22 18:22:15
【问题描述】:

我正在尝试对一些数据进行排序,在我的基本骨架中我的排序不起作用,如果我删除排序它工作正常。

那么我怎样才能在我的基础骨架中进行排序并对一些数据进行排序。

我不能只放

   $params['body'] = [           
    'sort' => [['title' => ['order' => 'asc']]]];
$results = $client->search($params);

因为我有其他条件需要必须条件。 谁能知道怎么解决。

任何建议将不胜感激。

      // my base skeleton          
      $params = array(
                'index' => "myIndex",
                'type' => "myType",
                'body' => array(
                    'query' => array(
                        'bool' => array(
                            'must' => array(
                            // empty should clause for starters
                            )
                        )
                    ),
                'sort' => array()
                )
            );

     // sorting is not working with bool and must
     if ($request->request->get('salarySort')) {
                $params['body']['query']['bool']['must'][] = array(
                    'sort' => array(
                        "title" => array('order' => 'asc')
                    )
                );
            }

这就是我得到的 json_encode ---

{
  "took": 4,
  "timed_out": false,
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "hits": {
    "total": 1066,
    "max_score": null,
    "hits": [
      {
        "_index": "myIndex",
        "_type": "myType",
        "_id": "pe065319de73937aa6ef46413afd7aac26a58a611",
        "_score": null,
        "_source": {
          "title": "Smarason trycker ",
          "content": "HIF gör 2-0 mot Halmstad.",
          "tag": [
            "Soprts"
          ],
          "category": [
            "Sports"
          ]
        },
        "sort": [
          "0"
        ]
      },
      {
        "_index": "myIndex",
        "_type": "myType",
        "_id": "pebc44a70008f53f74f23ab23f8a1f79b2b729448",
        "_score": null,
        "_source": {
          "title": "Anders Svenssons tips gav 1-0",
          "content": "Anders Svenssons tips i halvtid Kalmar FF.",
          "source": "Unknown",
          "tag": [
            "Soprts"
          ],
          "category": [
            "Sports"
          ]
        },
        "sort": [
          "0"
        ]
      }
    ]
  }
}

以 JSON 格式查询 ---

{
  "index": "myIndex",
  "type": "myType",
  "size": 30,
  "body": {
    "query": {
      "match_all": []
    },
    "sort": [
      {
        "title": "asc"
      }
    ]
  }
}

【问题讨论】:

    标签: php sorting elasticsearch


    【解决方案1】:

    你快到了。您已正确地将空的 sort 数组放置在与您的 query 相同的级别,这是正确的。

    稍后当您尝试将其作为 bool/must 约束而不是在空的 sort 数组中提供时,问题就会出现。

     // sorting is not working with bool and must
     if ($request->request->get('salarySort')) {
        $params['body']['sort'][] = array(      <---- this line needs to be changed
            "Salary" => 'asc'                   <---- this line needs to be changed, too
        );
     }
    

    【讨论】:

    • 非常感谢您的回答,但它没有按应有的排序,您能否再检查一次,再次感谢您的回答:)
    • @非常感谢您的回答 val :),尽管它不起作用并且没有对文档进行排序 :)
    • 您确定您的Salary 字段名称正确吗?您可以使用json_encode($params) 或类似方法打印出生成的查询吗?
    • 我已经更新了我的问题,你能看看吗:)
    • 所以当我按名称排序时,它不是按名称排序
    猜你喜欢
    • 1970-01-01
    • 2019-05-09
    • 2017-03-10
    • 2017-12-30
    • 1970-01-01
    • 2021-08-25
    • 1970-01-01
    • 2015-09-11
    • 1970-01-01
    相关资源
    最近更新 更多