@@ -0,0 +1,142 @@
1+from json import loads, dumps
2+from lxml import html
3+from urllib.parse import quote, urljoin
4+from requests.auth import HTTPBasicAuth
5+from searx.utils import extract_text, get_torrent_size
6+
7+
8+base_url = 'http://localhost:9200'
9+username = ''
10+password = ''
11+index = ''
12+search_url = base_url + '/' + index + '/_search'
13+query_type = 'match'
14+custom_query_json = {}
15+show_metadata = False
16+categories = ['general']
17+
18+
19+def init(engine_settings):
20+ if 'query_type' in engine_settings and engine_settings['query_type'] not in _available_query_types:
21+ raise ValueError('unsupported query type', engine_settings['query_type'])
22+
23+ if index == '':
24+ raise ValueError('index cannot be empty')
25+
26+
27+def request(query, params):
28+ if query_type not in _available_query_types:
29+ return params
30+
31+ if username and password:
32+ params['auth'] = HTTPBasicAuth(username, password)
33+
34+ params['url'] = search_url
35+ params['method'] = 'GET'
36+ params['data'] = dumps(_available_query_types[query_type](query))
37+ params['headers']['Content-Type'] = 'application/json'
38+
39+ return params
40+
41+
42+def _match_query(query):
43+ """
44+ The standard for full text queries.
45+ searx format: "key:value" e.g. city:berlin
46+ REF: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html
47+ """
48+
49+ try:
50+ key, value = query.split(':')
51+ except:
52+ raise ValueError('query format must be "key:value"')
53+
54+ return {"query": {"match": {key: {'query': value}}}}
55+
56+
57+def _simple_query_string_query(query):
58+ """
59+ Accepts query strings, but it is less strict than query_string
60+ The field used can be specified in index.query.default_field in Elasticsearch.
61+ REF: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html
62+ """
63+
64+ return {'query': {'simple_query_string': {'query': query}}}
65+
66+
67+def _term_query(query):
68+ """
69+ Accepts one term and the name of the field.
70+ searx format: "key:value" e.g. city:berlin
71+ REF: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html
72+ """
73+
74+ try:
75+ key, value = query.split(':')
76+ except:
77+ raise ValueError('query format must be key:value')
78+
79+ return {'query': {'term': {key: value}}}
80+
81+
82+def _terms_query(query):
83+ """
84+ Accepts multiple terms and the name of the field.
85+ searx format: "key:value1,value2" e.g. city:berlin,paris
86+ REF: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html
87+ """
88+
89+ try:
90+ key, values = query.split(':')
91+ except:
92+ raise ValueError('query format must be key:value1,value2')
93+
94+ return {'query': {'terms': {key: values.split(',')}}}
95+
96+
97+def _custom_query(query):
98+ key, value = query.split(':')
99+ custom_query = custom_query_json
100+ for query_key, query_value in custom_query.items():
101+ if query_key == '{{KEY}}':
102+ custom_query[key] = custom_query.pop(query_key)
103+ if query_value == '{{VALUE}}':
104+ custom_query[query_key] = value
105+ return custom_query
106+
107+
108+def response(resp):
109+ results = []
110+
111+ resp_json = loads(resp.text)
112+ if 'error' in resp_json:
113+ raise Exception(resp_json['error'])
114+
115+ for result in resp_json['hits']['hits']:
116+ r = {key: str(value) if not key.startswith('_') else value for key, value in result['_source'].items()}
117+ r['template'] = 'key-value.html'
118+
119+ if show_metadata:
120+ r['metadata'] = {'index': result['_index'],
121+ 'id': result['_id'],
122+ 'score': result['_score']}
123+
124+ results.append(r)
125+
126+ return results
127+
128+
129+_available_query_types = {
130+ # Full text queries
131+ # https://www.elastic.co/guide/en/elasticsearch/reference/current/full-text-queries.html
132+ 'match': _match_query,
133+ 'simple_query_string': _simple_query_string_query,
134+
135+ # Term-level queries
136+ # https://www.elastic.co/guide/en/elasticsearch/reference/current/term-level-queries.html
137+ 'term': _term_query,
138+ 'terms': _terms_query,
139+
140+ # Query JSON defined by the instance administrator.
141+ 'custom': _custom_query,
142+}