fullscale / elastic.js

A JavaScript implementation of the elasticsearch Query DSL
http://docs.fullscale.co/elasticjs/
MIT License
654 stars 163 forks source link

How can I set type "cross-fields" in MultiMatchQuery ? #82

Open antoineberthelin opened 9 years ago

antoineberthelin commented 9 years ago

Hello,

I try to set "cross-fields" in MultiMatchQuery

http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html#type-cross-fields

I don't find this parameter, do you know if it's possible to set it ?

Thanks,

Antoine

ajcarpenter commented 9 years ago

You should be able to set this via the type method, but for some reason, elastic.js restricts this to only have the values of boolean, phrase or phrase_prefix (https://github.com/fullscale/elastic.js/blob/master/src/query/MultiMatchQuery.js#L283). If I remove this validation, I am able to successfully run a cross-fields search. ElasticSearch docs say valid values include best_fields, most_fields, cross_fields, phrase, phrase_prefix. I would guess that this was changed at some point and elastic.js hasn't been updated to match. I will try and submit a pull request for this.

ajcarpenter commented 9 years ago

As a hack, you can trick elastic.js in to thinking you're passing in a query object by giving it something like:

{
    _type: function () { return 'query'; },
    toJSON: function () { return {multi_match: this.multi_match}; },
    multi_match: { 
        query: queryParams.query,
        fields: [ 'artist_name', 'title' ],
        zero_terms_query: 'all',
        type: 'cross_fields',
        operator: 'and' 
    } 
}
ajcarpenter commented 9 years ago

Looks like there is already a PR to fix this: https://github.com/fullscale/elastic.js/pull/84