CodeX Backend task: Search

4 min read

Task

Write an API that will allow quick search of data, as well as recording and editing of this data using Elasticsearch (ES) in conjunction with a database. Adding and editing of the data in database should influence the search and data structure in ES.

Setup

You can use docker container preset for ES, also you will need database for storing actual data (we recomend PostgreSQL, but you can use any database that you are comfortable with)

Given

Database mocks with format

[ { id: number, title: string, content: string, } ]

Lets call this structure a "document".

This are mocks for your database.

You can store this data in ES data-node in any format you want, try to find the best option.

Scenarios

Save

First of all, you need to be able to save information passed to database to ES. For that we need to have one route that is responsible for saving data.

As a result we should have route

POST /document request: { body: { document: { title: string, // title of the document to be posted content: string, // content of the document to be posted } } } reply: { document: { id: number, // id of the posted document title: string, // title of the posted document content: string, // content of the posted document } }

Search

We need to have route that would search for the given substring across all the documents (full text search), but priority should be given to title field.

We should have this response structure:

{ id: number, // id of the document, where substring was found fieldName: string, // name of the field where substring was found ('title' or 'content') fieldContent: string, // content of the field where substring was found }

e.g.

Given database structure

[ { id: 1, title: "Codex", content: "We are international developers team" }, { id: 2, title: "Our projects", content: "Codex team developed several big projects like Editor.js, Hawk, Notex" }, { id: 3, title: "Codex Workflow", content: "Here you can see work progress of the codex team" } ]

Given search request:

Search by word "Codex"

All three should be found, but priority would be given to document, whose title contain keyword. We should get this response structure:

[ { id: 1, fieldName: 'title', fieldContent: 'Codex', }, { id: 3, fieldName: 'title', fieldContent: 'Codex Workflow', }, { id: 2, fieldName: 'content', fieldContent: 'Codex team developed several big projects like Editor.js, Hawk, Notex' }, ]

As a result we should have route

GET /search request: { querystring: string // text search substring } reply: { documents: [ { id: number // id of the document, where substring is found fieldName: string // field name, where substring is found ('title' or 'content') fieldContent: string // content of the field, where substring is found } ] }

Patch

Last, but not least is that we need to have ability to patch database fields by id. Сhange in the database must be accompanied by a change in data in the ES data-node so that the search occurs only according to current data.

As a result we should have route

PATCH /document request: { documentId: number, // id of the document to be patched document: { title: string, // new title of the document content: string, // new content of the document } } reply: { document: { id: number, // id of the patched document title: string, // new document title content: string, // new document content } }

Where to submit?

Publish you code on the GitHub and provide all required instructions for using it

Deadline

October, 16th. It's recommended to show your in-between progress and ask questions in a Telegram Channel.