|
22
|
1 |
from django.conf import settings |
|
77
|
2 |
from ldt.indexation import STORE |
|
22
|
3 |
import lucene |
|
|
4 |
import uuid |
|
|
5 |
|
|
|
6 |
__BOOLEAN_DICT = { |
|
|
7 |
'false':False, |
|
|
8 |
'true':True, |
|
|
9 |
'0':False, |
|
|
10 |
'1':True, |
|
|
11 |
't': True, |
|
|
12 |
'f':False |
|
|
13 |
} |
|
|
14 |
|
|
|
15 |
def boolean_convert(bool): |
|
|
16 |
if bool is None: |
|
|
17 |
return False |
|
|
18 |
if bool is True or bool is False: |
|
|
19 |
return bool |
|
|
20 |
key = str(bool).lower() |
|
|
21 |
return __BOOLEAN_DICT.get(key, False) |
|
|
22 |
|
|
|
23 |
|
|
|
24 |
def generate_uuid(): |
|
|
25 |
return unicode(uuid.uuid1()) |
|
|
26 |
|
|
|
27 |
|
|
|
28 |
#def normalize_tags(list): |
|
|
29 |
# nlist=[] |
|
|
30 |
# for tag in list: |
|
|
31 |
# tag = tag.lower() |
|
|
32 |
# nlist.append(tag) |
|
|
33 |
# taglist = dict().fromkeys(nlist).keys() |
|
|
34 |
# |
|
|
35 |
# return taglist |
|
|
36 |
|
|
|
37 |
|
|
|
38 |
class TextSearch(object): |
|
|
39 |
|
|
|
40 |
def query(self, field, query): |
|
|
41 |
indexSearcher = lucene.IndexSearcher(STORE) |
|
|
42 |
queryParser = lucene.QueryParser(lucene.Version.LUCENE_30, field, lucene.FrenchAnalyzer(lucene.Version.LUCENE_30)) |
|
|
43 |
queryParser.setDefaultOperator(lucene.QueryParser.Operator.AND) |
|
|
44 |
queryObj = queryParser.parse(query) |
|
|
45 |
hits = indexSearcher.search(queryObj, settings.LDT_MAX_SEARCH_NUMBER) |
|
|
46 |
|
|
|
47 |
res = [] |
|
|
48 |
for hit in hits.scoreDocs: |
|
|
49 |
doc = indexSearcher.doc(hit.doc) |
|
63
|
50 |
res.append({"external_id":doc.get("external_id"), "title":doc.get("title")}) |
|
22
|
51 |
indexSearcher.close() |
|
|
52 |
return res |
|
|
53 |
|
|
|
54 |
def queryAll(self, query): |
|
|
55 |
return self.query("all", query) |
|
|
56 |
|
|
|
57 |
|