Skip to content

Commit

Permalink
Docker for search service, rearchitecture
Browse files Browse the repository at this point in the history
  • Loading branch information
simonj2 committed Aug 2, 2022
1 parent b86795c commit 5675534
Show file tree
Hide file tree
Showing 21 changed files with 83 additions and 33 deletions.
20 changes: 20 additions & 0 deletions .env
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Password for the 'elastic' user (at least 6 characters)
# This needs to be set in the environment variables
# ELASTIC_PASSWORD=

# Version of Elastic products
STACK_VERSION=8.3.3

# Set the cluster name
CLUSTER_NAME=docker-cluster

# Set to 'basic' or 'trial' to automatically start the 30-day trial
LICENSE=basic

# Port to expose Elasticsearch HTTP API to the host
ES_PORT=9200
#ES_PORT=127.0.0.1:9200

# Increase or decrease based on the available host memory (in bytes)
# 1GB works well, 2GB and above leads to lower latency
MEM_LIMIT=2147483648
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,6 @@ celerybeat.pid
*.sage.py

# Environments
.env
.venv
env/
venv/
Expand Down
7 changes: 7 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Instructions from https://fastapi.tiangolo.com/deployment/docker/
FROM python:3.9
WORKDIR /code
COPY ./requirements.txt /code/requirements.txt
RUN pip install --no-cache-dir --upgrade -r /code/requirements.txt
COPY ./app /code/app
CMD ["uvicorn", "app.api:app", "--proxy-headers", "--host", "0.0.0.0", "--port", "8000"]
37 changes: 27 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,47 @@ The main file is `api.py`, and the Product schema is in `models/product.py`.
The `scripts/` directory contains various scripts for manual validation, constructing the product schema, importing, etc.

### Running locally
Firstly, make sure your environment is configured:
```commandline
export ELASTIC_PASSWORD=PASSWORD_HERE
```

Then start docker:
```console
docker-compose up -d
```

Docker spins up:
- Two elasticsearch nodes
- [Elasticvue](https://elasticvue.com/)
- The search service on port 8000

You will then need to import from CSV (see instructions below).

Make sure your environment is configured:
```commandline
export ELASTIC_PASSWORD=PASSWORD_HERE
```


### Helpful commands:
### Development
For development, you have two options for running the service:
1. Docker
2. Locally

To start docker:
To develop on docker, make the changes you need, then build the image and compose by running:
```console
docker build -t off_search_image .
docker-compose up -d
```

To start server:
However, this tends to be slower than developing locally.

To develop locally, create a venv, install dependencies, then run the service:
```console
uvicorn api:app --reload
virtualenv .
source venv/bin/activate
pip install -r requirements.txt
uvicorn app.api:app --reload --port=8001
```
Note that it's important to use port 8001, as port 8000 will be used by the docker version of the search service.


### Helpful commands:
To import data from the [CSV export](https://world.openfoodfacts.org/data):
```console
python scripts/perform_import.py --filename=/path/to/file.csv
File renamed without changes.
6 changes: 3 additions & 3 deletions api.py → app/api.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from elasticsearch_dsl import Q
from fastapi import FastAPI, HTTPException

from models.product import Product
from models.request import AutocompleteRequest, SearchRequest
from utils import connection, constants, response
from app.models.product import Product
from app.models.request import AutocompleteRequest, SearchRequest
from app.utils import connection, constants, response

app = FastAPI()
connection.get_connection()
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions models/product.py → app/models/product.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from elasticsearch_dsl import Document, Date, Double, Keyword, Text, Integer

from utils import constants
from utils.analyzers import autocomplete
from app.utils import constants
from app.utils.analyzers import autocomplete


class Product(Document):
Expand Down
2 changes: 1 addition & 1 deletion models/request.py → app/models/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Optional, List, Set
from pydantic import BaseModel

from utils import constants
from app.utils import constants


class SearchBase(BaseModel):
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions scripts/es_query.py → app/scripts/es_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"""
import time

from models.product import Product
from utils import connection
from app.models.product import Product
from app.utils import connection


def manual_query():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
This script takes the data-fields.txt and generates the updated product fields.
Note, if field names are changed, etc, this will have considerable implications for the index.
"""
from utils import constants
from app.utils import constants


def get_type_for_field(field):
Expand Down
3 changes: 1 addition & 2 deletions scripts/http_query.py → app/scripts/http_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
import time
import requests

from models.product import Product
from utils import connection
from app.utils import connection


def manual_query():
Expand Down
4 changes: 2 additions & 2 deletions scripts/perform_import.py → app/scripts/perform_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

from elasticsearch.helpers import bulk

from models.product import Product
from utils import connection, constants
from app.models.product import Product
from app.utils import connection, constants


def gen_documents(filename, next_index):
Expand Down
Empty file added app/utils/__init__.py
Empty file.
2 changes: 1 addition & 1 deletion utils/analyzers.py → app/utils/analyzers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
'autocomplete',
tokenizer=tokenizer('bigram', 'edge_ngram', min_gram=2, max_gram=25, token_chars=['letter', 'digit', 'punctuation']),
filter=['lowercase', 'asciifolding']
)
)
6 changes: 6 additions & 0 deletions app/utils/connection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import os
from elasticsearch_dsl.connections import connections


def get_connection():
return connections.create_connection(hosts=[os.getenv('ELASTICSEARCH_URL', '127.0.0.1:9200')])
File renamed without changes.
4 changes: 2 additions & 2 deletions utils/response.py → app/utils/response.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from models.product import Product
from models.request import SearchBase
from app.models.product import Product
from app.models.request import SearchBase


def create_response(es_results, request: SearchBase):
Expand Down
9 changes: 8 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ services:
timeout: 10s
retries: 120


# elasticsearch browser
elasticvue:
image: cars10/elasticvue
Expand All @@ -133,6 +132,14 @@ services:
links:
- es01

searchservice:
image: off_search_image
container_name: searchservice
environment:
- ELASTICSEARCH_URL=host.docker.internal:9200
ports:
- '8000:8000'

volumes:
certs:
driver: local
Expand Down
5 changes: 0 additions & 5 deletions utils/connection.py

This file was deleted.

0 comments on commit 5675534

Please sign in to comment.