简体   繁体   中英

Typescript object not passed properly to elasticsearch

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.

在此处输入图片说明

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".

搜索必须包含对象或字符串之一

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM