Elasticsearch词条查询未给出任何结果 [英] Elasticsearch term query does not give any results

查看:78
本文介绍了Elasticsearch词条查询未给出任何结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am very new to Elasticsearch and I have to perform the following query:

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

根据Elasticsearch API,它等效于伪SQL:

According to the Elasticsearch API, it is equivalent to pseudo-SQL:

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

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

The problem is that my document looks like this:

{  
   "_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查询不会返回任何内容.

And the Elasticsearch query above doesn't return anything.

此查询返回上面的文档:

Whereas, this query returns the document above:

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

这使我怀疑两个字段中的标题"具有相同的名称这一事实是出于某种原因.

This makes me suspect that the fact that "title" is the same name for the two fields is for something.

是否有一种方法可以解决此问题,而不必重命名任何字段.还是我在其他地方想念它?

Is there a way to fix this without having to rename any of the fields. Or am I missing it somewhere else?

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

Thanks for anyone trying to help.

推荐答案

您的问题已得到描述我怀疑您的索引上没有任何显式映射,这意味着Elasticsearch将使用动态映射.

I suspect that you don't have any explicit mapping on your index, which means elasticsearch will use dynamic mapping.

对于字符串字段,它将通过

For string fields, it will pass the string through the standard analyzer which lowercases it (among other things). This is why your query doesn't work.

您的选择是:

  1. 在字段上指定显式映射,以便在存储到索引( index:not_analyzed )之前不进行分析.
  2. 在将术语查询发送到elasticsearch之前先对其进行清理(在这种特定的查询中,可以使用小写形式,但请注意,标准分析器还可以执行其他操作,例如删除停用词,因此取决于标题,您可能仍然会遇到问题).
  3. 使用其他查询类型(例如, query_string 代替 term ,它将在运行查询之前对其进行分析).
  1. Specify an explicit mapping on the field so that it isn't analyzed before storing in the index (index: not_analyzed).
  2. Clean your term query before sending it to elasticsearch (in this specific query lowercasing will work, but note that the standard analyzer also does other things like remove stop words, so depending on the title you may still have issues).
  3. Use a different query type (e.g., query_string instead of term which will analyze the query before running it).

查看要存储的数据种类,您可能需要指定一个显式的 not_analyzed 映射.

Looking at the sort of data you are storing you probably need to specify an explicit not_analyzed mapping.

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

For option three your query would look something like this:

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

请注意, query_string查询具有特殊的语法(例如,OR和AND不被视为文字),这意味着您必须小心输入.因此,使用术语过滤器的显式映射可能更适合您的用例.

Note that the query_string query has special syntax (e.g., OR and AND are not treated as literals) which means you have to be careful what you give it. For this reason explicit mapping with a term filter is probably more appropriate for your use case.

这篇关于Elasticsearch词条查询未给出任何结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆