npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

search-microservice

v0.3.1

Published

search microservice

Downloads

33

Readme

Search Microservice

Search Microservice

The Search Microservice provides a RESTful API for managing and searching text data, with built-in support for synonyms. It features endpoints for configuring settings, and for adding, updating, deleting, and searching text data.

The search functionality is powered by Elasticsearch, utilizing both fuzzy matching and synonym matching to deliver highly relevant and comprehensive search results. This enables flexible and intelligent search queries.

Example Usage

Adding Synonyms

# Add synonyms 'title, subject' for the 'news' index
curl -X POST http://localhost:3000/news/synonyms \
  -H 'Content-Type: application/json' \
  -d '{ "synonyms": ["title, subject"] }'

Adding Text Data

# Add new text data to the 'news' index
curl -X POST http://localhost:3000/news \
  -H 'Content-Type: application/json' \
  -d '{ "id": 1, "text": ["News title", "News detailed content"] }'

Performing a Search

# Search for 'subject' in the 'news' index
curl -X GET http://localhost:3000/news?search=subject

Search Result

[
  {
    "_index": "news",
    "_type": "_doc",
    "_id": "1",
    "_score": 0.2876821,
    "_source": {
      "text": [
        "News title",
        "News detailed content"
      ]
    },
    "highlight": {
      "text": [
        "News **title**"
      ]
    }
  }
]

Example Searches

Here are some examples that demonstrate the power of fuzzy matching and synonym support:

  • Searching for text will match text.
  • Searching for Txt will match text.
  • Searching for Tuxt will match text.
  • Searching for automobile will match car, provided the synonym "automobile, car" has been configured.

Installation

To install and run the microservice:

git clone https://github.com/ivanoff/search-microservice.git
cd search-microservice

Initialize Data Folder

./init.sh

Start the Server:

docker compose up -d

API Endpoints

Set Synonyms

POST /:index/synonyms

Sets synonyms for the specified index.

Request Body

{
  "synonyms": [
    "word1, synonym word1, synonym word1",
    "word2, synonym word2, synonym word2"
  ]
}

Example Request

curl -X POST http://localhost:3000/news/synonyms \
  -H 'Content-Type: application/json' \
  -d '{
    "synonyms": [
      "word1, synonym word1, synonym word1",
      "word2, synonym word2, synonym word2"
    ]
  }'

Get Synonyms

GET /:index/synonyms

Retrieves the synonym configuration for the specified index.

Example Request

curl -X GET http://localhost:3000/news/synonyms

Add Text Data

POST /:index

Adds new text data to the specified index.

Request Body

{
  "id": ":indexId",
  "text": ["text data", "additional text data"]
}

Example Request

curl -X POST http://localhost:3000/news \
  -H 'Content-Type: application/json' \
  -d '{
    "id": 1,
    "text": ["text data", "additional text data"]
  }'

Update Text Data

PUT /:index/:indexId

Updates the existing text data in the specified index.

Request Body

{
  "text": ["updated text data", "more updated text data"]
}

Example Request

curl -X PUT http://localhost:3000/news/1 \
  -H 'Content-Type: application/json' \
  -d '{
    "text": ["updated text data", "more updated text data"]
  }'

Delete Text Data

DELETE /:index/:indexId

Deletes text data from the specified index.

Example Request

curl -X DELETE http://localhost:3000/news/1

Search

GET /:index?search=word&page=1&size=10

Performs a search on the specified index with pagination support.

Example Request

curl -X GET http://localhost:3000/news?search=word&page=1&size=10

Possible Errors

TOO_MANY_REQUESTS/12/disk usage exceeded

If you encounter an error like:

ResponseError: cluster_block_exception
	Root causes:
		cluster_block_exception: index [news] blocked by: [TOO_MANY_REQUESTS/12/disk usage exceeded flood-stage watermark, index has read-only-allow-delete block];

Solution:

You can resolve this issue by updating the Elasticsearch cluster settings:

curl -X PUT "http://localhost:9200/_cluster/settings" \
  -H 'Content-Type: application/json' \
  -d '{
    "persistent": {
      "cluster.routing.allocation.disk.watermark.low": "97%",
      "cluster.routing.allocation.disk.watermark.high": "98%",
      "cluster.routing.allocation.disk.watermark.flood_stage": "99%"
    }
  }'