def test_chaining(): # Internal q = SearchHelper(INDEX, search_client=SEARCH_CLIENT) q._field("source_name", "cip") q._and_join() q._field("elements", "Al") res1 = q._ex_search(limit=10000) res2 = (SearchHelper(INDEX, search_client=SEARCH_CLIENT)._field( "source_name", "cip")._and_join()._field("elements", "Al")._ex_search(limit=10000)) assert all([r in res2 for r in res1]) and all([r in res1 for r in res2]) # External f = SearchHelper(INDEX, search_client=SEARCH_CLIENT) f.match_field("source_name", "cip") f.match_field("material.elements", "Al") res1 = f.search() res2 = f.match_field("source_name", "cip").match_field("material.elements", "Al").search() assert all([r in res2 for r in res1]) and all([r in res1 for r in res2])
def test_field(): q1 = SearchHelper(INDEX, search_client=SEARCH_CLIENT) # Single field and return value test assert isinstance(q1._field("mdf.source_name", "oqmd"), SearchHelper) assert q1._SearchHelper__query["q"] == "(mdf.source_name:oqmd" # Multi-field and grouping test q1._and_join(close_group=True)._field("dc.title", "sample") assert q1._SearchHelper__query[ "q"] == "(mdf.source_name:oqmd) AND (dc.title:sample" # Negation test q1._negate() assert q1._SearchHelper__query[ "q"] == "(mdf.source_name:oqmd) AND (dc.title:sample NOT " # Explicit operator test # Makes invalid query for this case q1._operator("NOT") assert q1._SearchHelper__query[ "q"] == "(mdf.source_name:oqmd) AND (dc.title:sample NOT NOT " # Ensure advanced is set assert q1._SearchHelper__query["advanced"] is True # Test noop on blanks q2 = SearchHelper(INDEX, search_client=SEARCH_CLIENT) assert q2._SearchHelper__query["q"] == "(" q2._field(field="", value="value") assert q2._SearchHelper__query["q"] == "(" q2._field(field="field", value="") assert q2._SearchHelper__query["q"] == "(" q2._field(field="", value="") assert q2._SearchHelper__query["q"] == "(" q2._field(field="field", value="value") assert q2._SearchHelper__query["q"] == "(field:value" # Test auto-quote q3 = SearchHelper(INDEX, search_client=SEARCH_CLIENT) q3._field("dc.descriptions.description", "With Spaces") assert q3._SearchHelper__query[ "q"] == '(dc.descriptions.description:"With Spaces"' q3._and_join(close_group=True)._field("dc.title", "Mark's") assert q3._SearchHelper__query["q"] == ( '(dc.descriptions.description:"With Spaces") AND (' 'dc.title:"Mark\'s"') q3._or_join(close_group=False)._field("dc.title", "The\nLarch") assert q3._SearchHelper__query["q"] == ( '(dc.descriptions.description:"With Spaces") AND (' 'dc.title:"Mark\'s" OR dc.title:"The\nLarch"') # No auto-quote on ranges q3._and_join(close_group=True)._field("block.range", "[5 TO 6]") assert q3._SearchHelper__query["q"] == ( '(dc.descriptions.description:"With Spaces") AND (' 'dc.title:"Mark\'s" OR dc.title:"The\nLarch") AND (' 'block.range:[5 TO 6]')