File size: 2,667 Bytes
57cf043
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import logging

import pandas as pd
from elasticsearch import Elasticsearch
from tqdm import tqdm


def create_index_elastic_abbreviation(
    df: pd.DataFrame,
    logger: logging.Logger | None,
):
    if logger is None:
        logger = logging.getLogger(__name__)

    # Подключение к Elasticsearch
    es = Elasticsearch(hosts='localhost:9200')

    INDEX_NAME = 'nmd_abbreviation_elastic'

    # Удаление старого индекса, если он существует
    if es.indices.exists(index=INDEX_NAME):
        es.indices.delete(index=INDEX_NAME)

    mapping = {
        "mappings": {
            "properties": {
                "abbreviation": {"type": "text", "analyzer": "russian"},
                "text": {"type": "text", "analyzer": "russian"},
            }
        }
    }

    # Создание индекса с указанным маппингом
    es.indices.create(index=INDEX_NAME, body=mapping)

    # Индексация документов
    for ind, row in tqdm(df.iterrows()):
        document = {'abbreviation': row['name'], 'text': row['definition']}

        # Индексирование документа в Elasticsearch
        es.index(index=INDEX_NAME, id=ind, body=document)

    if es.indices.exists(index=INDEX_NAME):
        logger.info(f"Index '{INDEX_NAME}' exists.")

    # # Подсчет количества документов в индексе
    count_response = es.count(index=INDEX_NAME)
    logger.info(f"Total documents in '{INDEX_NAME}': {count_response['count']}")

    # Поиск документов, где поле "person_full_name" содержит определенное значение "Александров Д.В."
    query = {
        "query": {
            "multi_match": {
                "query": "для нужен стандарт управления бизнес процессами компании?",
                "fuzziness": "AUTO",
                "minimum_should_match": "83%",
                "fields": ["text"],
            }
        },
        "highlight": {"fields": {"text": {}}},
    }

    # Выполнение поиска в Elasticsearch
    response = es.search(index=INDEX_NAME, body=query, size=1)
    logger.info(f"Number of hits: {response['hits']['total']['value']}")

    # Вывод результата поиска
    for hit in response['hits']['hits']:
        logger.info(hit)
        logger.info('=====')


if __name__ == '__main__':
    # Чтение CSV файла с данными
    df = pd.read_csv('/mnt/ntr_work/project/nmd800/data/abbreviations.csv')

    create_index_elastic_abbreviation(df)