def load(cls, serialized_index): """Load a serialized index""" from lunr import __TARGET_JS_VERSION__ if isinstance(serialized_index, str): serialized_index = json.loads(serialized_index) if serialized_index["version"] != __TARGET_JS_VERSION__: logger.warning( "Version mismatch when loading serialized index. " "Current version of lunr {} does not match that of serialized " "index {}".format(__TARGET_JS_VERSION__, serialized_index["version"])) field_vectors = { ref: Vector(elements) for ref, elements in serialized_index["fieldVectors"] } tokenset_builder = TokenSetBuilder() inverted_index = {} for term, posting in serialized_index["invertedIndex"]: tokenset_builder.insert(term) inverted_index[term] = posting tokenset_builder.finish() return Index( fields=serialized_index["fields"], field_vectors=field_vectors, inverted_index=inverted_index, token_set=tokenset_builder.root, pipeline=Pipeline.load(serialized_index["pipeline"]), )
def test_load_with_registered_functions(self): serialized_pipeline = ["fn"] Pipeline.register_function(fn, "fn") pipeline = Pipeline.load(serialized_pipeline) assert len(pipeline) == 1 assert pipeline._stack[0] == fn
def test_load_with_unregistered_functions(self): serialized_pipeline = ["fn"] with pytest.raises(BaseLunrException): Pipeline.load(serialized_pipeline)