.oO SearXNG Developer Documentation Oo.
Loading...
Searching...
No Matches
searx.engines.elasticsearch Namespace Reference

Functions

 init (engine_settings)
 
 request (query, params)
 
 _match_query (query)
 
 _simple_query_string_query (query)
 
 _term_query (query)
 
 _terms_query (query)
 
 _custom_query (query)
 
 response (resp)
 

Variables

str base_url = 'http://localhost:9200'
 
str username = ''
 
str password = ''
 
str index = ''
 
str search_url = '{base_url}/{index}/_search'
 
str query_type = 'match'
 
dict custom_query_json = {}
 
bool show_metadata = False
 
list categories = ['general']
 
dict _available_query_types
 

Detailed Description

.. sidebar:: info

   - :origin:`elasticsearch.py <searx/engines/elasticsearch.py>`
   - `Elasticsearch <https://www.elastic.co/elasticsearch/>`_
   - `Elasticsearch Guide
     <https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html>`_
   - `Install Elasticsearch
     <https://www.elastic.co/guide/en/elasticsearch/reference/current/install-elasticsearch.html>`_

Elasticsearch_ supports numerous ways to query the data it is storing.  At the
moment the engine supports the most popular search methods (``query_type``):

- ``match``,
- ``simple_query_string``,
- ``term`` and
- ``terms``.

If none of the methods fit your use case, you can select ``custom`` query type
and provide the JSON payload to submit to Elasticsearch in
``custom_query_json``.

Example
=======

The following is an example configuration for an Elasticsearch_ instance with
authentication configured to read from ``my-index`` index.

.. code:: yaml

  - name: elasticsearch
    shortcut: es
    engine: elasticsearch
    base_url: http://localhost:9200
    username: elastic
    password: changeme
    index: my-index
    query_type: match
    # custom_query_json: '{ ... }'
    enable_http: true

Function Documentation

◆ _custom_query()

searx.engines.elasticsearch._custom_query ( query)
protected

Definition at line 137 of file elasticsearch.py.

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

◆ _match_query()

searx.engines.elasticsearch._match_query ( query)
protected
The standard for full text queries.
searx format: "key:value" e.g. city:berlin
REF: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html

Definition at line 82 of file elasticsearch.py.

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

◆ _simple_query_string_query()

searx.engines.elasticsearch._simple_query_string_query ( query)
protected
Accepts query strings, but it is less strict than query_string
The field used can be specified in index.query.default_field in Elasticsearch.
REF: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-simple-query-string-query.html

Definition at line 97 of file elasticsearch.py.

97def _simple_query_string_query(query):
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

◆ _term_query()

searx.engines.elasticsearch._term_query ( query)
protected
Accepts one term and the name of the field.
searx format: "key:value" e.g. city:berlin
REF: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html

Definition at line 107 of file elasticsearch.py.

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

◆ _terms_query()

searx.engines.elasticsearch._terms_query ( query)
protected
Accepts multiple terms and the name of the field.
searx format: "key:value1,value2" e.g. city:berlin,paris
REF: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html

Definition at line 122 of file elasticsearch.py.

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

◆ init()

searx.engines.elasticsearch.init ( engine_settings)

Definition at line 59 of file elasticsearch.py.

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

◆ request()

searx.engines.elasticsearch.request ( query,
params )

Definition at line 67 of file elasticsearch.py.

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

◆ response()

searx.engines.elasticsearch.response ( resp)

Definition at line 148 of file elasticsearch.py.

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

Variable Documentation

◆ _available_query_types

dict searx.engines.elasticsearch._available_query_types
protected
Initial value:
1= {
2 # Full text queries
3 # https://www.elastic.co/guide/en/elasticsearch/reference/current/full-text-queries.html
4 'match': _match_query,
5 'simple_query_string': _simple_query_string_query,
6 # Term-level queries
7 # https://www.elastic.co/guide/en/elasticsearch/reference/current/term-level-queries.html
8 'term': _term_query,
9 'terms': _terms_query,
10 # Query JSON defined by the instance administrator.
11 'custom': _custom_query,
12}

Definition at line 167 of file elasticsearch.py.

◆ base_url

str searx.engines.elasticsearch.base_url = 'http://localhost:9200'

Definition at line 48 of file elasticsearch.py.

◆ categories

list searx.engines.elasticsearch.categories = ['general']

Definition at line 56 of file elasticsearch.py.

◆ custom_query_json

dict searx.engines.elasticsearch.custom_query_json = {}

Definition at line 54 of file elasticsearch.py.

◆ index

str searx.engines.elasticsearch.index = ''

Definition at line 51 of file elasticsearch.py.

◆ password

str searx.engines.elasticsearch.password = ''

Definition at line 50 of file elasticsearch.py.

◆ query_type

str searx.engines.elasticsearch.query_type = 'match'

Definition at line 53 of file elasticsearch.py.

◆ search_url

str searx.engines.elasticsearch.search_url = '{base_url}/{index}/_search'

Definition at line 52 of file elasticsearch.py.

◆ show_metadata

bool searx.engines.elasticsearch.show_metadata = False

Definition at line 55 of file elasticsearch.py.

◆ username

str searx.engines.elasticsearch.username = ''

Definition at line 49 of file elasticsearch.py.