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

Add support to create index with mapping definition #89

Closed

Conversation

andersoncarubelli
Copy link

Hi!

I'm facing a problem with my current project where I need to create a rotating index and for this, I need to obey some mapping definitions, that's the reason I crating this PR collaborating with this project and also trying to help features requests here and here.

I've decided to use property "ELASTICSEARCH_INDEX_MAPPING" to set file location of mapping definition instead of writing the definition itself inside the "settings.py" file, that's because these definitions tend to grow a lot and can easily pollute the code, eg: { "settings" : { "index" : { "number_of_shards" : 1, "number_of_replicas" : 1 }, "analysis": { "analyzer": { "my_analyzer": { "type": "snowball" } } } }, "mappings": { "dynamic": "strict", "properties": { "source": { "type": "text" }, "url": { "type": "text" }, "name": { "type": "text", "analyzer": "my_analyzer", "search_analyzer": "my_analyzer" }, "phone": { "type": "text" }, "photo": { "type": "text" }, "details": { "type": "text", "analyzer": "my_analyzer", "search_analyzer": "my_analyzer" }, "created_at": { "type": "date" } } } }

I decided to make it simple and clean just setting the file path of the index definition.

DISCLAIMER: I'm not sure if is the right decision to call the function "create_index_with_mapping" inside the function called "from_crawler". I would love to know your option about that.

Thanks for your project it's helping me a lot and I hope I can help a little bit with it too.

Cheers
Anderson Carubelli

Signed-off-by: Anderson Carubelli <acarubelli@gmail.com>
Signed-off-by: Anderson Carubelli <acarubelli@gmail.com>
Signed-off-by: Anderson Carubelli <acarubelli@gmail.com>
Signed-off-by: Anderson Carubelli <acarubelli@gmail.com>
@@ -88,6 +89,10 @@ def from_crawler(cls, crawler):

cls.validate_settings(ext.settings)
ext.es = cls.init_es_client(crawler.settings)

if 'ELASTICSEARCH_INDEX_MAPPING' in ext.settings:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not an expert on this tool yet but seems a good place to do it (right after ES init)

logging.debug("index already exists, mapping will not be updated")
else:
logging.debug("will create index with mapping definition")
file = open(mapping_file, "r")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use a ctx manager to close the file right after 😉

ext.es.indices.create(
index=index_name, ignore=400, body=mapping_data)

def create_index_name(ext):
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not mandatory but I would extrapolate from index_item index name computation and rename this method like _get_index_name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants