1

I'm indexing meta data on files and my mappings looks like this:

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"
                }
            }
        }
    }
}
'

There are some other fields too, but they are not relevant to this question. Here 'name' is the name of a file, 'path' is the path to the file and user is the owner of that file. When indexing the data could look something like:

{ name: 'foo', path: '/', user: 'dude' }
{ name: 'bar', path: '/subfolder/', user: 'dude' }
{ name: 'baz', path: '/', user: 'someotherdude' }

My question is how to build my query so that I can do folder listings given a path and a user. So searching for the user 'dude' and path '/' should result in 'foo'.

1 Answer 1

5

You can use a bool which allows you to specify the queries that must match to constitute a hit. Your query will look something like this

{
    "query": {
        "bool" : {
            "must" : [ 
                { "match": { "user" : "dude" } },
                { "match": { "path" : "/" } }
            ]
         }
    },
    "fields": ["name"]
}

What this does is checks that there is an exact match for the term "dude" and path "/"

Sign up to request clarification or add additional context in comments.

9 Comments

This works fine if I e.g. curl it on command line. Since I'm in a JavaScript environment and build my query objects directly from JS it becomes problematic due to multiple "must" in the "bool" section. Pondering how to get around that. Any suggestions? :)
Figured it out. Would be nice if you could update your answer to "must" : [ { "match": { "user" : "dude" } }, { "match": { "path" : "/" } } ]
Thanks all to your contributions. It works like a clock here. Very fast and clean! :)
It's truly amazing and CRAZY fast!
Thanks ! I was looking for something like this
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.