Exemple #1
0
 def get(self, request, *args, **kwargs):
     # 接收 DSL 参数执行搜索
     dsl = request.GET.get('dsl')  # <type 'unicode'>
     dsl = json.loads(dsl)  # <type 'dict'>
     # ElasticSearch(单例模式)
     from elasticsearchclient.elasticsearchclient import es_client
     print '>>>>>>>>>> ElasticSearch(Singleton)'
     print id(es_client)
     print '>>>>>>>>>> ElasticSearch(Singleton)'
     search = es_client.search(
         index='douban',
         doc_type='movie',
         query=dsl,
         es_from=0,
         size=250,
     )  # <type 'dict'>
     ret = json.JSONEncoder(indent=4).encode(search)  # <type 'str'>
     print '----------------------------------------------------------------------------------------'
     print type(dsl)
     print dsl
     print '----------------------------------------------------------------------------------------'
     print type(ret)
     print ret
     print '----------------------------------------------------------------------------------------'
     return HttpResponse(content=ret,
                         content_type='application/json; charset=UTF-8')
Exemple #2
0
    def get(self, request, *args, **kwargs):
        """
        搜索 ElasticSearch
        """
        # 关键字参数和分页参数
        keyword = request.GET.get("keyword")
        page = request.GET.get("page", 1)
        print "----------------------------------------------------------------------------------------"
        dsl_movie = {
            "query": {
                "bool": {
                    "should": [
                        {"match": {"title": keyword}},
                        {"match": {"desc": keyword}},
                        {"match": {"quote": keyword}},
                    ]
                }
            }
        }
        # [1] 创建客户端对象 查询ElasticSearch
        # from pyelasticsearch import ElasticSearch
        # es = ElasticSearch(
        #     urls='http://192.168.100.100',
        #     port=9200,
        # )
        # [2] ElasticSearch(单例模式)
        from elasticsearchclient.elasticsearchclient import es_client

        print ">>>>>>>>>> ElasticSearch(Singleton)"
        print id(es_client)
        print ">>>>>>>>>> ElasticSearch(Singleton)"
        # ElasticSearch 分页参数: es_from, size
        search = es_client.search(index="douban", doc_type="movie", query=dsl_movie, es_from=0, size=250)
        # print json.JSONEncoder(indent=4).encode(search)
        hits = search["hits"]["hits"]
        movies = [hit["_source"] for hit in hits]
        print "----------------------------------------------------------------------------------------"
        # 分页
        paginator = Paginator(object_list=movies, per_page=10)
        try:
            pager = paginator.page(page)
        except PageNotAnInteger:
            pager = paginator.page(1)
        except EmptyPage:
            pager = paginator.page(paginator.num_pages)
            pass
        # 分页片段中使用 pager.queries 达到在翻页时带着查询参数的目的
        pager.queries = "keyword=%s" % (request.GET.get("keyword") or "",)
        # [网页模板]和[通用分页片段(pagination_jinja.html)]中使用 "page" 来访问 Page object
        context = {}
        context["page"] = pager
        return coffin_render(request, "movie.html", context)
Exemple #3
0
    def get(self, request, *args, **kwargs):
        # 接收 DSL 参数执行搜索
        dsl = request.GET.get("dsl")  # <type 'unicode'>
        dsl = json.loads(dsl)  # <type 'dict'>
        # ElasticSearch(单例模式)
        from elasticsearchclient.elasticsearchclient import es_client

        print ">>>>>>>>>> ElasticSearch(Singleton)"
        print id(es_client)
        print ">>>>>>>>>> ElasticSearch(Singleton)"
        search = es_client.search(index="douban", doc_type="movie", query=dsl, es_from=0, size=250)  # <type 'dict'>
        ret = json.JSONEncoder(indent=4).encode(search)  # <type 'str'>
        print "----------------------------------------------------------------------------------------"
        print type(dsl)
        print dsl
        print "----------------------------------------------------------------------------------------"
        print type(ret)
        print ret
        print "----------------------------------------------------------------------------------------"
        return HttpResponse(content=ret, content_type="application/json; charset=UTF-8")
Exemple #4
0
 def get(self, request, *args, **kwargs):
     """
     搜索 ElasticSearch
     """
     # 关键字参数和分页参数
     keyword = request.GET.get('keyword')
     page = request.GET.get('page', 1)
     print '----------------------------------------------------------------------------------------'
     dsl_movie = {
         'query': {
             'bool': {
                 'should': [{
                     'match': {
                         'title': keyword,
                     }
                 }, {
                     'match': {
                         'desc': keyword,
                     }
                 }, {
                     'match': {
                         'quote': keyword,
                     }
                 }]
             }
         }
     }
     # [1] 创建客户端对象 查询ElasticSearch
     # from pyelasticsearch import ElasticSearch
     # es = ElasticSearch(
     #     urls='http://192.168.100.100',
     #     port=9200,
     # )
     # [2] ElasticSearch(单例模式)
     from elasticsearchclient.elasticsearchclient import es_client
     print '>>>>>>>>>> ElasticSearch(Singleton)'
     print id(es_client)
     print '>>>>>>>>>> ElasticSearch(Singleton)'
     # ElasticSearch 分页参数: es_from, size
     search = es_client.search(
         index='douban',
         doc_type='movie',
         query=dsl_movie,
         es_from=0,
         size=250,
     )
     # print json.JSONEncoder(indent=4).encode(search)
     hits = search['hits']['hits']
     movies = [hit['_source'] for hit in hits]
     print '----------------------------------------------------------------------------------------'
     # 分页
     paginator = Paginator(object_list=movies, per_page=10)
     try:
         pager = paginator.page(page)
     except PageNotAnInteger:
         pager = paginator.page(1)
     except EmptyPage:
         pager = paginator.page(paginator.num_pages)
         pass
     # 分页片段中使用 pager.queries 达到在翻页时带着查询参数的目的
     pager.queries = "keyword=%s" % (request.GET.get('keyword') or '', )
     # [网页模板]和[通用分页片段(pagination_jinja.html)]中使用 "page" 来访问 Page object
     context = {}
     context['page'] = pager
     return coffin_render(request, 'movie.html', context)