0
votes

I am using Firebase Flashlight to query my Firebase database.

Here is how my data is structured in Firebase:

posts: {
  id: {  
   "address": "",
   "id": "",
   "name": "",
   "review": "",
   "summary": "",
  }, id: {...}
}, 
users: {
   uid: {
    "username": "",
    "name": "",
   }, uid: {...}
}

And this is the how I have set up my config.js in the Flashlight Node app:

exports.paths = [
   {
      path:  "users",
      index: "firebase",
      type:  "user",
      fields: ["username", "name"]
   },{
      path: "posts",
      index: "firebase",
      type: "post",
      fields: ["id","name","summary","review", "address"]
   }
];

I can easily search for a user by sending a simple query string that will search the usernames and names of users and return the uid of that user.

I want to be able to loop through an array of post ids (basically just a loop of queries), and see if any of the other fields (address, name, review, summary) match a specified keyword, with wildcard too.

So the search must match the post id, and must match the keyword to address OR name OR review OR summary.

This is the query I have tried, but I do not know how to do the ORs:

var query = {
  'query': {
    'bool': {
      'must': [
        {'match': {'name': 'duck'}},
        {'match': {'review': 'duck'}},
        {'match': {'summary': 'duck'}},
        {'match': {'address': 'duck'}},
        {'match': {'id': '-K1kiCqGWfM-29pi9wdC'}}
      ]
    }
  }
};

How can I form a query that will check if the id matches the id field and the keyword matches any of the other fields?

2

2 Answers

0
votes

I got it working with the following:

  var query = {
     'query': {
      'bool': {
        'must': [
          {'match': {'id': '-K1kiCqGWfM-29pi9wdC'}},
          {'bool': {
            'should': [
              {"match": {"name": "duck"}},
              {"match": {"review": "duck"}},
              {"match": {"summary": "duck"}},
              {"match": {"address": "duck"}}
            ]
          }}
        ]
      }
    }
  };

Thanks to Evaldas Buinauskas for pointing me in the right direction.

0
votes

This is going toe be raw JSON query, but it should work for you:

{
  "query": {
    "bool": {
      "must": [
        {"match": {"id": "-K1kiCqGWfM-29pi9wdC"}},
        {
          "bool": {
            "should": [
              {"match": {"name": "duck"}},
              {"match": {"review": "duck"}},
              {"match": {"summary": "duck"}},
              {"match": {"address": "duck"}}
            ]
          }
        }
      ]
    }
  }
}

In Elasticsearch should stands as OR condition.

Notice that I created nested bool query. If that would be SQL, it would look somehow like this:

SELECT *
FROM Document
WHERE id = '-K1kiCqGWfM-29pi9wdC'
  AND (
    address = 'duck'
    OR NAME = 'duck'
    OR review = 'duck'
    OR summary = 'duck'
    );