Search code examples
buildfire

Buildfire - Using regex in search


I have been looking at the documentation but I can't seem to figure out how to correctly create a filter object for the buildfire.datastore.search query.

I have an address property on my object and I want to be able to type in a partial amount of the address and have it return. Below are the filter objects I have tried to pass into the search query:

search = {filter: {"$json.address": {"$regex": `/${this.state.search}/`}}};

search = {filter: {'$regex': {'$json.address': this.state.search}}};

Neither have worked. End goal is:

buildfire.datastore.search(search, 'location', cb);

EDIT:

I even tried to hardcode the regex in the docs:

"$or" : [
  {"description": {"$regex":"/new /"}}
]

and it didn't work (I replaced 'new' with a string I knew would show).


Solution

  • I just inserted the following on the control side:

    for(let i = 0 ; i < 50 ; i++) {
        buildfire.datastore.insert({
            name: "Address" + i
            ,address: i + " " + (i % 2 ? "Main ":"4th ") + (i % 3 ? "ave":"st" )
        },function(){});
    }
    

    then did a search on the widget side like this:

    <body>
    <input type="text" id="criteria" /><button onclick="search()">Search</button>
    <div id="results"></div>
    <script>
        function search(){
            var cri = document.getElementById("criteria").value;
    
            buildfire.datastore.search( {filter:{"$json.name": {"$regex": cri  }  } } , function(err,results){
                document.getElementById("results").innerHTML = JSON.stringify(results);
            });
        }
    </script>
    </body>
    

    Works fine. Given if you want the search to be more complex then you need to modify the regex statement for example case insensitivity.

    Hope this helps