在elasticsearch中搜索多个字段

ral*_*nja 1 elasticsearch

我正在索引文件的元数据,我的映射看起来像这样:

curl -XPUT "http://localhost:9200/myapp/" -d '
{
    "mappings": {
        "file": {
            "properties": {
                "name": {
                    "type": "string", "index": "not_analyzed"
                },
                "path": {
                    "type": "string", "index": "not_analyzed"
                },
                "user": {
                    "type": "string", "index": "not_analyzed"
                }
            }
        }
    }
}
'
Run Code Online (Sandbox Code Playgroud)

还有其他一些领域,但它们与这个问题无关.这里'name'是文件的名称,'path'是文件的路径,user是该文件的所有者.索引数据时看起来像:

{ name: 'foo', path: '/', user: 'dude' }
{ name: 'bar', path: '/subfolder/', user: 'dude' }
{ name: 'baz', path: '/', user: 'someotherdude' }
Run Code Online (Sandbox Code Playgroud)

我的问题是如何构建我的查询,以便我可以在给定路径和用户的情况下进行文件夹列表.因此,搜索用户'dude'和路径'/'应该导致'foo'.

Aks*_*rma 6

您可以使用bool,它允许您指定必须匹配的查询以构成匹配.您的查询将如下所示

{
    "query": {
        "bool" : {
            "must" : [ 
                { "match": { "user" : "dude" } },
                { "match": { "path" : "/" } }
            ]
         }
    },
    "fields": ["name"]
}
Run Code Online (Sandbox Code Playgroud)

这样做是检查术语"dude"和路径"/"是否完全匹配

  • 弄清楚了.如果您能将答案更新为"必须",那就太好了:[{"match":{"user":"dude"}},{"match":{"path":"/"}}] (2认同)