Flask + elasticsearch achieve search engine Start Tutorial

A few days ago, on github I saw a small item on elasticsearch of little interest, so he combined the Flask, study a little, to others.

prepare materials:

1, the installation elasticsearch

Tutorial: https: //www.cnblogs.com/gangle/p/9328257.html

2, simple search engine

Tutorial: https: //dev.to/aligoren/using-elasticsearch-with-python-and-flask-2i0e

This tutorial explains every step in great detail, and finally attached the source code, though a bit simple, but understanding elasticsearch enough.

 

You can copy the source code into their own flask project folder, debugging achieve results with Curl.

curl -i -H "Content-Type:application/json" -X POST -d "{\"slug\":\"slug1\",\"title\":\"kehuan\",\"content\":\"zhen ciji\"}" http://localhost:5000/insert_data

POST code via the above method, the index is inserted;

Get the return.

curl -i -H "Content-Type:application/json" -X POST -d "{\"keyword\":\"kehuan\"}" http://localhost:5000/search

Defined by the code search method, to achieve the keyword "kehuan" search, i.e., in accordance with the given keyword, the search index is inserted before the match value, and returns as shown.

Extended:

Can be used in conjunction with article elasticsearch keyword search database implementation blog, combined with reptiles crawling technology to achieve real-time data display and so on.

Note: The tutorial source code is not suitable for curl debugging, the debugger needs to be done to achieve the curl can modify the following two points:

1, a total of four modified

Request.Form [ '  ' ] modified as: request.json [ ' ']

2, a modified

res = es.search(index="contents", doc_type="title", body=body)   去掉:   doc_type="title" 即可

Finally, like Flask friends, you can scan the two-dimensional code, there are a small series of original technology articles, we hope everyone will help.

 

Guess you like

Origin www.cnblogs.com/Fcode-/p/11577487.html