def add_to_index(index, model): es = get_elastic_client() if not es: return payload = {} for field in model.__searchable__: payload[field] = getattr(model, field) es.index(index=index, id=model.id, body=payload)
def query_index(index, query): es = get_elastic_client() if not es: return [] search = es.search( index=index, body={ "query": { "multi_match": { "query": query, "fields": ["*"], "analyzer": "rebuilt_standard"}}}, ) return search["hits"]["hits"]
def query_index(index, query): es = get_elastic_client() if not es: return [] search = es.search( index=index, body={ "query": { "multi_match": { "query": query, "fields": ["*"], "analyzer": "rebuilt_standard" } }, "highlight": { "fields": { "content": { "pre_tags": "<span style='background-color: #f6efa6'>", "post_tags": "</span>", "boundary_max_scan": 200, "fragment_size": 0 } } } }) hits = [] for hit in search["hits"]["hits"]: formatted_hit = { "id": hit["_id"], "title": hit["_source"]["title"], "highlight": [] } if "highlight" in hit: # FIXME: find a way to make this less hacky and # yet still conserve logical separations # hack to make pandoc faster by converting highlights in one go # join highlights into string with symbolic separator SEPARATOR = "SEPARATOR.m.m.m.m.m.m.m.m.m.SEPARATOR" concatenated_highlight = SEPARATOR.join( [highlight for highlight in hit["highlight"]["content"]]) # re split highlights formatted_hit["highlight"] = convert_text( concatenated_highlight, "html", format="md").split(SEPARATOR) hits.append(formatted_hit) return hits
app.register_blueprint(api_bp, url_prefix='/api') # check if pandoc is installed, otherwise install try: pypandoc.get_pandoc_version() except OSError: app.logger.info("Installing pandoc") pypandoc.download_pandoc() # create dir that will hold data if it doesn't already exist DIRNAME = app.config["APP_PATH"] + "/data/" Path(DIRNAME).mkdir(parents=True, exist_ok=True) if app.config["ELASTICSEARCH_ENABLED"]: with app.app_context(): es = extensions.get_elastic_client() try: es.indices.create( index=app.config["INDEX_NAME"], body=app.config["ELASTIC_CONF"]) except elasticsearch.ElasticsearchException: app.logger.info("Elasticsearch index already created") # login routes / setup login_manager = LoginManager() login_manager.login_view = "login" login_manager.init_app(app) @login_manager.user_loader def load_user(user_id):
def remove_from_index(index, dataobj_id): es = get_elastic_client() if not es: return es.delete(index=index, id=dataobj_id)