Skip to content

Commit

Permalink
Allow setting allow_partial_search_results in ES queries (#1359)
Browse files Browse the repository at this point in the history
<!-- Specify linked issues and REMOVE THE UNUSED LINES -->

> **Epic** #
> Close advthreat/iroh#3057
> Related #

- Expose a new configuration property at the ES Store level `allow_partial_search_results`
- This boolean is transmitted in the ES query params if specified

This ES option is documented here: https://www.elastic.co/guide/en/elasticsearch/reference/7.17/search-search.html

<!-- UNCOMMENT THIS SECTION IF NEEDED
<a name="iroh-services-clients">[§](#iroh-services-clients)</a> IROH Services Clients
=====================================================================================

Put all informations that need to be communicated to IROH Services Clients.
Typically IROH-UI, ATS Integration, Orbital, etc...
 -->

<a name="qa">[§](#qa)</a> QA
============================

<!--
Describe the steps to test your PR.

1.
2.
3.

Or if no QA is needed keep it as is.
 -->
No QA is needed.

<!-- UNCOMMENT THIS SECTION IF NEEDED
<a name="ops">[§](#ops)</a> Ops
===============================

  Specify Ops related issues and documentation
- Config change needed: threatgrid/tenzin#
- Migration needed: threatgrid/tenzin#
- How to enable/disable that feature: (ex remove service from `bootstrap.cfg`, add scope to org)
-->

<!-- UNCOMMENT THIS SECTION IF NEEDED
<a name="documentation">[§](#documentation)</a> Documentation
=============================================================

  Public Facing documentation section;
- Public documentation updated needed: threatgrid/iroh-ui#
  See internal [doc file](./services/iroh-auth/doc/public-doc.org)
 -->

<a name="release-notes">[§](#release-notes)</a> Release Notes
=============================================================

<!-- REMOVE UNUSED LINES -->

```
public: allow setting the `allow_partial_search_results` as an ES Store option
```

<a name="squashed-commits">[§](#squashed-commits)</a> Squashed Commits
======================================================================
  • Loading branch information
gbuisson authored Apr 4, 2023
1 parent 7abea1b commit c386246
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
1 change: 1 addition & 0 deletions resources/ctia-default.properties
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ ctia.store.es.default.shards=5
ctia.store.es.default.refresh=false
ctia.store.es.default.rollover.max_docs=10000000
ctia.store.es.default.aliased=true
ctia.store.es.default.allow_partial_search_results=false
ctia.store.es.default.version=5
ctia.store.es.default.update-mappings=false
ctia.store.es.default.update-settings=false
Expand Down
1 change: 1 addition & 0 deletions src/ctia/properties.clj
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
(str prefix store ".default_operator") (s/enum "OR" "AND")
(str prefix store ".timeout") s/Num
(str prefix store ".version") s/Num
(str prefix store ".allow_partial_search_results") s/Bool
(str prefix store ".update-mappings") s/Bool
(str prefix store ".update-settings") s/Bool
(str prefix store ".refresh-mappings") s/Bool
Expand Down
18 changes: 13 additions & 5 deletions src/ctia/stores/es/crud.clj
Original file line number Diff line number Diff line change
Expand Up @@ -453,13 +453,21 @@ It returns the documents with full hits meta data including the real index in wh
:props s/Any
(s/optional-key :sort-extension-definitions) SortExtensionDefinitions})

(defn es-seven-configured?
"given ES store properties, check it is configured to use ES7"
[{:keys [version]}]
(<= 7 version))

(s/defn make-query-params :- {s/Keyword s/Any}
[{:keys [params props sort-extension-definitions]} :- MakeQueryParamsArgs]
(cond-> (-> params
(rename-sort-fields sort-extension-definitions)
(with-default-sort-field props)
make-es-read-params)
(<= 7 (:version props)) (assoc :track_total_hits true)))
(let [pres-k :allow_partial_search_results]
(cond-> (-> params
(rename-sort-fields sort-extension-definitions)
(with-default-sort-field props)
make-es-read-params)
(and (es-seven-configured? props) (contains? props pres-k))
(assoc pres-k (pres-k props))
(es-seven-configured? props) (assoc :track_total_hits true))))

(defn handle-find
"Generate an ES find/list handler using some mapping and schema"
Expand Down
2 changes: 2 additions & 0 deletions test/ctia/properties_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"ctia.store.es.malware.rollover.max_age" s/Str
"ctia.store.es.malware.aliased" s/Bool
"ctia.store.es.malware.default_operator" (s/enum "OR" "AND")
"ctia.store.es.malware.allow_partial_search_results" s/Bool
"ctia.store.es.malware.version" s/Num
"ctia.store.es.malware.update-mappings" s/Bool
"ctia.store.es.malware.update-settings" s/Bool
Expand Down Expand Up @@ -46,6 +47,7 @@
"prefix.sighting.rollover.max_age" s/Str
"prefix.sighting.aliased" s/Bool
"prefix.sighting.default_operator" (s/enum "OR" "AND")
"prefix.sighting.allow_partial_search_results" s/Bool
"prefix.sighting.version" s/Num
"prefix.sighting.update-mappings" s/Bool
"prefix.sighting.update-settings" s/Bool
Expand Down
12 changes: 11 additions & 1 deletion test/ctia/stores/es/crud_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,17 @@
{:sort [{"timestamp" {:order :asc}}
{"id" {:order :asc}}]
:_source [:id :id :owner :groups :tlp :authorized_users :authorized_groups]
:track_total_hits true}))
:track_total_hits true}

{:fields [:id]}
{:default-sort "timestamp,id"
:version 7
:allow_partial_search_results false}
{:sort [{"timestamp" {:order :asc}}
{"id" {:order :asc}}]
:_source [:id :id :owner :groups :tlp :authorized_users :authorized_groups]
:track_total_hits true
:allow_partial_search_results false}))

(deftest make-search-query-test
(es-helpers/for-each-es-version
Expand Down

0 comments on commit c386246

Please sign in to comment.