php - ElasticSearch查询以搜索父文档并认为至少有一个 child

标签 php laravel elasticsearch

我在elasticsearch中实现了父子关系船,父类型名称是participant_tests,其子类型名称是participant_test_question_answers,我对父类型有一些搜索条件,如下所示:

query": {
  "bool":{
    "should":[
      {
        "range":{
          "created_at":{
             "gte":"2016-01-01",
             "lte":"2016-05-11",
             "format":"YYYY-MM-dd"
           }
         }
       },
       {
         "terms":{
            "created_by.id":["1000001"]
         }
       },
       {
         "multi_match":{
            "query":"test human time",
             "fields":[
                "course_tests.course_id.course_no",
                 "participant_test_questions.answer_text",
                 "participants.first_name",
                 "participants.last_name",
                 "participants.promote_unique_user_id","result"
              ],
              "operator":"OR"
            }
          }
        ]
     }
  }

以上查询返回结果,但是如果我要检查has_child,则没有返回结果。
我在bool类之后添加了这些行。
"has_child": {
  "type": "participant_test_question_answers",
  "min_children": 1,
  "query": {
    "match_all": {}
  }
}

最佳答案

试试这个查询:

{
  "query": {
    "bool": {
      "should": [
        {
          "range": {
            "created_at": {
              "gte": "2016-01-01",
              "lte": "2016-05-11",
              "format": "YYYY-MM-dd"
            }
          }
        },
        {
          "terms": {
            "created_by.id": [
              "1000001"
            ]
          }
        },
        {
          "multi_match": {
            "query": "test human time",
            "fields": [
              "course_tests.course_id.course_no",
              "participant_test_questions.answer_text",
              "participants.first_name",
              "participants.last_name",
              "participants.promote_unique_user_id",
              "result"
            ],
            "operator": "OR"
          }
        }
      ],
      "must": [
        {
          "has_child": {
            "type": "participant_test_question_answers",
            "min_children": 1,
            "query": {
              "match_all": {}
            }
          }
        }
      ]
    }
  }
}

关于php - ElasticSearch查询以搜索父文档并认为至少有一个 child ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37161431/

相关文章:

php - 检查 Blade 模板中是否存在 session - Laravel

php - Laravel:无法访问 Eloquent 关系

full-text-search - msearch 不能与 bool 一起使用

elasticsearch - 在ElasticSearch中搜索查询分数详细信息

laravel - 验证数组中的多个文件

elasticsearch - 带有数组的Elasticsearch script_score

javascript - 如何将 html 转换为可动态或数据表

php - "SELECT"命令的准备语句失败,在 PHP 中使用 mysqli

javascript - 使用 jquery 将 radio 输入值发送到 php 文件

php - 搜索工作正常,只有第一个结果未显示