Skip to content
Snippets Groups Projects
Unverified Commit 57889804 authored by Tim Macdonald's avatar Tim Macdonald Committed by GitHub
Browse files

Abbreviate search context (#15028)

parent c20e9b11
Branches
Tags
No related merge requests found
......@@ -23,6 +23,10 @@
"Results in more dashboards than this are all considered to be equally popular."
50)
(def ^:const surrounding-match-context
"Show this many words of context before/after matches in long search results"
2)
(def searchable-models
"Models that can be searched. The order of this list also influences the order of the results: items earlier in the
list will be ranked higher."
......
......@@ -62,6 +62,18 @@
(count search-tokens)))
fs))
(defn- tokens->string
[tokens abbreviate?]
(let [->string (partial str/join " ")
context search-config/surrounding-match-context]
(if (or (not abbreviate?)
(<= (count tokens) (* 2 context)))
(->string tokens)
(str
(->string (take context tokens))
"…"
(->string (take-last context tokens))))))
(defn- match-context
"Breaks the matched-text into match/no-match chunks and returns a seq of them in order. Each chunk is a map with keys
`is_match` (true/false) and `text`"
......@@ -71,10 +83,11 @@
{:text match-token
:is_match (boolean (some #(matches? % match-token) query-tokens))}))
(partition-by :is_match)
(map (fn [matches-or-misses-map]
{:is_match (:is_match (first matches-or-misses-map))
:text (str/join " "
(map :text matches-or-misses-map))}))))
(map (fn [matches-or-misses-maps]
(let [is-match (:is_match (first matches-or-misses-maps))
text-tokens (map :text matches-or-misses-maps)]
{:is_match is-match
:text (tokens->string text-tokens (not is-match))})))))
(def ^:const text-score-max
"The maximum text score that could be achieved without normalization. This value is then used to normalize it down to the interval [0, 1]"
......
......@@ -129,6 +129,7 @@
(deftest match-context-test
(let [context #'search/match-context
tokens (partial map str)
match (fn [text] {:text text :is_match true})
no-match (fn [text] {:text text :is_match false})]
(testing "it groups matches together"
......@@ -145,7 +146,20 @@
(is (= [(no-match "aviary stats")]
(context
["rasta" "toucan"]
["aviary" "stats"]))))))
["aviary" "stats"]))))
(testing "it abbreviates when necessary"
(is (= [(no-match "one two…eleven twelve")
(match "rasta toucan")
(no-match "alpha beta…the end")]
(context
(tokens '(rasta toucan))
(tokens '(one two
this should not be included
eleven twelve
rasta toucan
alpha beta
some other noise
the end))))))))
(deftest test-largest-common-subseq-length
(let [subseq-length (partial #'search/largest-common-subseq-length =)]
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment