def test_pipeline_processing_enabled(self, index): query = Query(index.fields) query.clause(term="study", use_pipeline=True) # stemmed to studi results = index.query(query) assert len(results) == 2 assert results[0]["ref"] == "b" assert results[1]["ref"] == "a"
def test_negated_query_no_match(self, index, query_or_search): if query_or_search == "query": query = Query(index.fields) query.term("qwertyuiop", presence=QueryPresence.PROHIBITED) results = index.query(query) else: results = index.search("-qwertyuiop") assert len(results) == 3
def test_only_prohibited_match_yields_no_results(self, index, query_or_search): if query_or_search == "query": query = Query(index.fields) query.term("green", presence=QueryPresence.PROHIBITED) results = index.query(query) else: results = index.search("-green") assert len(results) == 0
def test_negated_query_some_match(self, index, query_or_search): if query_or_search == "query": query = Query(index.fields) query.term("plant", presence=QueryPresence.PROHIBITED) results = index.query(query) else: results = index.search("-plant") assert len(results) == 1 assert results[0]["score"] == 0
def test_no_query_boosts_document_boost_ranks_higher( self, documents, query_or_search): documents_with_boost = [(document, { "boost": 10 if document["id"] == "c" else 1 }) for document in documents] idx = lunr(ref="id", fields=("title", "body"), documents=documents_with_boost) if query_or_search == "search": results = idx.search("plumb") else: query = Query(idx.fields) query.term("plumb") results = idx.query(query) assert results[0]["ref"] == "c"
def test_typeahead_no_results(self, index): query = Query(index.fields) query.clause("xyz", boost=100, use_pipeline=True) query.clause("xyz", boost=10, use_pipeline=False, wildcard=Query.WILDCARD_TRAILING) query.clause("xyz", boost=1, edit_distance=1) results = index.query(query) assert len(results) == 0
def create_query(self, fields=None): """Convenience method to create a Query with the Index's fields. Args: fields (iterable, optional): The fields to include in the Query, defaults to the Index's `all_fields`. Returns: Query: With the specified fields or all the fields in the Index. """ if fields is None: return Query(self.fields) non_contained_fields = set(fields) - set(self.fields) if non_contained_fields: raise BaseLunrException("Fields {} are not part of the index", non_contained_fields) return Query(fields)
def test_no_query_boosts_build_boost_ranks_higher(self, documents, query_or_search): idx = lunr( ref="id", fields=["title", { "field_name": "body", "boost": 10 }], documents=documents, ) if query_or_search == "search": results = idx.search("professor") else: query = Query(idx.fields) query.term("professor") results = idx.query(query) assert results[0]["ref"] == "b"
def test_typeahead_results(self, index): query = Query(index.fields) query.clause("pl", boost=100, use_pipeline=True) query.clause("pl", boost=10, use_pipeline=False, wildcard=Query.WILDCARD_TRAILING) query.clause("pl", boost=1, edit_distance=1) results = index.query(query) assert len(results) == 2 assert {r["ref"] for r in results} == {"b", "c"} assert set( results[0]["match_data"].metadata.keys()) == {"plumb", "plant"} assert set( results[1]["match_data"].metadata.keys()) == {"plumb", "plant"}
def test_required_term_not_matching_yields_no_results( self, index, query_or_search): if query_or_search == "query": query = Query(index.fields) query.term("qwertyuiop", presence=QueryPresence.REQUIRED) query.term("green", presence=QueryPresence.OPTIONAL) results = index.query(query) else: results = index.search("+qwertyuiop green") assert len(results) == 0
def test_two_required_matches_yields_no_results(self, index, query_or_search): if query_or_search == "query": query = Query(index.fields) query.term("mustard", presence=QueryPresence.REQUIRED) query.term("plant", presence=QueryPresence.REQUIRED) results = index.query(query) else: results = index.search("+mustard +plant") assert len(results) == 0
def test_required_match(self, index, query_or_search): if query_or_search == "query": query = Query(index.fields) query.term("candlestick", presence=QueryPresence.REQUIRED) query.term("green", presence=QueryPresence.OPTIONAL) results = index.query(query) else: results = index.search("+candlestick green") assert len(results) == 1 assert set(results[0]["match_data"].metadata.keys()) == { "candlestick", "green" }
def test_field_match(self, index, query_or_search): if query_or_search == "query": query = Query(index.fields) query.term("plant", presence=QueryPresence.PROHIBITED, fields=["title"]) query.term("plumb", presence=QueryPresence.OPTIONAL) results = index.query(query) else: results = index.search("-title:plant plumb") assert len(results) == 1 assert set(results[0]["match_data"].metadata.keys()) == {"plumb"}
def test_prohibited_match_excludes_prohibited_result( self, index, query_or_search): if query_or_search == "query": query = Query(index.fields) query.term("candlestick", presence=QueryPresence.PROHIBITED) query.term("green", presence=QueryPresence.OPTIONAL) results = index.query(query) else: results = index.search("-candlestick green") assert len(results) == 2 assert {r["ref"] for r in results} == {"b", "c"} assert set(results[0]["match_data"].metadata.keys()) == {"green"} assert set(results[1]["match_data"].metadata.keys()) == {"green"}
def test_combined_required_optional_and_prohibited_match( self, index, query_or_search): if query_or_search == "query": query = Query(index.fields) query.term("plant", presence=QueryPresence.REQUIRED) query.term("green", presence=QueryPresence.OPTIONAL) query.term("office", presence=QueryPresence.PROHIBITED) results = index.query(query) else: results = index.search("+plant green -office") assert len(results) == 1 assert set( results[0]["match_data"].metadata.keys()) == {"green", "plant"} assert results[0]["ref"] == "b"
def test_required_term_on_field_matches(self, index, query_or_search): if query_or_search == "query": query = Query(index.fields) query.term("plant", presence=QueryPresence.REQUIRED, fields=["title"]) query.term("green", presence=QueryPresence.OPTIONAL) results = index.query(query) else: results = index.search("+title:plant green") assert len(results) == 1 assert set( results[0]["match_data"].metadata.keys()) == {"plant", "green"} assert results[0]["ref"] == "b"
def test_required_terms_on_different_fields_match(self, index, query_or_search): if query_or_search == "query": query = Query(index.fields) query.term("plant", presence=QueryPresence.REQUIRED, fields=["title"]) query.term("study", presence=QueryPresence.REQUIRED, fields=["body"]) results = index.query(query) else: results = index.search("+title:plant +body:study") assert len(results) == 1 assert set( results[0]["match_data"].metadata.keys()) == {"studi", "plant"} assert results[0]["ref"] == "b"
def test_pipeline_processing_disabled(self, index): query = Query(index.fields) query.clause(term="study", use_pipeline=False) # stemmed to studi results = index.query(query) assert len(results) == 0