elasticsearch父/子查询逻辑

时间:2017-10-31 10:02:11

标签: elasticsearch

弹性版: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的结果?

1 个答案:

答案 0 :(得分:0)

'must'|'should'|'must_not'条款具有不同的含义。让我用简单搜索的例子来解释你。

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

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

SQL:从用户中选择* country_code ='US' AND state_code ='NY' 查询DSL:

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

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

SQL:从用户中选择* country_code ='US' OR state_code ='NY'
查询DSL:

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

must_not:条件必须与文档不匹配 SQL:从用户中选择* country_code != '美国' 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 ..)OR(inner_hits.name = b ..)的文档