gpt4 book ai didi

Elasticsearch term查询没有给出任何结果

转载 作者:行者123 更新时间:2023-11-29 02:47:58 29 4
gpt4 key购买 nike

我是 Elasticsearch 的新手,我必须执行以下查询:

GET book-lists/book-list/_search
{
"query":{
"filtered":{
"filter":{
"bool":{
"must":[
{
"term":{
"title":"Sociology"
}
},
{
"term":{
"idOwner":"17xxxxxxxxxxxx45"
}
}
]
}
}
}
}
}

根据Elasticsearch API,相当于伪SQL:

SELECT document
FROM book-lists
WHERE title = "Sociology"
AND idOwner = 17xxxxxxxxxxxx45

问题是我的文档看起来像这样:

{  
"_index":"book-lists",
"_type":"book-list",
"_id":"AVBRSvHIXb7carZwcePS",
"_version":1,
"_score":1,
"_source":{
"title":"Sociology",
"books":[
{
"title":"The Tipping Point: How Little Things Can Make a Big Difference",
"isRead":true,
"summary":"lorem ipsum",
"rating":3.5
}
],
"numberViews":0,
"idOwner":"17xxxxxxxxxxxx45"
}
}

上面的 Elasticsearch 查询没有返回任何内容。

鉴于此查询返回上面的文档:

GET book-lists/book-list/_search
{
"query":{
"filtered":{
"filter":{
"bool":{
"must":[
{
"term":{
"numberViews":"0"
}
},
{
"term":{
"idOwner":"17xxxxxxxxxxxx45"
}
}
]
}
}
}
}
}

这让我怀疑这两个字段的“title”是同名这一事实是有原因的。

有没有办法在不重命名任何字段的情况下解决这个问题。还是我在其他地方遗漏了它?

感谢任何试图提供帮助的人。

最佳答案

您的问题描述为in the documentation .

我怀疑您的索引上没有任何显式映射,这意味着 elasticsearch 将使用动态映射。

对于字符串字段,它将通过standard analyzer 传递字符串。将它小写(除其他外)。这就是您的查询不起作用的原因。

您的选择是:

  1. 在字段上指定一个显式映射,以便在存储到索引之前不对其进行分析 (index: not_analyzed)。
  2. 在将您的术语查询发送到 elasticsearch 之前清理它(在此特定查询中,小写字母会起作用,但请注意,标准分析器还会执行其他操作,例如删除停用词,因此根据标题,您可能仍然会遇到问题)。<
  3. 使用不同的查询类型(例如,query_string 而不是 term,后者将在运行查询之前对其进行分析)。

查看您正在存储的数据类型,您可能需要指定一个显式的 not_analyzed 映射。

对于选项三,您的查询将如下所示:

{  
"query":{
"filtered":{
"filter":{
"bool":{
"must":[
{
"query_string":{
"fields": ["title"],
"analyzer": "standard",
"query": "Sociology"
}
},
{
"term":{
"idOwner":"17xxxxxxxxxxxx45"
}
}
]
}
}
}
}
}

请注意 query_string query 有特殊的语法(例如,OR 和 AND 不被视为文字)这意味着你必须小心你给它的东西。因此,使用术语过滤器的显式映射可能更适合您的用例。

关于Elasticsearch term查询没有给出任何结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33053382/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com