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

Functions

 init (engine_settings)
 
EngineResults search (query, params)
 
EngineResults _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
 
 _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()

EngineResults searx.engines.postgresql._fetch_results ( cur)
protected

Definition at line 86 of file postgresql.py.

86def _fetch_results(cur) -> EngineResults:
87 res = EngineResults()
88 try:
89 titles = [column_desc.name for column_desc in cur.description]
90 for row in cur:
91 kvmap = dict(zip(titles, map(str, row)))
92 res.add(res.types.KeyValue(kvmap=kvmap))
93
94 # no results to fetch
95 except psycopg2.ProgrammingError:
96 pass
97
98 return res

◆ init()

searx.engines.postgresql.init ( engine_settings)

Definition at line 58 of file postgresql.py.

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

◆ search()

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

Definition at line 76 of file postgresql.py.

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

Variable Documentation

◆ _connection

searx.engines.postgresql._connection = None
protected

Definition at line 55 of file postgresql.py.

◆ database

str searx.engines.postgresql.database = ""

Definition at line 41 of file postgresql.py.

◆ engine_type

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

Definition at line 33 of file postgresql.py.

◆ host

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

Definition at line 35 of file postgresql.py.

◆ limit

int searx.engines.postgresql.limit = 10

Definition at line 53 of file postgresql.py.

◆ paging

bool searx.engines.postgresql.paging = True

Definition at line 54 of file postgresql.py.

◆ password

str searx.engines.postgresql.password = ""

Definition at line 47 of file postgresql.py.

◆ port

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

Definition at line 38 of file postgresql.py.

◆ query_str

str searx.engines.postgresql.query_str = ""

Definition at line 50 of file postgresql.py.

◆ username

str searx.engines.postgresql.username = ""

Definition at line 44 of file postgresql.py.