@@ -137,8 +137,53 @@ def process_qedge(self, qedge: QEdgeDict) -> list[ESFilterClause]:
137137 if (values := qedge .get (qfield ))
138138 ]
139139
140- def generate_query_for_merged_edges (
141- self , in_node : QNodeDict , edge : QEdgeDict , out_node : QNodeDict
140+ def generate_attribute_constraints (
141+ self ,
142+ in_node : QNodeDict ,
143+ edge : QEdgeDict ,
144+ out_node : QNodeDict ,
145+ query_kwargs : ESBooleanQuery ,
146+ ) -> ESBooleanQuery :
147+ """Generate attribute constraints based on QNode/QEdge payload."""
148+ constraint_origins : list [AttributeOrigin ] = ["edge" , "subject" , "object" ]
149+
150+ all_must : list [AttributeFilterQuery ] = []
151+ all_must_not : list [AttributeFilterQuery ] = []
152+
153+ for origin in constraint_origins :
154+ entity = (
155+ edge
156+ if origin == "edge"
157+ else in_node
158+ if origin == "subject"
159+ else out_node
160+ )
161+
162+ if origin == "edge" :
163+ constraints = entity .get ("attribute_constraints" , None )
164+ else :
165+ constraints = entity .get ("constraints" , None )
166+
167+ if constraints :
168+ must , must_not = process_attribute_constraints (constraints , origin )
169+ if must :
170+ all_must .extend (must )
171+ if must_not :
172+ all_must_not .extend (must_not )
173+
174+ if all_must :
175+ query_kwargs ["must" ] = all_must
176+ if all_must_not :
177+ query_kwargs ["must_not" ] = all_must_not
178+
179+ return query_kwargs
180+
181+ def generate_queries (
182+ self ,
183+ in_node : QNodeDict ,
184+ edge : QEdgeDict ,
185+ out_node : QNodeDict ,
186+ gen_attribute_constraints : bool = False , # disable attribute constraints for now
142187 ) -> ESPayload :
143188 """Generate query based on merged edges schema on Elasticsearch.
144189
@@ -185,37 +230,12 @@ def generate_query_for_merged_edges(
185230 query_kwargs ["filter" ].append (qualifier_terms )
186231
187232 # generate constraint terms for edges and associated nodes
188- constraint_origins : list [AttributeOrigin ] = ["edge" , "subject" , "object" ]
189-
190- all_must : list [AttributeFilterQuery ] = []
191- all_must_not : list [AttributeFilterQuery ] = []
192-
193- for origin in constraint_origins :
194- entity = (
195- edge
196- if origin == "edge"
197- else in_node
198- if origin == "subject"
199- else out_node
233+ # currently, this is DISABLED by default to favor post-processing
234+ if gen_attribute_constraints :
235+ query_kwargs = self .generate_attribute_constraints (
236+ in_node , edge , out_node , query_kwargs
200237 )
201238
202- if origin == "edge" :
203- constraints = entity .get ("attribute_constraints" , None )
204- else :
205- constraints = entity .get ("constraints" , None )
206-
207- if constraints :
208- must , must_not = process_attribute_constraints (constraints , origin )
209- if must :
210- all_must .extend (must )
211- if must_not :
212- all_must_not .extend (must_not )
213-
214- if all_must :
215- query_kwargs ["must" ] = all_must
216- if all_must_not :
217- query_kwargs ["must_not" ] = all_must_not
218-
219239 return ESPayload (query = ESQueryContext (bool = ESBooleanQuery (** query_kwargs )))
220240
221241 @override
@@ -226,7 +246,7 @@ def convert_triple(self, qgraph: QueryGraphDict) -> ESPayload:
226246 raise ValueError ("Query graph must contain exactly one edge." )
227247 in_node = qgraph ["nodes" ][edge ["subject" ]]
228248 out_node = qgraph ["nodes" ][edge ["object" ]]
229- return self .generate_query_for_merged_edges (in_node , edge , out_node )
249+ return self .generate_queries (in_node , edge , out_node )
230250
231251 @override
232252 def convert_batch_triple (self , qgraphs : list [QueryGraphDict ]) -> list [ESPayload ]:
0 commit comments