Code Monkey home page Code Monkey logo

node-app-with-elasticsearch's Introduction

Node Elasticsearch Example

Starting Services

docker-compose up elasticsearch kibana
docker-compose up mysql redis
docker-compose up app listener

Database Structure and Initialization

Connect the mysql container with following configuration:

Host: mysql
User : root
Password : 123456
Port : 33060

And import data/sample_data.sql file for our sample data.

Redis configuration :

Host: redis

Elastic Integration

Then use Kibana Console interface to be able to create your index. First of all create your products index and product type.

DELETE products

PUT products
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0,
    "analysis": {
      "analyzer": {
        "autocomplete_analyzer" : {
          "type" : "custom",
          "tokenizer": "standard",
          "filter": ["lowercase", "autocomplete_filter"]
        },
        "default_search": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "standard",
            "lowercase",
            "asciifolding"
          ]
        }
      },
      "filter": {
        "autocomplete_filter": {
          "type": "edge_ngram",
          "min_gram": 2,
          "max_gram": 10
        }
      }
    }
  }
}


PUT products/product/_mapping
{
  "properties": {
    "name": {
      "type": "text",
      "fields": {
        "autocomplete": {
          "type": "text", 
          "analyzer": "autocomplete_analyzer",
          "search_analyzer": "standard"
        }
      }
    },
    "description": {
      "type": "text",
      "fields": {
        "autocomplete": {
          "type": "text", 
          "analyzer": "autocomplete_analyzer",
          "search_analyzer": "standard"
        }
      }
    },
    "quantity": {
      "type": "long"
    },
    "price": {
      "type": "double"
    },
    "created_at": {
      "type": "date",
      "format": "strict_date_optional_time||yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
    },
    "updated_at": {
      "type": "date",
      "format": "strict_date_optional_time||yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
    },
    "categories": {
      "type": "object",
      "properties": {
        "id": {
          "type": "long"
        },
        "name": {
          "type": "keyword",
          "fields": {
            "autocomplete": {
              "type": "text", 
              "analyzer": "autocomplete_analyzer",
              "search_analyzer": "standard"
            }
          }
        }
      }
    },
    "completion": {
      "type": "completion",
      "analyzer": "default_search",
      "search_analyzer": "default_search"
    }
  }
}

Then check data/logstash.conf file and also last part of the data/sample_data.sql. There is a procedure in there.

DROP PROCEDURE fetchDataForElastic;

DELIMITER //
CREATE PROCEDURE fetchDataForElastic
(IN currentdate Datetime)
BEGIN
  SELECT
    p.*,
    
    CAST( (CONCAT ('[', GROUP_CONCAT(CONCAT('{"id":', c.id, ', "name":"',c.name,'"}')), ']'))  AS JSON) categories
    FROM products p LEFT JOIN product_category pc ON pc.product_id = p.id LEFT JOIN categories c ON c.id = pc.category_id
  WHERE p.updated_at > currentdate GROUP BY p.id;
END //
DELIMITER ;

At the end, run logstash. docker-compose up logstash

Known Issues :

  • Sometimes application stack without error while updating product

node-app-with-elasticsearch's People

Contributors

hkulekci avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.