如何用弹性搜索中的数组元素进行搜索? [英] How to search by array elements in elasticsearch?

查看:124
本文介绍了如何用弹性搜索中的数组元素进行搜索?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  {
content:Some content with @someone提到,
提到:[某人],
userId:4dff31eaf8815f4df04e2d62
}

我尝试用查询找到它:

  {
query: {
filtered:{
filter:{term:{userId:4dff31eaf8815f4df04e2d62}},
query:{
term:{ 提到:某人}
}
}
}
}





同时内容查询工作正常:

  {
query:{
filtered:{
filter:{term:{userId:4dff31eaf8815f4df04e2d62}}
query:{
term:{content:some}
}
}
}
}

是一些specia l通过数组搜索所需的语法?我发现了几个主题[ 1 ,<关于弹性搜索中的数组的href =http://elasticsearch-users.115913.n3.nabble.com/Query-Syntax-for-arrays-and-nested-objects-td212582.html> 2 ]但是没有直接的答案。



UPD 获取Mapping调用返回下一个结果:

  {
records:{
all:{
properties:{
content:{类型:string},
userId:{type:string},
提到:{type:string}
}
}
}
}

UPD2 的问题。我不小心在我的问题中引入了一个错误。 DB中实际使用的用户名是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.

解决方案

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.

这篇关于如何用弹性搜索中的数组元素进行搜索?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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