Skip to content
This repository has been archived by the owner on Jan 12, 2019. It is now read-only.

Query nested object #144

Open
martinop opened this issue Mar 21, 2017 · 8 comments
Open

Query nested object #144

martinop opened this issue Mar 21, 2017 · 8 comments

Comments

@martinop
Copy link

martinop commented Mar 21, 2017

If I have this:

...
name: "Rokgo",
createdAt: "2017-02-09T22:47:23.740Z"
pets: {
  catsAllowed: true,
  dogsAllowed: true
}
...

How I can query this nested object?
I try with:

   params = {
      body: {
        query: {
          "bool": {
            "must": [
              {
                "nested": {
                  "path": "pets", 
                  "query": {
                    "bool": {
                      "must": [ 
                        {
                          "match": {
                            "pets.catsAllowed": true
                          }
                        },
                        {
                          "match": {
                            "pets.dogsAllowed": true
                          }
                        }
                      ]
                    }
                  }
                }
              }
            ]
          }
        },
      }
}

But had this error:

Error: Firebase.push failed: first argument  contains an invalid key (pets.catsAllowed) in property 'search.request.body.query.bool.must.1.nested.query.bool.must.0.match'.  Keys must be non-empty strings and can't contain ".", "#", "$", "/", "[", or "]"
@katowulf
Copy link
Contributor

Stringify the JSON under the body key, so that you have:

params = {
body: "{ "query": .... }"
}

Flashlight will reconstitute the object server side.

@martinop
Copy link
Author

Params = {
body: "{"query":{"bool":{"must":[{"bool":{"should":[{"match":{"type":"Single Family Home"}},{"type":{"value":"property"}}]}},{"nested":{"path":"pets","query":{"bool":{"must":[{"match":{"pets.catsAllowed":true}},{"match":{"pets.dogsAllowed":true}}]}}}}]}}}"
form: 0,
index: "firebase",
size: 10
}

I never got a response :/

@katowulf
Copy link
Contributor

That's not a valid JSON string.

@martinop
Copy link
Author

Why? How I can fix this?

@katowulf
Copy link
Contributor

Try creating the json object you are going to put into the body key, then using JSON.stringify(...) on it. If you can't get this working, there is an error log somewhere, find that and share and I'll see what I can do to help.

@martinop
Copy link
Author

martinop commented Mar 21, 2017

I try with this:

const obj = {
        query: {
          "bool": {
            "must": [
              {
                "nested": {
                  "path": "pets", 
                  "query": {
                    "bool": {
                      "must": [ 
                        {
                          "match": {
                            "pets.catsAllowed": true
                          }
                        },
                        {
                          "match": {
                            "pets.dogsAllowed": true
                          }
                        }
                      ]
                    }
                  }
                }
              }
            ]
          }
        },
      }
    console.log(JSON.stringify(obj))
    const params = {
      sort: ["_score"],
      from: 0,
      size: 10,
      type: "property",
      body: JSON.stringify(obj)
    }

And the same :/ dont return nothing, keep loading

@katowulf
Copy link
Contributor

I don't know what "don't return nothing" means in this context. You'll need to start with a minimal repro, including all the critical elements for understanding and reproducing the error. See how to ask and creating an mcve.

@martinop
Copy link
Author

martinop commented Mar 22, 2017

I have this:

...
name: "Rokgo",
createdAt: "2017-02-09T22:47:23.740Z"
pets: {
  catsAllowed: true,
  dogsAllowed: true
}
...

And I want to make a request to query only the elements that allows cats, I try to do that with this:

 const params = {
      from: 0,
      size: 10,
      body: {
        query: {
          "bool": {
            "must": [
              {
                "nested": {
                  "path": "pets", 
                  "query": {
                    "bool": {
                      "must": [ 
                        {
                          "match": {
                            "pets.catsAllowed": true
                          }
                        }
                      ]
                    }
                  }
                }
              }
            ]
          }
        },
      }
    }

I have this error:
Error: Firebase.push failed: first argument contains an invalid key (pets.catsAllowed) in property 'search.request.body.query.bool.must.1.nested.query.bool.must.0.match'. Keys must be non-empty strings and can't contain ".", "#", "$", "/", "[", or "]" And if I "Stringify" the query inside the body I dont have any response, still loading until timeout

This is a good way to query that? no? What I can do to filter Elements by Pets Object?

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants