Skip to content

Commit

Permalink
Upgrade to [org.querqy/querqy-core "3.15.0"]
Browse files Browse the repository at this point in the history
  • Loading branch information
jvia committed Jan 19, 2024
1 parent 30714c6 commit d82f968
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 47 deletions.
2 changes: 1 addition & 1 deletion project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@

:dependencies
[[org.clojure/clojure "1.11.1"]
[org.querqy/querqy-core "3.14.0"]]
[org.querqy/querqy-core "3.15.0"]]

:repl-options {:init-ns com.nytimes.querqy}

Expand Down
108 changes: 77 additions & 31 deletions src/com/nytimes/querqy/commonrules.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,50 @@
"CommonRules based rewriter"
(:refer-clojure :exclude [filter])
(:require
[clojure.java.io :as io]
[clojure.string :as str]
[com.nytimes.querqy.model :as model]
[com.nytimes.querqy.parser :as parser])
[clojure.java.io :as io]
[clojure.string :as str]
[com.nytimes.querqy.model :as model]
[com.nytimes.querqy.parser :as parser])
(:import
(java.io Reader)
(java.net URL)
(java.util List UUID)
(querqy.model Input Input$BooleanInput Input$SimpleInput)
(querqy.parser QuerqyParser)
(querqy.rewrite RewriterFactory)
(querqy.rewrite.commonrules CommonRulesRewriter LineParser QuerqyParserFactory SimpleCommonRulesParser WhiteSpaceQuerqyParserFactory)
(querqy.rewrite.commonrules.model BoostInstruction BoostInstruction$BoostDirection BoostInstruction$BoostMethod DeleteInstruction FilterInstruction Instructions SynonymInstruction TrieMapRulesCollectionBuilder)
(querqy.rewrite.commonrules.select SelectionStrategyFactory)
(querqy.rewrite.commonrules.select.booleaninput BooleanInputParser)
(querqy.rewrite.commonrules.select.booleaninput.model BooleanInputElement BooleanInputElement$Type BooleanInputLiteral)))
(java.io
Reader)
(java.net
URL)
(java.util
List
UUID)
(querqy.model
Input
Input$BooleanInput
Input$SimpleInput)
(querqy.parser
QuerqyParser)
(querqy.rewrite
RewriterFactory)
(querqy.rewrite.commonrules
CommonRulesRewriter
LineParser
QuerqyParserFactory
SimpleCommonRulesParser
WhiteSpaceQuerqyParserFactory)
(querqy.rewrite.commonrules.model
BoostInstruction
BoostInstruction$BoostDirection
BoostInstruction$BoostMethod
DeleteInstruction
FilterInstruction
InstructionDescription
Instructions
SynonymInstruction
TrieMapRulesCollectionBuilder)
(querqy.rewrite.commonrules.select
SelectionStrategyFactory)
(querqy.rewrite.commonrules.select.booleaninput
BooleanInputParser)
(querqy.rewrite.commonrules.select.booleaninput.model
BooleanInputElement
BooleanInputElement$Type
BooleanInputLiteral)))

(set! *warn-on-reflection* true)

Expand All @@ -44,8 +72,8 @@
(proxy [RewriterFactory] [(str (UUID/randomUUID))]
(createRewriter [_ _]
(CommonRulesRewriter.
rules
SelectionStrategyFactory/DEFAULT_SELECTION_STRATEGY))
rules
SelectionStrategyFactory/DEFAULT_SELECTION_STRATEGY))
(getCacheableGenerableTerms [] #{})))

;; ----------------------------------------------------------------------
Expand All @@ -59,11 +87,11 @@
ignore-case true
parser (WhiteSpaceQuerqyParserFactory.)}}]
(let [rules-parser (SimpleCommonRulesParser.
^Reader stream
^boolean boolean-input
^QuerqyParserFactory parser
^boolean ignore-case
BoostInstruction$BoostMethod/ADDITIVE)]
^Reader stream
^boolean boolean-input
^QuerqyParserFactory parser
^boolean ignore-case
BoostInstruction$BoostMethod/ADDITIVE)]
(.parse rules-parser))))

(extend-protocol CommonRulesRewriterBuilder
Expand All @@ -78,6 +106,15 @@

(defrecord Rule [input instructions])

(defn- description
^InstructionDescription
[& {:keys [^String type, param, ^String value]}]
(cond-> (InstructionDescription/builder)
(some? type) (.typeName type)
(some? param) (.param param)
(some? value) (.value value)
:finally (.build)))

(defn match*
"Create a "
[head & tail]
Expand Down Expand Up @@ -114,7 +151,9 @@

(defn delete
[string]
(DeleteInstruction. (parse-string string)))
(DeleteInstruction.
(parse-string string)
(description {:type "delete", :value string})))

(defn synonym?
[obj]
Expand All @@ -123,9 +162,12 @@
(defn synonym
"Create a synonym instruction."
([string]
(SynonymInstruction. (parse-string string)))
(synonym 1.0 string))
([boost string]
(SynonymInstruction. (parse-string string) boost)))
(SynonymInstruction.
(parse-string string)
boost
(description {:type "synonym", :param boost, :value string}))))

(defn boost
"Boost a matching term or query."
Expand All @@ -134,15 +176,19 @@
(throw (IllegalArgumentException. "Cannot boost by 0")))
(let [UP BoostInstruction$BoostDirection/UP
DOWN BoostInstruction$BoostDirection/DOWN]
(BoostInstruction. (parse-query query)
(if (>= boost 0) UP DOWN)
BoostInstruction$BoostMethod/ADDITIVE
(abs boost))))
(BoostInstruction.
(parse-query query)
(if (>= boost 0) UP DOWN)
BoostInstruction$BoostMethod/ADDITIVE
(abs boost)
(description {:type "boost", :param boost, :value (pr-str query)}))))

(defn filter
"Add a filter to the query."
[query]
(FilterInstruction. (parse-query query)))
(FilterInstruction.
(parse-query query)
(description {:type "filter", :value (pr-str query)})))

;;; match impl

Expand Down Expand Up @@ -206,4 +252,4 @@
(.addRule rules-builder input literal)))

;;
(rewriter-factory (.build rules-builder)))))
(rewriter-factory (.build rules-builder)))))
15 changes: 7 additions & 8 deletions src/com/nytimes/querqy/context.clj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
(ns com.nytimes.querqy.context
(:import
(java.util Optional)
(querqy.rewrite SearchEngineRequestAdapter)))
(java.util Optional)
(querqy.rewrite RewriteLoggingConfig SearchEngineRequestAdapter)))

(defn optional
([] (Optional/empty))
Expand All @@ -18,12 +18,11 @@
(getFloatRequestParam [_ k] (some-> (get params k) float optional))
(getDoubleRequestParam [_ k] (some-> (get params k) double optional))
(isDebugQuery [_] debug?)
(getInfoLoggingContext [_] (optional)))
(getRewriteLoggingConfig [_] (RewriteLoggingConfig/off)))

(def empty-context
(map->Context
{:chain []
:debug? false
:params {}
:context {}}))

{:chain []
:debug? false
:params {}
:context {}}))
14 changes: 7 additions & 7 deletions src/com/nytimes/querqy/protocols.clj
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
(ns com.nytimes.querqy.protocols
(:require
[com.nytimes.querqy.context :as context])
[com.nytimes.querqy.context :as context])
(:import
(querqy.model ExpandedQuery Query)
(querqy.rewrite ContextAwareQueryRewriter RewriteChain RewriterFactory SearchEngineRequestAdapter)))
(querqy.model ExpandedQuery Query)
(querqy.rewrite QueryRewriter RewriteChain RewriterFactory SearchEngineRequestAdapter)))

(defprotocol Parser
(parse ^Query [this ^String string]
Expand All @@ -21,19 +21,19 @@
;; Some default Rewriter implementations for the Querqy classes.

(extend-protocol Rewriter
ContextAwareQueryRewriter
QueryRewriter
(rewrite
([this query]
(rewrite this query context/empty-context))
([this query context]
(.rewrite this query context)))
(some-> (.rewrite this query context) (.getExpandedQuery))))

RewriteChain
(rewrite
([this query]
(rewrite this query context/empty-context))
([this query context]
(.rewrite this query context)))
(some-> (.rewrite this query context) (.getExpandedQuery))))

RewriterFactory
(rewrite
Expand All @@ -47,4 +47,4 @@

(defprotocol Emitter
(emit [this query opts]
"Emit a system-specific query for a given `querqy.model.ExpandedQuery`"))
"Emit a system-specific query for a given `querqy.model.ExpandedQuery`"))

0 comments on commit d82f968

Please sign in to comment.