So far so good, but when I run the sample search term 'policy', it gives me a
elasticsearch.NotFoundError: NotFoundError(404, 'index_not_found_exception', 'no such index [my_documents]', my_documents, index_or_alias)
Hi @Christian_Dahlqvist - Yes. And thanks for responding. I've simply followed the tutorial direction. I'm sure this is a rtfm moment for me.. Here's my search.py
import json
from pprint import pprint
import os
import time
from dotenv import load_dotenv
from elasticsearch import Elasticsearch
load_dotenv()
class Search:
def __init__(self):
self.es = Elasticsearch(cloud_id=os.environ['ELASTIC_CLOUD_ID'],
api_key=os.environ['ELASTIC_API_KEY'])
# ...
client_info = self.es.info()
print('***** eyeball *****')
pprint(client_info.body)
def create_index(self):
self.es.indices.delete(index='my_documents', ignore_unavailable=True)
self.es.indices.create(index='my_documents')
def insert_document(self, document):
return self.es.index(index='my_documents', body=document)
def insert_documents(self, documents):
operations = []
for document in documents:
operations.append({'index': {'_index': 'my_documents'}})
operations.append(document)
return self.es.bulk(operations=operations)
def search(self, **query_args):
return self.es.search(index='my_documents', **query_args)
I have not gone used this tutorial, but the docs I linked to contains an example of how to load data from the data.json file. This should apparently be part of the project you downloaded here. Seems like you have missed adding the data.
import json
from search import Search
es = Search()
with open('data.json', 'rt') as f:
documents = json.loads(f.read())
for document in documents:
es.insert_document(document)
The section in the tutorial directly before "Search basics" is called "Create an Index", and has URL
The code @Christian_Dahlqvist quoted is taken directly from that URL, and the specific code snippet is preceded by:
To import the entire contents of the data.json file, you could do something like this
That could leaves some wiggle room, I guess. Anyways, the tutorial runs through a few other things and ends with
Now when you want to generate a clean index, all you need to do is run flask reindex.
Again, giving benefit of the doubt, it doesn't say you must do a flask reindex at that point either.
I think the mindset is that at this point in the tutorial you will have understood that Elasticsearch is (at one level) based around searching index-es, and index-es contain documents.
There's a link at the bottom, click Report an issue, which you can use to send feedback about the tutorial.
As someone who has written many of these sort of tutorials/cookbooks/step-by-step guides, almost all feedback is appreciated as the authors tend to start from a different place compared to the readers, and know that they do, and can't always appreciate how their text will actually be understood/interpreted by others with different levels of knowledge/understanding.
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.