Untitled

 avatar
user_3839718
python
7 months ago
1.3 kB
3
Indexable
Never
from chalice import Chalice
from chalicelib.db.es import ES

app = Chalice(app_name='crawler-api')

client = ES(container_es_host="0.0.0.0:9200",
            container_es_user="elastic",
            container_es_password="password")


@app.route('/')
def index():
    return {'hello': 'world'}


@app.route('/search', methods=['GET'])
def search():
    query = {"query": {"match_all": {}}}
    return client.search(index_name="review", query=query)


@app.route('/search/location', methods=['POST'])
def location():
    param = app.current_request.json_body
    if "lat" not in param or "lon" not in param or "distance" not in param:
        return {"error": "lat, lon, and distance are required"}

    query = {"query": {
                "bool": {
                  "must": {
                    "match_all": {}
                  },
                  "filter": {
                    "geo_distance": {
                      "distance": param["distance"],
                      "location": {
                        "lat": param["lat"],
                        "lon": param["lon"]
                      }
                    }
                  }
                }
              }
            }
    return client.search(index_name="review", query=query)
Leave a Comment