3030import math
3131from flask import jsonify , Response
3232
33- key_number_search_template_ = Template (
34- """
33+ key_number_search_template_ = Template ("""
3534{"size":0,"aggs":{"value_search":{"nested":{"path":"key_values"},
3635"aggs":{"value_filter":{"filter":{"terms":
3736{"key_values.name.keyword":["$key"]}},
3837"aggs":{"required_values":{"cardinality":
3938{"field":"key_values.value.keyvalue","precision_threshold":4000
40- }}}}}}}}"""
41- )
39+ }}}}}}}}""" )
4240
43- key_number_search_template = Template (
44- """
41+ key_number_search_template = Template ("""
4542{"size":0,"query":{ "bool": {"must": {"match":
4643{"data_source.keyvalue":"$data_source"}}}},
4744"aggs":{"value_search":{"nested":{"path":"key_values"},"aggs":{"value_filter":{"filter":{"terms":{
4845"key_values.name.keyword":["$key"]}},"aggs":{"required_values":{"cardinality":{
49- "field":"key_values.value.keyvalue","precision_threshold":4000}}}}}}}}"""
50- )
46+ "field":"key_values.value.keyvalue","precision_threshold":4000}}}}}}}}""" )
5147
52- search_by_value_only = Template (
53- """
48+ search_by_value_only = Template ("""
5449{"query":{"bool":{"must":[{"nested":
5550{"path":"key_values","query":{"bool":{"must":[{"wildcard":
56- {"key_values.value.keyvaluenormalize":"*eLa*"}}]}}}}]}}}"""
57- )
51+ {"key_values.value.keyvaluenormalize":"*eLa*"}}]}}}}]}}}""" )
5852
59- value_number_search_template = Template (
60- """
53+ value_number_search_template = Template ("""
6154{"size": 0,"aggs": {"name_search": {"nested":
6255{"path": "key_values"},"aggs": {"value_filter": {"filter": {
6356"terms": {"key_values.value.keyvaluenormalize":
6457["$value"]}},"aggs": {"required_name": {"cardinality": {
65- "field": "key_values.name.keyword","precision_threshold": 4000}}}}}}}}"""
66- )
58+ "field": "key_values.name.keyword","precision_threshold": 4000}}}}}}}}""" )
6759
68- value_search_template = Template (
69- """
60+ value_search_template = Template ("""
7061{"size": 0,"aggs": {"name_search":
7162{"nested": {"path": "key_values"},"aggs": {"value_filter": {
7263"filter": {"terms": {"key_values.value.keyvaluenormalize":
7364["$value"]}},"aggs": {"required_name": {
74- "terms": {"field": "key_values.name.keyword","size": 9999}}}}}}}}"""
75- )
65+ "terms": {"field": "key_values.name.keyword","size": 9999}}}}}}}}""" )
7666
77- value_search_contain_template = Template (
78- """
67+ value_search_contain_template = Template ("""
7968{"size": 0,"aggs": {"name_search": {"nested": {"path": "key_values"},
8069"aggs": {"value_filter": {"terms":
8170{"field":"key_values.value.keyvaluenormalize","include": ".*$value.*"},
8271"aggs": {"required_name": {"terms":
83- {"field": "key_values.name.keyword","size": 9999}}}}}}}}"""
84- )
72+ {"field": "key_values.name.keyword","size": 9999}}}}}}}}""" )
8573
86- key_search_template = Template (
87- """
74+ key_search_template = Template ("""
8875{"size": 0,"aggs": {"name_search": {"nested": {"path": "key_values"},
8976"aggs": {"value_filter": {
9077"filter": {"terms": {"key_values.name.keyword": ["$key"]}},
9178"aggs": {"required_values": {
9279"terms": {"field": "key_values.value.keyvaluenormalize",
93- "size": 9999}}}}}}}}"""
94- )
80+ "size": 9999}}}}}}}}""" )
9581
96- values_for_key_template = Template (
97- """
82+ values_for_key_template = Template ("""
9883{"size":0, "query":{ "bool" : {"must": {"match":{
9984"data_source.keyvalue":"$data_source"}}}},
10085"aggs":{"name_search":{"nested":{ "path":"key_values"},
10186"aggs":{"value_filter":{"filter":{
10287"terms":{"key_values.name.keyword":["$key"]}},"aggs":{"required_values":{
10388"terms":{"field":"key_values.value.keyvaluenormalize",
10489"include": {"partition": "$cur","num_partitions": "$total"},
105- "size":10000 }}}}}}}}"""
106- )
90+ "size":10000 }}}}}}}}""" )
10791
10892
10993def search_index_for_value (e_index , query , get_size = False ):
@@ -160,11 +144,8 @@ def search_index_for_values_get_all_buckets(e_index, query):
160144 returened_results .append (res )
161145 co += page_size
162146 if len (res ["hits" ]["hits" ]) == 0 :
163- search_omero_app .logger .info (
164- "No result is found in the final\
165- loop: %s for size %s"
166- % (co , size )
167- )
147+ search_omero_app .logger .info ("No result is found in the final\
148+ loop: %s for size %s" % (co , size ))
168149 return returened_results
169150 bookmark = [
170151 int (res ["hits" ]["hits" ][- 1 ]["sort" ][0 ]),
@@ -580,16 +561,14 @@ def search_value_for_resource(
580561"""
581562Search using key and resource
582563"""
583- key_values_buckets_template = Template (
584- """
564+ key_values_buckets_template = Template ("""
585565{
586566"query":{"bool":{"must":[{"bool":{
587567"must":{"match":{"Attribute.keynamenormalize":"$name"}}}},{"bool":{"must":{
588568"match":{"resource.keyresource":"$resource"}}}
589569},{"bool":{"must":{"terms":{"data_source.keyvalue":$data_source}
590570}}}]}}}
591- """
592- )
571+ """ )
593572
594573"""
595574Search using key, part of the value and resource
@@ -607,42 +586,34 @@ def search_value_for_resource(
607586# "fields": ["Attribute","Value","items_in_the_bucket",
608587# "total_items_in_saved_buckets","total_buckets","total_items"],
609588# "_source": false,
610- ss = Template (
611- """
589+ ss = Template ("""
612590{"query":{"bool":{"must":[{"bool":{
613591"must":{"match":{"Attribute.keyname":"$name"}}}},{"bool": {
614592"must": {"match": {"resource.keyresource": "$resource"}}}}]}}
615- "size": 9999}"""
616- )
593+ "size": 9999}""" )
617594
618595"""
619596Search using value and resource
620597"""
621- key_values_search_buckets_template = Template (
622- """
598+ key_values_search_buckets_template = Template ("""
623599{"query":{"bool":{"must":[{"bool":{
624600"must":{"match":{"Value.keyvalue":"$value"}}}},{
625- "bool": {"must": {"match":{"resource.keyresource": "$resource"}}}}]}},"size": 9999}"""
626- )
601+ "bool": {"must": {"match":{"resource.keyresource": "$resource"}}}}]}},"size": 9999}""" )
627602
628603"""
629604Search using value or part of value and return all the posible mathes
630605"""
631606
632- value_all_buckets_template = Template (
633- """
607+ value_all_buckets_template = Template ("""
634608{"query":{"bool":{"must":[{"bool":{
635609"must":{"wildcard":
636- {"Value.keyvaluenormalize":"*$value*"}}}}]}},"size": 9999}"""
637- )
610+ {"Value.keyvaluenormalize":"*$value*"}}}}]}},"size": 9999}""" )
638611
639- resource_key_values_buckets_size_template = Template (
640- """
612+ resource_key_values_buckets_size_template = Template ("""
641613{"query":{"bool":{"must":[{"bool":{
642614"must":{"wildcard":{"Value.keyvaluenormalize":"*$value*"}}}},{"bool":{
643615"must":{"terms":{"data_source.keyvalue":$data_source}}}},{
644- "bool": {"must": {"match":{"resource.keyresource": "$resource"}}}}]}}}"""
645- )
616+ "bool": {"must": {"match":{"resource.keyresource": "$resource"}}}}]}}}""" )
646617
647618resource_key_values_buckets_template = Template ( # noqa
648619 """
@@ -697,11 +668,9 @@ def get_resource_attributes(
697668 return the available attributes for one or all resources
698669 """
699670 if mode and mode != "searchterms" :
700- return build_error_message (
701- "The mode parameter supports only 'searchterms'\
671+ return build_error_message ("The mode parameter supports only 'searchterms'\
702672 to return the common search terms,\
703- you may remove it to return all the keys."
704- )
673+ you may remove it to return all the keys." )
705674 returned_results = []
706675 if data_source and data_source .lower () != "all" :
707676 data_source = [itm .strip ().lower () for itm in data_source .split ("," )]
@@ -757,13 +726,11 @@ def get_resource_attributes(
757726 return returned_results
758727
759728
760- attribute_search_values_template = Template (
761- """
729+ attribute_search_values_template = Template ("""
762730{"query":{"bool":{"must":[
763731{"bool":{"must":{"match":{"resource.keyresource":"$resource"}}}},
764732{"bool": {"must":
765- {"match": {"Attribute.keyname":"$name"}}}}]}},"size":9999}"""
766- )
733+ {"match": {"Attribute.keyname":"$name"}}}}]}},"size":9999}""" )
767734
768735
769736def get_resource_attribute_search_values (
@@ -1087,17 +1054,15 @@ def process_container_query(
10871054}},"uniquesTerms": {"terms": {"field":
10881055"key_values.name.keynamenormalize", "size": 10000}}}}}"""
10891056
1090- resource_keys_template = Template (
1091- """
1057+ resource_keys_template = Template ("""
10921058{"size":0,"query":{ "bool" : {"must": {"match":{
10931059"data_source.keyvalue":"$data_source"}}}},
10941060"aggs":{"value_search":{"nested":{"path":"key_values"},
10951061"aggs":{"required_values":{"cardinality":{
10961062"field":"key_values.name.keyword","precision_threshold":4000}}},
10971063"aggs": {"required_name": {
10981064"terms": {"field": "key_values.name.keyword","size": 9999}}}}}}
1099- """
1100- )
1065+ """ )
11011066
11021067
11031068def get_resource_keys (resource , data_source ):
@@ -1112,8 +1077,7 @@ def get_resource_keys(resource, data_source):
11121077# Return sub container using a container attribute
11131078# for example get the number of sub-containers e.g. datasets names,
11141079# inside a container, e.g. project using name.
1115- container_returned_sub_container_template = Template (
1116- """
1080+ container_returned_sub_container_template = Template ("""
11171081 {
11181082 "values":{
11191083 "filter":{
@@ -1139,8 +1103,7 @@ def get_resource_keys(resource, data_source):
11391103 }
11401104 }
11411105}
1142- """
1143- )
1106+ """ )
11441107
11451108
11461109def get_containers_no_images (
0 commit comments