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

Functions

 init (engine_settings)
 
 search (query, params)
 
 _fetch_results (cur)
 

Variables

str engine_type = 'offline'
 
str host = "127.0.0.1"
 
str port = "5432"
 
str database = ""
 
str username = ""
 
str password = ""
 
str query_str = ""
 
int limit = 10
 
bool paging = True
 
str result_template = 'key-value.html'
 
 _connection = None
 

Detailed Description

PostgreSQL is a powerful and robust open source database.  Before configuring
the PostgreSQL engine, you must install the dependency ``psychopg2``.

Example
=======

Below is an example configuration:

.. code:: yaml

   - name: my_database
     engine: postgresql
     database: my_database
     username: searxng
     password: password
     query_str: 'SELECT * from my_table WHERE my_column = %(query)s'

Implementations
===============

Function Documentation

◆ _fetch_results()

searx.engines.postgresql._fetch_results ( cur)
protected

Definition at line 85 of file postgresql.py.

85def _fetch_results(cur):
86 results = []
87 titles = []
88
89 try:
90 titles = [column_desc.name for column_desc in cur.description]
91
92 for res in cur:
93 result = dict(zip(titles, map(str, res)))
94 result['template'] = result_template
95 results.append(result)
96
97 # no results to fetch
98 except psycopg2.ProgrammingError:
99 pass
100
101 return results

◆ init()

searx.engines.postgresql.init ( engine_settings)

Definition at line 57 of file postgresql.py.

57def init(engine_settings):
58 global _connection # pylint: disable=global-statement
59
60 if 'query_str' not in engine_settings:
61 raise ValueError('query_str cannot be empty')
62
63 if not engine_settings['query_str'].lower().startswith('select '):
64 raise ValueError('only SELECT query is supported')
65
66 _connection = psycopg2.connect(
67 database=database,
68 user=username,
69 password=password,
70 host=host,
71 port=port,
72 )
73
74

◆ search()

searx.engines.postgresql.search ( query,
params )

Definition at line 75 of file postgresql.py.

75def search(query, params):
76 query_params = {'query': query}
77 query_to_run = query_str + ' LIMIT {0} OFFSET {1}'.format(limit, (params['pageno'] - 1) * limit)
78
79 with _connection:
80 with _connection.cursor() as cur:
81 cur.execute(query_to_run, query_params)
82 return _fetch_results(cur)
83
84

Variable Documentation

◆ _connection

searx.engines.postgresql._connection = None
protected

Definition at line 54 of file postgresql.py.

◆ database

str searx.engines.postgresql.database = ""

Definition at line 39 of file postgresql.py.

◆ engine_type

str searx.engines.postgresql.engine_type = 'offline'

Definition at line 31 of file postgresql.py.

◆ host

str searx.engines.postgresql.host = "127.0.0.1"

Definition at line 33 of file postgresql.py.

◆ limit

int searx.engines.postgresql.limit = 10

Definition at line 51 of file postgresql.py.

◆ paging

bool searx.engines.postgresql.paging = True

Definition at line 52 of file postgresql.py.

◆ password

str searx.engines.postgresql.password = ""

Definition at line 45 of file postgresql.py.

◆ port

str searx.engines.postgresql.port = "5432"

Definition at line 36 of file postgresql.py.

◆ query_str

str searx.engines.postgresql.query_str = ""

Definition at line 48 of file postgresql.py.

◆ result_template

str searx.engines.postgresql.result_template = 'key-value.html'

Definition at line 53 of file postgresql.py.

◆ username

str searx.engines.postgresql.username = ""

Definition at line 42 of file postgresql.py.