aboutsummaryrefslogtreecommitdiff
path: root/benchmark/testelasticsearch.py
blob: accb8792d306448b4d075ca2c9118a8d8fa9e7ca (plain)
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
#! /usr/bin/env python
#-*- coding: utf-8 -*- 

import elasticsearch
from elasticsearch import client

from pyaggr3g470r import utils

# Connect to Elasticsearch node specified in the configuration file:
es = elasticsearch.Elasticsearch(hosts={"127.0.0.1" : 9200})

def delete_index():
    """
    Deletes all indexes.
    """
    es = elasticsearch.Elasticsearch(hosts={"127.0.0.1" : 9200})
    ic = client.IndicesClient(es.indices.client)
    try:
        ic.delete("")
    except:
        pass

def create_index(articles):
    """
    Creates the index.
    """
    for article in articles:
        res = es.index(
            index="pyaggr3g470r",
            doc_type="text",
            id=str(article.id),
            body={
                "title": article.title,
                "content": utils.clear_string(article.content)
            }
        )
    return True

def search(term):
    """
    Search a term.
    """
    try:
        es.search(index="pyaggr3g470r", body=
            {"query" : {
                    "filtered" : {
                        "query" : { 
                            "query_string" : { 
                                "default_field" : "content",
                                "query" : term
                            }
                        }
                    }
                }
            }, size=5000)
    except elasticsearch.exceptions.NotFoundError as e:
        pass
bgstack15