.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
elasticsearch.py
Go to the documentation of this file.
1# SPDX-License-Identifier: AGPL-3.0-or-later
2""".. sidebar:: info
3
4 - :origin:`elasticsearch.py <searx/engines/elasticsearch.py>`
5 - `Elasticsearch <https://www.elastic.co/elasticsearch/>`_
6 - `Elasticsearch Guide
7 <https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html>`_
8 - `Install Elasticsearch
9 <https://www.elastic.co/guide/en/elasticsearch/reference/current/install-elasticsearch.html>`_
10
11Elasticsearch_ supports numerous ways to query the data it is storing. At the
12moment the engine supports the most popular search methods (``query_type``):
13
14- ``match``,
15- ``simple_query_string``,
16- ``term`` and
17- ``terms``.
18
19If none of the methods fit your use case, you can select ``custom`` query type
20and provide the JSON payload to submit to Elasticsearch in
21``custom_query_json``.
22
23Example
24=======
25
26The following is an example configuration for an Elasticsearch_ instance with
27authentication configured to read from ``my-index`` index.
28
29.. code:: yaml
30
31 - name: elasticsearch
32 shortcut: es
33 engine: elasticsearch
34 base_url: http://localhost:9200
35 username: elastic
36 password: changeme
37 index: my-index
38 query_type: match
39 # custom_query_json: '{ ... }'
40 enable_http: true
41
42"""
43
44from json import loads, dumps
45from searx.exceptions import SearxEngineAPIException
46
47
48base_url = 'http://localhost:9200'
49username = ''
50password = ''
51index = ''
52search_url = '{base_url}/{index}/_search'
53query_type = 'match'
54custom_query_json = {}
55show_metadata = False
56categories = ['general']
57
58
59def init(engine_settings):
60 if 'query_type' in engine_settings and engine_settings['query_type'] not in _available_query_types:
61 raise ValueError('unsupported query type', engine_settings['query_type'])
62
63 if index == '':
64 raise ValueError('index cannot be empty')
65
66
67def request(query, params):
68 if query_type not in _available_query_types:
69 return params
70
71 if username and password:
72 params['auth'] = (username, password)
73
74 params['url'] = search_url.format(base_url=base_url, index=index)
75 params['method'] = 'GET'
76 params['data'] = dumps(_available_query_types[query_type](query))
77 params['headers']['Content-Type'] = 'application/json'
78
79 return params
80
81
82def _match_query(query):
83 """
84 The standard for full text queries.
85 searx format: "key:value" e.g. city:berlin
86 REF: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html
87 """
88
89 try:
90 key, value = query.split(':')
91 except Exception as e:
92 raise ValueError('query format must be "key:value"') from e
93
94 return {"query": {"match": {key: {'query': value}}}}
95
96
98 """
99 Accepts query strings, but it is less strict than query_string
100 The field used can be specified in index.query.default_field in Elasticsearch.
101 REF: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html
102 """
103
104 return {'query': {'simple_query_string': {'query': query}}}
105
106
107def _term_query(query):
108 """
109 Accepts one term and the name of the field.
110 searx format: "key:value" e.g. city:berlin
111 REF: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html
112 """
113
114 try:
115 key, value = query.split(':')
116 except Exception as e:
117 raise ValueError('query format must be key:value') from e
118
119 return {'query': {'term': {key: value}}}
120
121
122def _terms_query(query):
123 """
124 Accepts multiple terms and the name of the field.
125 searx format: "key:value1,value2" e.g. city:berlin,paris
126 REF: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html
127 """
128
129 try:
130 key, values = query.split(':')
131 except Exception as e:
132 raise ValueError('query format must be key:value1,value2') from e
133
134 return {'query': {'terms': {key: values.split(',')}}}
135
136
137def _custom_query(query):
138 key, value = query.split(':')
139 custom_query = custom_query_json
140 for query_key, query_value in custom_query.items():
141 if query_key == '{{KEY}}':
142 custom_query[key] = custom_query.pop(query_key)
143 if query_value == '{{VALUE}}':
144 custom_query[query_key] = value
145 return custom_query
146
147
148def response(resp):
149 results = []
150
151 resp_json = loads(resp.text)
152 if 'error' in resp_json:
153 raise SearxEngineAPIException(resp_json['error'])
154
155 for result in resp_json['hits']['hits']:
156 r = {key: str(value) if not key.startswith('_') else value for key, value in result['_source'].items()}
157 r['template'] = 'key-value.html'
158
159 if show_metadata:
160 r['metadata'] = {'index': result['_index'], 'id': result['_id'], 'score': result['_score']}
161
162 results.append(r)
163
164 return results
165
166
167_available_query_types = {
168 # Full text queries
169 # https://www.elastic.co/guide/en/elasticsearch/reference/current/full-text-queries.html
170 'match': _match_query,
171 'simple_query_string': _simple_query_string_query,
172 # Term-level queries
173 # https://www.elastic.co/guide/en/elasticsearch/reference/current/term-level-queries.html
174 'term': _term_query,
175 'terms': _terms_query,
176 # Query JSON defined by the instance administrator.
177 'custom': _custom_query,
178}