-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Elasticsearch Document Store (#13)
- Loading branch information
Showing
15 changed files
with
164 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
from elasticsearch import Elasticsearch | ||
from elasticsearch_dsl import Search, Document as ESDoc, Text, connections | ||
from haystack.database.base import BaseDocumentStore | ||
|
||
|
||
class Document(ESDoc): | ||
name = Text() | ||
text = Text() | ||
tags = Text() | ||
|
||
class Index: | ||
name = "document" | ||
|
||
|
||
class ElasticsearchDocumentStore(BaseDocumentStore): | ||
def __init__(self, host="localhost", username="", password="", index="document"): | ||
self.client = Elasticsearch(hosts=[{"host": host}], http_auth=(username, password)) | ||
self.connections = connections.create_connection(hosts=[{"host": host}], http_auth=(username, password)) | ||
Document.init() # create mapping if not exists. | ||
self.index = index | ||
|
||
def get_document_by_id(self, id): | ||
query = {"filter": {"term": {"_id": id}}} | ||
result = self.client.search(index=self.index, body=query)["hits"]["hits"] | ||
if result: | ||
document = {"id": result["_id"], "name": result["name"], "text": result["text"]} | ||
else: | ||
document = None | ||
return document | ||
|
||
def get_document_ids_by_tags(self, tags): | ||
query = { | ||
"query": { | ||
"bool": { | ||
"should": [ | ||
{ | ||
"terms": { | ||
"tags": tags | ||
} | ||
} | ||
] | ||
} | ||
} | ||
} | ||
result = self.client.search(index=self.index, body=query)["hits"]["hits"] | ||
documents = [] | ||
for hit in result: | ||
documents.append({"id": hit["_id"], "name": hit["name"], "text": hit["text"]}) | ||
return documents | ||
|
||
def write_documents(self, documents): | ||
for doc in documents: | ||
d = Document( | ||
name=doc["name"], | ||
text=doc["text"], | ||
document_id=doc.get("document_id", None), | ||
tags=doc.get("tags", None), | ||
) | ||
d.save() | ||
|
||
def get_document_count(self): | ||
s = Search(using=self.client, index=self.index) | ||
return s.count() | ||
|
||
def get_all_documents(self): | ||
search = Search(using=self.client, index=self.index) | ||
documents = [] | ||
for hit in search: | ||
documents.append( | ||
{ | ||
"id": hit.meta["id"], | ||
"name": hit["name"], | ||
"text": hit["text"], | ||
} | ||
) | ||
return documents | ||
|
||
def query(self, query, top_k=10): | ||
search = Search(using=self.client, index=self.index).query("match", text=query)[:top_k].execute() | ||
paragraphs = [] | ||
meta_data = [] | ||
for hit in search: | ||
paragraphs.append(hit["text"]) | ||
meta_data.append({"paragraph_id": hit.meta["id"], "document_id": hit["document_id"]}) | ||
return paragraphs, meta_data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
from abc import ABC, abstractmethod | ||
|
||
|
||
class BaseRetriever(ABC): | ||
@abstractmethod | ||
def retrieve(self, query, candidate_doc_ids=None, top_k=1): | ||
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
from haystack.retriever.base import BaseRetriever | ||
|
||
|
||
class ElasticsearchRetriever(BaseRetriever): | ||
def __init__(self, document_store): | ||
self.document_store = document_store | ||
|
||
def retrieve(self, query, candidate_doc_ids=None, top_k=10): | ||
return self.document_store.query(query, top_k) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters