Skip to content

Commit f97ef9e

Browse files
committed
Add Geo Distance to near Search Results
Signed-off-by: Jonas Wagner <jwagner@knoppiks.de>
1 parent 964606e commit f97ef9e

21 files changed

Lines changed: 113 additions & 40 deletions

File tree

.github/scripts/search-location-near.sh

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,15 @@ END
2121
location "Leipzig" "51.3397" "12.3731" | create "$base/Location" >/dev/null
2222
location "Jakarta" "-6.2" "106.8167" | create "$base/Location" >/dev/null
2323

24-
search() {
25-
curl -s -H "Content-Type: application/fhir+json" "$base/Location?near=$1&summary=count" | jq -r .total
26-
}
24+
response="$(curl -s -H "Content-Type: application/fhir+json" "$base/Location?near=43.77925|11.24626|900|km")"
25+
26+
test "Location 900km from Florence finds Leipzig" "$(echo "$response" | jq -r .total)" "1"
2727

28-
test "Location 900km from Florence finds Leipzig" "$(search "43.77925|11.24626|900|km")" "1"
28+
match_extension="$(echo "$response" | jq '.entry[0] | .search.extension[0]')"
29+
if echo "$match_extension" | jq -er '.valueDistance.unit == "m"' >/dev/null \
30+
&& echo "$match_extension" | jq -er '.url == "http://hl7.org/fhir/StructureDefinition/location-distance"' >/dev/null; then
31+
echo "✅ the search result contains a location-distance extension"
32+
else
33+
echo "🆘 the search does not contain a location-distance extension"
34+
exit 1
35+
fi

modules/db-protocols/src/blaze/db/impl/protocols.clj

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,13 @@
139139
[search-param batch-db compartment tid compiled-value]
140140
[search-param batch-db compartment tid compiled-value start-id]
141141
"Returns a reducible collection.")
142-
(-matcher [_ batch-db modifier values])
142+
(-matcher [_ batch-db modifier compiled-values])
143143
(-single-version-id-matcher [_ batch-db tid modifier compiled-values])
144-
(-second-pass-filter [search-param batch-db values])
144+
(-postprocess-matches
145+
[search-param batch-db values compiled-values]
146+
"Returns a transducer that will be applied on every matching resource-handle
147+
at the end of the query execution. Can also be used to finally remove
148+
unwanted matches.")
145149
(-compartment-ids [_ resolver resource])
146150
(-index-values [_ resolver resource])
147151
(-index-value-compiler [_]))

modules/db/java/blaze/db/impl/index/ResourceHandle.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import static java.util.Objects.requireNonNull;
99

10-
public final class ResourceHandle implements ILookup, IKeywordLookup {
10+
public final class ResourceHandle implements ILookup, IKeywordLookup, IObj {
1111

1212
public static final Comparator<ResourceHandle> ID_CMP = Comparator.comparing(rh -> rh.id);
1313

@@ -85,14 +85,16 @@ public static Keyword op(long state) {
8585
private final long t;
8686
private final Hash hash;
8787
private final long state;
88+
private final IPersistentMap meta;
8889

89-
public ResourceHandle(Keyword fhirType, int tid, String id, long t, Hash hash, long state) {
90+
public ResourceHandle(Keyword fhirType, int tid, String id, long t, Hash hash, long state, IPersistentMap meta) {
9091
this.fhirType = requireNonNull(fhirType);
9192
this.tid = tid;
9293
this.id = requireNonNull(id);
9394
this.t = t;
9495
this.hash = requireNonNull(hash);
9596
this.state = state;
97+
this.meta = meta;
9698
}
9799

98100
@Override
@@ -124,6 +126,16 @@ public ILookupThunk getLookupThunk(Keyword key) {
124126
return null;
125127
}
126128

129+
@Override
130+
public IObj withMeta(IPersistentMap meta) {
131+
return new ResourceHandle(fhirType, tid, id, t, hash, state, meta);
132+
}
133+
134+
@Override
135+
public IPersistentMap meta() {
136+
return meta;
137+
}
138+
127139
@Override
128140
public boolean equals(Object o) {
129141
if (this == o) return true;

modules/db/src/blaze/db/impl/index.clj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,11 +125,11 @@
125125
(let [ordered-index-handles #(ordered-index-handles* batch-db tid % start-id)]
126126
(intersection-index-handles (map ordered-index-handles clauses))))))
127127

128-
(defn- second-pass-filter [batch-db clauses]
128+
(defn- postprocess-matches [batch-db clauses]
129129
(transduce
130130
(keep
131-
(fn [[search-param _ values]]
132-
(p/-second-pass-filter search-param batch-db values)))
131+
(fn [[search-param _ values compiled-values]]
132+
(p/-postprocess-matches search-param batch-db values compiled-values)))
133133
comp
134134
clauses))
135135

@@ -203,7 +203,7 @@
203203
(defn- resource-handle-mapper*
204204
([batch-db tid clauses]
205205
(comp (u/resource-handle-xf batch-db tid)
206-
(second-pass-filter batch-db clauses)))
206+
(postprocess-matches batch-db clauses)))
207207
([batch-db tid clauses other-clauses]
208208
(comp (other-clauses-index-handle-filter batch-db tid other-clauses)
209209
(resource-handle-mapper* batch-db tid clauses))))

modules/db/src/blaze/db/impl/index/resource_handle.clj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@
4747
id
4848
t
4949
hash
50-
state))))
50+
state
51+
nil))))
5152

5253
(defn resource-handle?
5354
"Returns `true` if `x` is a resource handle."

modules/db/src/blaze/db/impl/search_param/chained.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@
157157
nil
158158
(single-version-id-targets batch-db tid single-version-id ref-c-hash ref-tid)))))
159159

160-
(-second-pass-filter [_ _ _]))
160+
(-postprocess-matches [_ _ _ _]))
161161

162162
(defn chained-search-param
163163
"Creates a new chaining search param from the following arguments:

modules/db/src/blaze/db/impl/search_param/composite/token_quantity.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@
8282
(spq/single-version-id-matcher batch-db tid c-hash prefix-length
8383
compiled-values))
8484

85-
(-second-pass-filter [_ _ _])
85+
(-postprocess-matches [_ _ _ _])
8686

8787
(-index-values [_ resolver resource]
8888
(when-ok [values (fhir-path/eval resolver main-expression resource)]

modules/db/src/blaze/db/impl/search_param/composite/token_token.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
(-matcher [_ batch-db _ compiled-values]
6262
(r-sp-v/value-prefix-filter (:snapshot batch-db) c-hash compiled-values))
6363

64-
(-second-pass-filter [_ _ _])
64+
(-postprocess-matches [_ _ _ _])
6565

6666
(-index-values [_ resolver resource]
6767
(when-ok [values (fhir-path/eval resolver main-expression resource)]

modules/db/src/blaze/db/impl/search_param/date.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -445,7 +445,7 @@
445445
(-single-version-id-matcher [_ batch-db tid _ values]
446446
(single-version-id-matcher batch-db tid c-hash values))
447447

448-
(-second-pass-filter [_ _ _])
448+
(-postprocess-matches [_ _ _ _])
449449

450450
(-index-values [search-param resolver resource]
451451
(when-ok [values (fhir-path/eval resolver expression resource)]

modules/db/src/blaze/db/impl/search_param/has.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@
150150
(p/-matcher search-param batch-db modifier compiled-values)
151151
(map svi/from-resource-handle)))
152152

153-
(-second-pass-filter [_ _ _])
153+
(-postprocess-matches [_ _ _ _])
154154

155155
(-index-values [_ _ _]
156156
[]))

0 commit comments

Comments
 (0)