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

How to query for nested objects 2 levels down? #150

Closed
chrillewoodz opened this issue Mar 27, 2017 · 7 comments
Closed

How to query for nested objects 2 levels down? #150

chrillewoodz opened this issue Mar 27, 2017 · 7 comments

Comments

@chrillewoodz
Copy link

chrillewoodz commented Mar 27, 2017

This can't be right (why I think this is wrong is because I get an error as well as this query being ridiculous to write):

const config: any = {
  type: 'event',
  body: {
    query: {
      bool: {
        must: [
          {
            nested: {
              path: 'guestlist',
              query: {
                bool: {
                  must: [
                    {
                      path: 'list',
                      query: {
                        bool: {
                          must: [
                            {
                              match: {
                                "list.name": query
                              }
                            }
                          ]
                        }
                      }
                    }
                  ]
                }
              }
            }
          }
        ]
      }
    }
  }
};

For data that looks like this:

[
  {
    guestlist: {
      list: [
        {
          name: 'Chris Woodland'
        }
      ]
    }
  }
]

The error I get is

Firebase.push failed: first argument  contains an invalid key (list.name) in property 'search.request.body.query.bool.must.0.nested.query.bool.must.0.query.bool.must.0.match'.  Keys must be non-empty strings and can't contain ".", "#", "$", "/", "[", or "]"

Which is also mentioned in #144. Some help with this would be appreciated since I find the docs for nested queries on both ES site and here a bit vague.

EDIT: So after stringifying the query of body it doesn't throw me error anymore, but I'm still not getting any results despite searching for exactly Chris Woodland. So I'm guessing that my query structure is off somewhere.

@chrillewoodz
Copy link
Author

I also seem to be getting a unknown key for VALUE_STRING error whenever I stringify the query property in body. Is this a known issue?

@katowulf
Copy link
Contributor

I think you stringify the whole body, not just the query parameter.

As for the ES syntax, it looks like you're trying to use deeply nested data structures. Those aren't going to work well with Flashlight or ES. You also shouldn't use arrays with distributed data.

But I can't be any more specific without some context on what the data structure is you're trying to index and what your config.js paths look like.

@chrillewoodz
Copy link
Author

Config paths:

exports.paths = [
  {
    path: "accounts",
    index: "firebase",
    type: "account"
  },
  {
    path: "events",
    index: "firebase",
    type: "event"
  }
];

So my database looks like this (visually presented):

/ accounts
    - account
    - account
/ events
    - event
      - guestlist
        - counters <-- counters keeping track of things inside the list
        - list <-- the actual list 
          - guest
            - name: 'Chris Woodland'
          - guest
            - name: 'Bugs Bunny'
          - guest
            - name: 'Bunny bugs'

I kinda don't want guestlist to be its own list with a reference to an event because that will complicate things.

Surely I must be able to just go into an event and search the guestlist from there?

As for the stringifying, I'm doing this: config.body.query = JSON.stringify(config.body.query); where config contains the body or q, type, size and from. And I can see that only the query is being stringified. So that shouldn't be the problem.

@katowulf
Copy link
Contributor

Look at the code I linked you to. You don't stringify components of body; you stringify the whole body object and save that to the db.

As for the nested query portion, that's purely ES. Looks like this article might help to understand how it should work.

@chrillewoodz
Copy link
Author

When I stringify the entire body I just get permission denied errors because of the security rules that you provided, do I have to do anything else?

@ghost
Copy link

ghost commented Apr 11, 2017

Hi I think I am having the same issue, except I am able to stringify the body and post the request to my Firebase instance. What happens next is that Flashlight never writes the response back.

@ghost
Copy link

ghost commented Apr 12, 2017

OK looks like #136 fixed the issue for me!

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