Kelvin
Kelvin

Reputation: 2288

Elasticsearch return only results that match array of ids

Is it possible to use elastic search to query only within a set of roomIds?

I tried using bool and should:

query: {
    bool: {
        must: [
        {
            multi_match: {
            operator: 'and',
            query: keyword,
            fields: ['content'],
            type: 'most_fields'
            }
        },
        { term: { users: caller } },
        {
            bool: {
            should: 
                term: {
                    room: [list of roomIds]
                }
            }
        }
        ]
    }
},

It works but when I have more than 1k roomIds I get "search_phase_execution_exception".

Is there a better way to do this? Thanks.

Upvotes: 0

Views: 950

Answers (1)

Vova Bilyachat
Vova Bilyachat

Reputation: 19484

For array search you should be using terms query instead of term

query: {
    bool: {
        must: [
        {
            multi_match: {
            operator: 'and',
            query: keyword,
            fields: ['content'],
            type: 'most_fields'
            }
        },
        { term: { users: caller } },
        {
            bool: {
            should: 
                terms: {
                    room: [list of roomIds]
                }
            }
        }
        ]
    }
},

From documentation

By default, Elasticsearch limits the terms query to a maximum of 65,536 terms. This includes terms fetched using terms lookup. You can change this limit using the index.max_terms_count setting.

Upvotes: 2

Related Questions