0
votes

I have a function in typescript which writes an object to my firebase's "search/request" path in the form of an elasticsearch query.

A script running flashlight on Heroku reads that "search/request/XYZ" object in the firebase and then creates a "search/response/XYZ" object after performing a search.

In my app, the following snippet works properly:

    let queryBody: Object = {
    index: 'firebase',
    type: 'vehicle',
    query: { match_all: {} },
    size: 4
}
this.requestKey = this._allSearchRequest$.push(queryBody).key

When executed, the response object in firebase correctly indicates "97 hits" and displays the first 4 of them.

enter image description here

However, I can't perform more complicated queries. I try to execute the following snippet, which is an attempt to match all vehicles according to the their vin property:

    let queryBody: Object = {
    index: 'firebase',
    type: 'vehicle',
    query: { match: {vin: '*'} },
    size: 4
}
this.requestKey = this._allSearchRequest$.push(queryBody).key

But when I do so, I get the following error, "Search must contain a string or object".

search must contain one of object or string

The documentation for elasticsearch is written in javascript, with double quotes used on all keys. Typescript does not use that syntax. I suspect that there may be an issue converting the typescript object to javascript to json.

My next step in debugging will be to try creating an explicit JSON object using the exact format specified by the ElasticSearch docs.

Any thoughts in the meantime would be helpful.

1
It has nothing to do with TypeScript, I promise you. By the way it fully supports double quoted keys and all other JavaScript syntax. Did you forget to call JSON.stringify? - Aluan Haddad
By the way, it is only possible to create a JSON object using a serializer or by hand using string manipulation. This is because JSON is a textual data interchange format and not an in memory object model. In JavaScript, all JSON values are members of the string type. - Aluan Haddad
You'll want to use q or body rather than query here. As mentioned, this has nothing to do with typescript and everything to do with the ES library for node not liking your query syntax. - Kato
@Kato, why is q or body preferred over query? - Cinghiale

1 Answers

0
votes

My solution is to make the query or q property value be a string. For example, the following snippet will match all objects whose vin property begins with "1GC".

let queryBody: Object = {
    index: 'firebase',
    type: 'vehicle',
    query: 'vin:1GC*'
}
this.requestKey = this._allSearchRequest$.push(queryBody).key;

Note that in the above example, this._allSearchRequest$ is a FireBase List Observable per angularfire2. I don't know whether this "string" method will be suitable for more complex queries.

A more powerful method is shown in the example client side script in the Flashlight repo. It appears to pass an object rather than a string.