<commit_before># -*- coding: utf-8 -*-
from gevent import monkey
monkey.patch_all()

import sys
from ConfigParser import ConfigParser

from flask import Flask, request, jsonify, abort
from openprocurement.search.engine import SearchEngine

# create Flask app

search_server = Flask(__name__)
search_server.config.from_object(__name__)

# load config

config_parser = ConfigParser()
if len(sys.argv) > 2 and sys.argv[1] == '--paste':
    config_parser.read(sys.argv[2])
elif len(sys.argv) > 1 and sys.argv[1][-4:] == '.ini':
    config_parser.read(sys.argv[1])
search_config = dict(config_parser.items('search_engine'))

# create engine

search_engine = SearchEngine(search_config)

TENDER_INDEX_KEYS = ['tenders', 'ocds']
PLAN_INDEX_KEYS = ['plans',]
ORGS_INDEX_KEYS = ['orgs',]

def rename_index_names(config, index_list):
    for i, name in enumerate(index_list):
        rename_key = 'rename_' + name
        if rename_key in config:
            index_list[i] = config[rename_key]
    return index_list

rename_index_names(search_config, TENDER_INDEX_KEYS)
rename_index_names(search_config, PLAN_INDEX_KEYS)
rename_index_names(search_config, ORGS_INDEX_KEYS)
search_server.logger.info("Start with indexes %s %s %s",
    str(TENDER_INDEX_KEYS), str(PLAN_INDEX_KEYS), str(ORGS_INDEX_KEYS))




def match_query(query, field, type_=None, operator=None, analyzer=None):
    count = len(query)
    query = " ".join(query)
    query = {"query": query}
    if count > 1 and operator:
        query["operator"] = operator
    if analyzer:
        query["analyzer"] = analyzer
    if type_:
        query["type"] = type_
    return {"match": {field: query}}


def prefix_query(query, field):
    body = []
    for q in query:
        body.append({"prefix": {field: q}})
    if len(body) == 1:
        return body[0]
    return {"bool": {"should": body}}


def range_query(query, field):
    body = []
    for q in query:
        if q.find('-') < 0:
            res = prefix_query([q], field)
            body.append(res)
        else:
            beg, end = q.split('-', 1)
            body.append({"range": {
                field: {"gte": beg, "lte": end}
                }})
    if len(body) == 1:
        return body[0]
    return {"bool": {"should": body}}


def dates_query(query, args):
    op, key = args
    body = {"range": {
        key: {
            op: query,
            "time_zone": "+2:00",
        }}}
    return body


prefix_map = {
    'cpv': 'items.classification.id',
    'dkpp': 'items.additionalClassifications.id',
}
match_map = {
    'tid': 'tenderID',
    'pid': 'planID',
    'edrpou': 'procuringEntity.identifier.id',
    'procedure': 'procurementMethod',
    'proc_type': 'procurementMethodType',<commit_msg>search add plan_cpv and plan_dkpp<commit_after># -*- coding: utf-8 -*-
from gevent import monkey
monkey.patch_all()

import sys
from ConfigParser import ConfigParser

from flask import Flask, request, jsonify, abort
from openprocurement.search.engine import SearchEngine

# create Flask app

search_server = Flask(__name__)
search_server.config.from_object(__name__)

# load config

config_parser = ConfigParser()
if len(sys.argv) > 2 and sys.argv[1] == '--paste':
    config_parser.read(sys.argv[2])
elif len(sys.argv) > 1 and sys.argv[1][-4:] == '.ini':
    config_parser.read(sys.argv[1])
search_config = dict(config_parser.items('search_engine'))

# create engine

search_engine = SearchEngine(search_config)

TENDER_INDEX_KEYS = ['tenders', 'ocds']
PLAN_INDEX_KEYS = ['plans',]
ORGS_INDEX_KEYS = ['orgs',]

def rename_index_names(config, index_list):
    for i, name in enumerate(index_list):
        rename_key = 'rename_' + name
        if rename_key in config:
            index_list[i] = config[rename_key]
    return index_list

rename_index_names(search_config, TENDER_INDEX_KEYS)
rename_index_names(search_config, PLAN_INDEX_KEYS)
rename_index_names(search_config, ORGS_INDEX_KEYS)
search_server.logger.info("Start with indexes %s %s %s",
    str(TENDER_INDEX_KEYS), str(PLAN_INDEX_KEYS), str(ORGS_INDEX_KEYS))




def match_query(query, field, type_=None, operator=None, analyzer=None):
    count = len(query)
    query = " ".join(query)
    query = {"query": query}
    if count > 1 and operator:
        query["operator"] = operator
    if analyzer:
        query["analyzer"] = analyzer
    if type_:
        query["type"] = type_
    return {"match": {field: query}}


def prefix_query(query, field):
    body = []
    for q in query:
        body.append({"prefix": {field: q}})
    if len(body) == 1:
        return body[0]
    return {"bool": {"should": body}}


def range_query(query, field):
    body = []
    for q in query:
        if q.find('-') < 0:
            res = prefix_query([q], field)
            body.append(res)
        else:
            beg, end = q.split('-', 1)
            body.append({"range": {
                field: {"gte": beg, "lte": end}
                }})
    if len(body) == 1:
        return body[0]
    return {"bool": {"should": body}}


def dates_query(query, args):
    op, key = args
    body = {"range": {
        key: {
            op: query,
            "time_zone": "+2:00",
        }}}
    return body


prefix_map = {
    'cpv': 'items.classification.id',
    'dkpp': 'items.additionalClassifications.id',
    'plan_cpv': 'classification.id',
    'plan_dkpp': 'additionalClassifications.id',
}
match_map = {
    'tid': 'tenderID',
    'pid': 'planID',
    'edrpou': 'procuringEntity.identifier.id',
    'procedure': 'procurementMethod',
    'proc_type': 'procurementMethodType',
{'fraction_non_alphanumeric': 0.14326750448833034, 'fraction_numerical': 0.006463195691202872, 'mean_word_length': 3.1918735891647856, 'pattern_counts': {'":': 24, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1996757', 'n_tokens_mistral': 1933, 'n_tokens_neox': 1872, 'n_words': 435}