ramuta/gae-2nd-gen-examples

Sample for gae with elasticsearch

montao opened this issue · 1 comments

Hi, I have successfully been able to make searches with elasticsearch instead of the old deprecated Python2 Search API.

If you are interested I can provide you with details. I was using GAE with Python2.7 and the "search API" but instead I started a Bitnami Elasticsearch VM in GCE and could connect to it from the new GAE Python3.7 environment. It is actually more convenient than the old API as far as I can tell.

My search handler is something like the following


@app.route("/view-list.html")
def view_list():
    es = Elasticsearch(['http://user:p477w0rd42@42.420.26.42:80/elasticsearch'])

    total = 0
    start = 0
    size = 50
    query = None
    qres = None
    res = None

    if request.args.get('start'):
        start = int(request.args.get('start'))
    if request.args.get('size'):
        size = int(request.args.get('size'))
    if request.args.get('query'):
        query = request.args.get('query')

    if query is None:
        res = es.search(index="f00-index",
                  body={"from": start, "size": size, "query": {"bool": {"filter": {"term": {"published": True}}}},
                        "sort": [{"milliseconds": {"order": "desc"}}]})

        total = res['hits']['total']["value"]
        res = res['hits']['hits']

    else:
        s = Search(using=es, index="kool-index").filter("term", published=True).query("match", text=query).sort('milliseconds')
        end = start + size
        qres = s[start:end].execute()
        total =  s.count()

        for hit in qres:
            logging.info(hit.meta.score)
            logging.info(hit.title)

        res = qres

    return render_template('view-list.html', total=total, start=start, size=size, request=request, query=query, res=res, es=es)

Hey Niklas, thanks for your code snippet! If you'll make an example repo on your GitHub account, I can link to it in this repo's readme ;)