弹性搜索 has_parent 查询

3

我正在尝试使用Elasticsearch的父子关系,并从fun-with-elasticsearch-s-children-and-nested-documents/中学习一些简单的例子。我可以通过在博客中运行查询来查询子元素。

curl -XPOST localhost:9200/authors/bare_author/_search -d '{

然而,我无法调整具有父项查询的示例。请问有人能指出我做错了什么,因为我一直得到0个结果。

这是我尝试过的内容:

#Returns 0 hits    
curl -XPOST localhost:9200/authors/book/_search -d '{
  "query": {
    "has_parent": {
      "type": "bare_author",
      "query" : {
        "filtered": {
          "query": { "match_all": {}},
          "filter" : {"term": {  "name": "Alastair Reynolds"}}            
          }
        }
      }
    }
  }'


#did not work either
curl -XPOST localhost:9200/authors/book/_search -d '{
"query": {
    "has_parent" : {
        "type" : "bare_author",
       "query" : {
        "term" : {
                "name" : "Alastair Reynolds"
            }
        }
    }
}
}' 

这适用于匹配,但只匹配了姓氏。
    #works but matches just first name
curl -XPOST localhost:9200/authors/book/_search -d '{
"query": {
    "has_parent" : {
        "type" : "bare_author",
       "query" : {
        "match" : {"name": "Alastair"}
        }
    }
  }
}' 

你应该首先尝试直接在 bare_author 类型上运行你的 term 查询或过滤器,以查看是否返回一些匹配项。这样你就可以确定问题是来自于 has_parent 还是 term 查询。 - mguillermin
1个回答

3
我想你正在使用默认映射,因此使用标准分析器分析名称字段。另一方面,术语查询和术语过滤器不支持文本分析,因此您搜索令牌Alastair Reynolds,而在索引中,alastairreynolds是两个不同的标记且小写。
匹配查询返回结果,因为它经过了分析,因此在小写下找到匹配项。您只需更改术语查询并将其变为匹配查询,即使有多个术语,它也会找到匹配项,因为在这种情况下,它将根据空格进行标记化,并根据提供的不同术语生成布尔或dismax查询。

1
你是正确的。为了解决这个问题,我删除了索引并使用not_analyzed重新创建了它。 - BSingh

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接