Merge and Sort results of multiple indexes

I’m not good at English…

Hello there.
I want to merge and sort the results of multiple indexes.

Specific example

Acquire only the latest 3 data from posts or comments.

indexes

posts
├── title
├── description
└── createdAt
comments
├── body
└── createdAt

data

{
    "posts": [
        {
            "title": "posts title1",
            "description": "description1",
            "createdAt": "2019-03-05T16:52:48"
        },
        {
            "title": "posts title2",
            "description": "description2",
            "createdAt": "2019-03-06T10:12:32"
        }
    ],
    "comments": [
        {
            "body": "comment1",
            "createdAt": "2019-03-05T18:12:09"
        },
        {
            "body": "comment2",
            "createdAt": "2019-03-06T00:24:11"
        }
    ],
}

hits

{
    hits: [
        {
            "title": "posts title2",
            "description": "description2",
            "createdAt": "2019-03-06T10:12:32"
        },
        {
            "body": "comment2",
            "createdAt": "2019-03-06T00:24:11"
        },
        {
            "body": "comment1",
            "createdAt": "2019-03-05T18:12:09"
        }
    ]
}

what I want to know

How can I merge and sort multiple indexes?

Hi Tsukasagr,

Thanks for your question!

I want to merge and sort the results of multiple indexes.

It is possible to do a search query in multiple indices, but you would have to merge them and sort them from your own front-end as the engine doesn’t support this kind of queries.

In your case, as you are doing the same search in both indices, and sorting them according to the same criteria, I would recommend having only one index for both that could look like this:

[
    {
        "title": "posts title1",
        "description": "description1",
        "createdAt": "2019-03-05T16:52:48",
        "type": "post"
    },
    {
        "title": "posts title2",
        "description": "description2",
        "createdAt": "2019-03-06T10:12:32",
        "type": "post"
    },
    {
        "body": "comment1",
        "createdAt": "2019-03-05T18:12:09",
        "type": "comment"
    },
    {
        "body": "comment2",
        "createdAt": "2019-03-06T00:24:11",
        "type": "comment"
    }
]

And then you can sort it by date: https://www.algolia.com/doc/guides/managing-results/refine-results/sorting/how-to/sort-an-index-by-date/. Note that to be properly sorted, dates have to be stored in the UNIX timestamp format. In order not to override the relevance ranking strategy for this index, I would also advise to apply this sorting criteria in a replica index.

You can still display posts and comments separately in different sections of your website by applying a filter on the “type” attribute.

In case you still want to do a multi-index search, you can setup an instantsearch instance for each of your indices, and then hook them to the same search box with the aid of a helper:
like explained in the docs here: https://www.algolia.com/doc/guides/building-search-ui/resources/ui-and-ux-patterns/in-depth/multi-index-search/js/
and implemented in this JS Fiddle: https://jsfiddle.net/j9nwpz34/27/.

I hope this helps!
Let us know how it goes and if you have more questions.

Thank you!

I would recommend having only one index for both that could look like this:

I will proceed with the policy recommended by you.

I have one more question.

When the size of this index increases,Is it better to split the index by date(or month) and sort by front-end?
I want to display multiple models like a twitter timeline, so I can not use instance search…