【问题标题】:elasticsearch parent/child query logicelasticsearch父/子查询逻辑
【发布时间】:2017-10-31 20:06:05
【问题描述】:

弹性版本:5.0.1

定义映射:

PUT test
{
  "mappings": {
    "my_parent": {
      "properties": {
        "key": {
          "type": "keyword"
        }
      }
    },
    "my_child": {
      "_parent": {
        "type": "my_parent"
      },
      "properties": {
        "key": {
          "type": "keyword"
        }
      }
    }
  }
}

添加演示数据:

POST _bulk

{"update": {"_index": "test","_type": "my_parent","_id": "1"}}
{"doc": {"key": 1},"doc_as_upsert": true}

{"update": {"_index": "test","_type": "my_child","_parent": 1,"_id": "11"}}
{"doc": {"key": 11},"doc_as_upsert": true}

{"update": {"_index": "test","_type": "my_child","_parent": 1,"_id": "12"}}
{"doc": {"key": 12},"doc_as_upsert": true}

查询:

POST test/my_parent/_search
{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "should": [
            {
              "bool": {
                "must": [
                  {
                    "term": {
                      "key": 3
                    }
                  },
                  {
                    "has_child": {
                      "type": "my_child",
                      "inner_hits": {
                        "name": "a"
                      },
                      "query": {
                        "term": {
                          "key": 11
                        }
                      }
                    }
                  }
                ]
              }
            },
            {
              "has_child": {
                "type": "my_child",
                "inner_hits": {
                  "name": "b"
                },
                "query": {
                  "term": {
                    "key": 12
                  }
                }
              }
            }
          ]
        }
      }
    }
  }
}

结果:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0,
    "hits": [
      {
        "_index": "test",
        "_type": "my_parent",
        "_id": "1",
        "_score": 0,
        "_source": {
          "key": 1
        },
        "inner_hits": {
          "a": {
            "hits": {
              "total": 1,
              "max_score": 0.9808292,
              "hits": [
                {
                  "_type": "my_child",
                  "_id": "11",
                  "_score": 0.9808292,
                  "_routing": "1",
                  "_parent": "1",
                  "_source": {
                    "key": 11
                  }
                }
              ]
            }
          },
          "b": {
            "hits": {
              "total": 1,
              "max_score": 0.9808292,
              "hits": [
                {
                  "_type": "my_child",
                  "_id": "12",
                  "_score": 0.9808292,
                  "_routing": "1",
                  "_parent": "1",
                  "_source": {
                    "key": 12
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}

这里的问题:

'must'\'should'\'must_not' 子句在普通搜索和父\子搜索之间是否具有相同的含义?

为什么会返回名为'a'的inner_hits的结果?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    'must'|'should'|'must_not' 子句有不同的含义。让我用普通搜索的例子来解释一下。

    用等效的 SQL 查询来理解这些子句。

    必须:子句(查询)必须出现在匹配的文档中,并且会影响分数。

    SQL: select * from user where country_code = 'US' AND state_code = 'NY' 查询 DSL:

    POST _search
    {
        "query": {
            "bool": {
                "must": [
                    {"term": {"country_code": "US"}},
                    {"term": {"state_code": "NY"}}
                ]
            }
        }
    }
    

    应该:这些子句中至少有一个必须匹配,例如逻辑 OR。

    SQL: select * from user where country_code = 'US' OR state_code = 'NY'
    查询 DSL:

    POST _search
    {
        "query": {
            "bool": {
                "should": [
                    {"term": {"country_code": "US"}},
                    {"term": {"state_code": "NY"}}
                ]
            }
        }
    }
    

    must_not: 条件不得与文档匹配。
    SQL: select * from user where country_code != 'US' AND state_code != 'NY'
    查询 DSL:

    POST _search
    {
        "query": {
            "bool": {
                "must_not": [
                    {"term": {"country_code": "US"}},
                    {"term": {"state_code": "NY"}}
                ]
            }
        }
    }
    

    为什么会返回名为'a'的inner_hits的结果?

    因为您在 should 过滤器中放置了两个 has_child 条件。如上所述,它匹配来自 (inner_hits.name =a ..) 或 (inner_hits.name=b ..) 的文档

    【讨论】:

    • 感谢您的解释。让我困惑的是非父查询和父查询之间的子句行为。为什么返回名称为'a'的inner_hits的结果?
    • 查询转换为SQL应该是:select * from my_parent left join my_child where (my_parent.key=3 and my_child.key=11) or my_child.key=12,表示不返回key为11的child。
    猜你喜欢
    • 2021-12-03
    • 2016-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多