48 Python distributed crawler build search engine Scrapy explaining-elasticsearch (search engine) implements the search function with Django

Source: Internet
Author: User
Tags lenovo

Django implements the search function

1. The route map on the Django Configuration search results page

 "" Pachong URL configurationthe ' urlpatterns ' list routes URLs to views. For more information see:https://docs.djangoproject.com/en/1.10/topics/http/urls/examples:function views 1. ADD an import:from My_app import views 2. Add a URL to Urlpatterns:url (R ' ^$ ', views.home, Name= ' home ") class-based views 1. ADD an import:from other_app.views import Home 2. Add a URL to Urlpatterns:url (R ' ^$ ', Home.as_view (), name= ' Home ') including another URLconf 1. Import the Include () function:from django.conf.urls import URL, include 2. Add a URL to Urlpatterns:url (R ' ^blog/', include (' Blog.urls ')) "" "from Django.conf.urls import urlfrom django.contrib impo RT Adminfrom App1 Import viewsurlpatterns = [url (r ' ^admin/', admin.site.urls), url (r ' ^$ ', views.indexluoji), url (R ' ^index/', Views.indexluoji), url (r ' ^suggest/$ ', views.suggestluoji,name= "suggest"), # search field completion request URL (R ' ^search/ $ ', views.searchluoji,name= "search"), # Searching] 

2, write the logical processing function

Implementing search data in logical processing functions

(1) Get the user's search terms

(2) using the native Elasticsearch (search engine) interface, to achieve the search, annotated:ELASTICSEARCH-DSL is on the original Elasticsearch to do the encapsulation

Elasticsearch () method to connect the native Elasticsearch server

The search () method, the native Elasticsearch query method, supports the native Elasticsearch query statement, and the returned native statement result is the data in the dictionary form

Keyword highlighting in query statements

Returns the result of the query to the HTML page after the loop gets to the

From django.shortcuts import render# Create your views here.from django.shortcuts import Render,httpresponsefrom django.v Iews.generic.base Import viewfrom app1.models import lagoutype # Importing Operation Elasticsearch (search engine) class import Jsonfrom ELA Sticsearch Import Elasticsearch # Importing native Elasticsearch (search engine) interface client = Elasticsearch (hosts=[""]) # Connection native Elasticsearchdef Indexluoji (Request): Print (Request.method) # Gets the path of the user request return render (request, ' index.html ') def su Ggestluoji (Request): # Search Auto-complete logic processing key_words = Request.                                  Get.get (' s ', ') # gets to the request word Re_datas = [] If key_words:s = Lagoutype.search ()            # Instantiation of search query for Elasticsearch (search engine) class S = s.suggest (' my_suggest ', Key_words, completion={ "Field": "Suggest", "fuzzy": {"fuzziness": 1}, "Size": 5}) su Ggestions = S.execute_suggest () for match in SUggestions.my_suggest[0].options:source = Match._source re_datas.append (source["title"]) return                                       HttpResponse (Json.dumps (Re_datas), content_type= "Application/json") def Searchluoji (Request): # search Logic Processing key_words = Request. Get.get (' Q ', ') # gets to the request word response = Client.search (# native Elast         The search () method of the Icsearch interface, which is searching, can support the native Elasticsearch statement query index= "Lagou", # set the index name                                                  Doc_type= "Biao", # Sets the table name body={ # write Elasticsearch statement "query": {"Multi_match": {# mu Lti_match query "Query": key_words, # query keyword "fields": ["title",                      "description"] # query Field}}, "from": 0,                    # get "Size" from the first few: 10, # Get how many data "Highli         Ght ": {# query keyword highlighting processing" pre_tags ": [' <span class= ' KeyWord ' > '],                                     # Highlight start Tag "post_tags": [' </span> '], # Highlight end tag ' fields ': {                    # Highlight Set "title": {}, # Highlight field "description": {} # Highlight field}}) Total_nums = Respo nse["hits" ["Total"] # Gets the overall number of bars for the query result hit_list = [] # setting                                           A list to store the information that was searched, returned to the HTML page for hits in response["hits" ["Hits"]: # Loop query to the result hit_dict = {} # Set a dictionary to store looping results if "title" in Hit["highlight"]: # Judgment       Title field, if the highlighted field has a class capacity     hit_dict["title"] = "". Join (hit["Highlight" ["title"]) # get the title else:hit_dict["title" in the Highlight) =                           hit["_source" ["title"] # otherwise get the title if "description" in hit["highlight") that is not highlighted:    # Determine the Description field if the highlighted field has a class tolerance of hit_dict["description" = "". Join (hit["Highlight" ["description"]) [: 500] # Get the highlights in the description else:hit_dict["description"] = hit["_source" ["description"] # Otherwise get not the highlights of the DESCR                                       iption hit_dict["url"] = hit["_source" ["url"] # Get Return URL hit_list.append (hit_dict) # will get a dictionary of content to add to the list return render (Request, ' result.html ', {"all_hits": hit_list, "Ke Y_words ": key_words}) #显示页面和将列表和搜索词返回到html

3. HTML pages Receive search results

Note: Because the Django implementation prevents malicious code from being written, the HTML type code that is transferred to the HTML page by the variable will be automatically converted to a string display, which we need to use in the field that receives the variable: {% autoescape off%} {{ Receive variable} {% Endautoescape%}, to display the HTML code,

After the search because of a jump, so the search box in the search term will not exist, we need to pass the search results to the page, the search word also passed in to fill in the search box

<! DOCTYPE html >

Final effect

48 Python distributed crawler build search engine Scrapy explaining-elasticsearch (search engine) implements the search function with Django

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.