Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use elasticsearch-py to access elasticsearch instead of requests. #2

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,9 @@ ESTester tests
--------------

In order to run ESTester tests, make sure you have ElasticSearch installed locally and it is up ::

make setup
make test
make tests


Compatibility
Expand Down
92 changes: 24 additions & 68 deletions estester/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import json
import time
import unittest
import urllib

import requests
from elasticsearch import Elasticsearch


__author__ = "Tatiana Al-Chueyr Pereira Martins"
Expand Down Expand Up @@ -63,6 +60,7 @@ class ElasticSearchQueryTestCase(ExtendedTestCase):
index = "sample.test" # must be lower case
reset_index = True # warning: if this is True, index will be cleared up
host = "http://0.0.0.0:9200/"
es = Elasticsearch(host)
mappings = {}
proxies = {}
fixtures = []
Expand Down Expand Up @@ -107,15 +105,13 @@ def create_index(self):
(i) http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/
configuring-analyzers.html
"""
url = "{0}{1}/"
url = url.format(self.host, self.index)

data = {}
if self.mappings:
data["mappings"] = self.mappings
if self.settings:
data["settings"] = self.settings
json_data = json.dumps(data)
response = requests.put(url, proxies=self.proxies, data=json_data)
self.es.indices.create(index=self.index, body=data)

def load_fixtures(self):
"""
Expand Down Expand Up @@ -146,52 +142,33 @@ def load_fixtures(self):
body: json with fields of values of document
"""
for doc in self.fixtures:
doc_type = urllib.quote_plus(doc["type"])
doc_id = urllib.quote_plus(doc["id"])
doc_body = doc["body"]
url = "{0}{1}/{2}/{3}"
url = url.format(self.host, self.index, doc_type, doc_id)
response = requests.put(
url,
data=json.dumps(doc_body),
proxies=self.proxies)
if not response.status_code in [200, 201]:
raise ElasticSearchException(response.text)
time.sleep(self.timeout)
self.es.index(
index=self.index, doc_type=doc["type"],
id=doc["id"], body=doc["body"], refresh=True
)
# http://0.0.0.0:9200/sample.test/_search

def delete_index(self):
"""
Deletes test index. Uses class attribute:
index: name of the index to be deleted
"""
url = "{0}{1}/".format(self.host, self.index)
requests.delete(url, proxies=self.proxies)
self.es.indices.delete(index=self.index, ignore=404)

def search(self, query=None):
"""
Run a search <query> (JSON) and returns the JSON response.
"""
url = "{0}{1}/_search".format(self.host, self.index)
query = {} if query is None else query
response = requests.post(
url,
data=json.dumps(query),
proxies=self.proxies)
return json.loads(response.text)
response = self.es.search(index=self.index, body=query)
return response

def tokenize(self, text, analyzer):
"""
Run <analyzer> on text and returns a dict containing the tokens.
"""
url = "{0}{1}/_analyze".format(self.host, self.index)
if analyzer != "default":
url += "?analyzer={0}".format(analyzer)
response = requests.post(
url,
data=json.dumps(text),
proxies=self.proxies)
return json.loads(response.text)
response = self.es.indices.analyze(index=self.index, body=text, analyzer=analyzer)
return response


class MultipleIndexesQueryTestCase(ElasticSearchQueryTestCase):
Expand Down Expand Up @@ -266,16 +243,13 @@ def create_index(self, index_name="", settings="", mappings=""):
(i) http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/
configuring-analyzers.html
"""
url = "{0}{1}/"
index = index_name or self.index
url = url.format(self.host, index)

data = {}
if self.mappings:
data["mappings"] = mappings or self.mappings
if self.settings:
data["settings"] = settings or self.settings
json_data = json.dumps(data)
response = requests.put(url, proxies=self.proxies, data=json_data)
self.es.indices.create(index=self.index, body=data)

def load_fixtures(self, index_name="", fixtures=""):
"""
Expand Down Expand Up @@ -308,49 +282,31 @@ def load_fixtures(self, index_name="", fixtures=""):
index = index_name or self.index
fixtures = fixtures or self.fixtures
for doc in fixtures:
doc_type = urllib.quote_plus(doc["type"])
doc_id = urllib.quote_plus(doc["id"])
doc_body = doc["body"]
url = "{0}{1}/{2}/{3}"
url = url.format(self.host, index, doc_type, doc_id)
response = requests.put(
url,
data=json.dumps(doc_body),
proxies=self.proxies)
if not response.status_code in [200, 201]:
raise ElasticSearchException(response.text)
time.sleep(self.timeout)
self.es.index(
index=index, doc_type=doc["type"],
id=doc["id"], body=doc["body"], refresh=True
)
# http://0.0.0.0:9200/sample.test/_search

def delete_index(self, index_name=""):
"""
Deletes test index. Uses class attribute:
index: name of the index to be deleted
"""
index = index_name or self.index
url = "{0}{1}/".format(self.host, self.index)
requests.delete(url, proxies=self.proxies)
self.es.indices.delete(index=self.index, ignore=404)

def search(self, query=None):
"""
Run a search <query> (JSON) and returns the JSON response.
"""
url = "{0}/_search".format(self.host)
query = {} if query is None else query
response = requests.post(
url,
data=json.dumps(query),
proxies=self.proxies)
return json.loads(response.text)
response = self.es.search(body=query)
return response

def search_in_index(self, index, query=None):
"""
Run a search <query> (JSON) and returns the JSON response.
"""
url = "{0}/{1}/_search".format(self.host, index)
query = {} if query is None else query
response = requests.post(
url,
data=json.dumps(query),
proxies=self.proxies)
return json.loads(response.text)
response = self.es.search(index=index, body=query)
return response
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
requests==2.0.0
elasticsearch==1.2.0
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
download_url = 'http://pypi.python.org/pypi/ESTester',
description=u"Utilities for testing ElasticSearch queries",
include_package_data=True,
install_requires=["requests>=2.0.0"],
install_requires=["elasticsearch>=1.2.0"],
license="GNU GPLv2",
long_description=README,
packages=find_packages(),
Expand Down
14 changes: 6 additions & 8 deletions tests/test_querytestcase.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import unittest
from mock import patch
from estester import ElasticSearchQueryTestCase, ExtendedTestCase,\
MultipleIndexesQueryTestCase

Expand Down Expand Up @@ -104,7 +103,7 @@ def test_search_all_indexes(self):
def test_search_one_index_that_has_item(self):
query = {
"query": {
"text": {
"match": {
"name": "Agnessa"
}
}
Expand All @@ -117,7 +116,7 @@ def test_search_one_index_that_has_item(self):
def test_search_one_index_that_doesnt_have_item(self):
query = {
"query": {
"text": {
"match": {
"name": "Agnessa"
}
}
Expand All @@ -144,7 +143,6 @@ class SimpleQueryTestCase(ElasticSearchQueryTestCase):

def test_search_by_nothing_returns_two_results(self):
response = self.search()
expected = {u"name": u"Nina Fox"}
self.assertEqual(response["hits"]["total"], 2)
self.assertEqual(response["hits"]["hits"][0]["_id"], u"1")
self.assertEqual(response["hits"]["hits"][1]["_id"], u"2")
Expand All @@ -159,13 +157,13 @@ def test_search_by_nina_returns_one_result(self):
def test_tokenize_with_default_analyzer(self):
response = self.tokenize("Nothing to declare", "default")
items_list = response["tokens"]
self.assertEqual(len(items_list), 2)
self.assertEqual(len(items_list), 3)
tokens = [item["token"] for item in items_list]
self.assertEqual(sorted(tokens), ["declare", "nothing"])
self.assertEqual(sorted(tokens), ["declare", "nothing", "to"])

def test_tokenize_with_default_analyzer(self):
def test_tokenize_with_whitespace_analyzer(self):
response = self.tokenize("Nothing to declare", "whitespace")
items_list = response["tokens"]
self.assertEqual(len(items_list), 3)
tokens = [item["token"] for item in items_list]
self.assertEqual(sorted(tokens), ['"Nothing', 'declare"', "to"])
self.assertEqual(sorted(tokens), ["Nothing", "declare", "to"])