| author | ymh <ymh.work@gmail.com> |
| Mon, 20 May 2013 18:02:37 +0200 | |
| changeset 1191 | b6e0b1811723 |
| parent 1190 | 129d45eec68c |
| child 1320 | 88ce48689c14 |
| permissions | -rw-r--r-- |
| 719 | 1 |
# -*- coding: utf-8 -*- |
2 |
''' |
|
3 |
Created on Jul 30, 2012 |
|
4 |
||
5 |
@author: ymh |
|
6 |
''' |
|
|
1190
129d45eec68c
Clean warning and errors for Django 1.5
ymh <ymh.work@gmail.com>
parents:
1181
diff
changeset
|
7 |
from haystack.backends import BaseEngine, elasticsearch_backend |
| 1117 | 8 |
from haystack.exceptions import MissingDependency |
9 |
from haystack.utils import get_identifier |
|
| 1191 | 10 |
#from ldt.ldt_utils.models import Segment |
| 1117 | 11 |
import collections |
12 |
try: |
|
13 |
import requests |
|
14 |
except ImportError: |
|
15 |
raise MissingDependency("The 'elasticsearch' backend requires the installation of 'requests'.") |
|
16 |
try: |
|
17 |
import pyelasticsearch |
|
18 |
except ImportError: |
|
19 |
raise MissingDependency("The 'elasticsearch' backend requires the installation of 'pyelasticsearch'. Please refer to the documentation.") |
|
20 |
||
21 |
||
| 719 | 22 |
|
23 |
class ElasticsearchSearchBackend(elasticsearch_backend.ElasticsearchSearchBackend): |
|
24 |
||
25 |
def build_search_kwargs(self, query_string, sort_by=None, start_offset=0, end_offset=None, |
|
26 |
fields='', highlight=False, facets=None, |
|
27 |
date_facets=None, query_facets=None, |
|
28 |
narrow_queries=None, spelling_query=None, |
|
29 |
within=None, dwithin=None, distance_point=None, |
|
30 |
models=None, limit_to_registered_models=None, |
|
31 |
result_class=None): |
|
32 |
||
33 |
kwargs = super(ElasticsearchSearchBackend, self).build_search_kwargs(query_string, sort_by=sort_by, start_offset=start_offset, end_offset=end_offset, |
|
34 |
fields=fields, highlight=highlight, facets=facets, |
|
35 |
date_facets=date_facets, query_facets=query_facets, |
|
36 |
narrow_queries=narrow_queries, spelling_query=spelling_query, |
|
37 |
within=within, dwithin=dwithin, distance_point=distance_point, |
|
38 |
models=models, limit_to_registered_models=limit_to_registered_models, |
|
39 |
result_class=result_class) |
|
40 |
||
41 |
#TODO : try to make list of field dynamic |
|
42 |
#TODO : How to handle multiple |
|
43 |
if highlight: |
|
44 |
fields_def = { } |
|
45 |
||
| 1191 | 46 |
if models is None or len(models) == 0 :#or Segment in models: |
| 719 | 47 |
fields_def['tags'] = {} |
48 |
fields_def['title'] = {} |
|
49 |
fields_def['abstract'] = {} |
|
50 |
||
51 |
kwargs['highlight'] = { |
|
52 |
'pre_tags' : ["<span class='highlight'>"], |
|
53 |
'post_tags' : ["</span>"], |
|
54 |
"number_of_fragments" : 0, |
|
55 |
'fields': fields_def |
|
56 |
} |
|
57 |
||
58 |
return kwargs |
|
59 |
||
| 1181 | 60 |
|
| 1117 | 61 |
def remove(self, obj_or_string, commit=True): |
62 |
||
63 |
if not self.setup_complete: |
|
64 |
try: |
|
65 |
self.setup() |
|
66 |
except (requests.RequestException, pyelasticsearch.ElasticHttpError), e: |
|
67 |
if not self.silently_fail: |
|
68 |
raise |
|
69 |
||
70 |
self.log.error("Failed to remove document '%s' from Elasticsearch: %s", repr(obj_or_string), e) |
|
71 |
return |
|
72 |
||
73 |
if isinstance(obj_or_string, collections.Iterable) and not isinstance(obj_or_string, basestring): |
|
74 |
ids = [get_identifier(elt) for elt in obj_or_string] |
|
75 |
if not ids: |
|
76 |
return |
|
77 |
q = {'ids' : {'values' : ids}} |
|
78 |
self.conn.delete_by_query(self.index_name, 'modelresult', q) |
|
79 |
else: |
|
80 |
return super(ElasticsearchSearchBackend, self).remove(obj_or_string, commit=commit) |
|
| 719 | 81 |
|
82 |
||
83 |
class ElasticsearchSearchEngine(BaseEngine): |
|
84 |
backend = ElasticsearchSearchBackend |
|
85 |
query = elasticsearch_backend.ElasticsearchSearchQuery |