Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Node.js ElastiSearch client. How to use filter_path parameter with _msearch method

I use https://github.com/elastic/elasticsearch-js ElasticSearch client in my application. I want use Multi Search API and reduce response with filter_path parameter. In Kibana request looks like:

POST _msearch?filter_path=responses.hits.total
{ "index": "first_index" }
{"query": {"term": {"status": 1}} }
{ "index": "second_index" }
{"query": {"term": {"status": 1}} }

Response:

{
  "responses": [
    {
      "hits": {
        "total": 1935
      }
    },
    {
      "hits": {
        "total": 1212
      }
    }
  ]
}

But I can't find right place where should be this filter_path parameter in client.msearch method. Something like:

client.msearch({
    body: [
        { "index": "first_index" },
        { "q": 'filter_path=responses.hits.total' },
        {"query": {"term": {"status": 1}} },
        { "index": "second_index" },
        { "q": 'filter_path=responses.hits.total' },
        {"query": {"term": {"status": 1}} }
    ]
})

does't work. How can I send this request with Node.js ElasticSearch Client?

like image 652
volodymyrkoval Avatar asked Aug 24 '26 00:08

volodymyrkoval


1 Answers

filterPath key should be given at the same level with body.

client.msearch({
    body: [
        { "index": "first_index" },
        {"query": {"term": {"status": 1}} },
        { "index": "second_index" },
        { "q": 'filter_path=responses.hits.total' },
        {"query": {"term": {"status": 1}} }
    ],
    filterPath: "responses.hits.total"
})

msearch function takes a parameter type of MSearchParams.

msearch<T>(params: MSearchParams): Promise<MSearchResponse<T>>;

I recommend you to install the elasticsearch type definition and so you can see the type hierarchy

export interface MSearchParams extends GenericParams {
    search_type?: "query_then_fetch" | "query_and_fetch" | "dfs_query_then_fetch" | "dfs_query_and_fetch";
    maxConcurrentSearches?: number;
    index?: NameList;
    type?: NameList;
}

export interface GenericParams {
    requestTimeout?: number;
    maxRetries?: number;
    method?: string;
    body?: any;
    ignore?: number | number[];
    filterPath?: string | string[];
}
like image 192
peja Avatar answered Aug 25 '26 14:08

peja



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!