如何在elasticsearch中通过数组元素进行搜索?
我有一个在 elasticsearch 中索引的文档:
{
"content":"Some content with @someone mention",
"mentions":["someone"],
"userId":"4dff31eaf8815f4df04e2d62"
}
我尝试使用查询找到它:
{
"query": {
"filtered": {
"filter": { "term":{"userId":"4dff31eaf8815f4df04e2d62"} },
"query": {
term: {"mentions":"someone"}
}
}
}
}
但没有收到任何结果。
同时,内容查询工作正常:
{
"query": {
"filtered": {
"filter": { "term":{"userId":"4dff31eaf8815f4df04e2d62"} },
"query": {
"term": {"content":"some"}
}
}
}
}
通过数组搜索是否需要一些特殊语法?我发现了几个主题 [1,<一个href="http://elasticsearch-users.115913.n3.nabble.com/Query-Syntax-for-arrays-and-nested-objects-td212582.html">2] 关于elasticsearch中的数组,但是没有直接的答案。
UPD Get Mapping 调用返回下一个结果:
{
"records": {
"all":{
"properties":{
"content":{"type":"string"},
"userId":{"type":"string"},
"mentions":{"type":"string"}
}
}
}
}
UPD2 我找到了问题的根源。我不小心在我的问题中引入了错误。我在数据库中实际拥有的用户名的形式是“some_one”(下划线很重要)。因此标准索引将其分成 2 个单词并查询失败原因的“some_one”。
I have a document indexed in elasticsearch:
{
"content":"Some content with @someone mention",
"mentions":["someone"],
"userId":"4dff31eaf8815f4df04e2d62"
}
I try to find it with a query:
{
"query": {
"filtered": {
"filter": { "term":{"userId":"4dff31eaf8815f4df04e2d62"} },
"query": {
term: {"mentions":"someone"}
}
}
}
}
and receive no results.
In the same time query for content works fine:
{
"query": {
"filtered": {
"filter": { "term":{"userId":"4dff31eaf8815f4df04e2d62"} },
"query": {
"term": {"content":"some"}
}
}
}
}
Is some special syntax required for search through arrays? I found several topics [1, 2] about arrays in elasticsearch, but there is no direct answer.
UPD Get Mapping call returns the next result:
{
"records": {
"all":{
"properties":{
"content":{"type":"string"},
"userId":{"type":"string"},
"mentions":{"type":"string"}
}
}
}
}
UPD2 I found the source of problem. I accidentally introduced an error into my question. The username I actually had in DB was in form "some_one" (underscore is significant). So standard index split it into 2 words and query for "some_one" of cause failing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如您的更新所提到的,这是正确的用法。
如果导入带有“mentions”数组的文档,则搜索匹配的数组项并将其称为“mentions”,将检索该文档。我也遇到了同样的问题,刚才自己验证了一下。
This is correct usage, as your update mentions.
If you import a document with a "mentions" array, searching on a matching array item, referring to it as "mentions", will retrieve the document. I had the same issue and verified it myself just now.