Skip to content

Commit

Permalink
Render raw string queries
Browse files Browse the repository at this point in the history
  • Loading branch information
jvia committed Mar 14, 2024
1 parent 9580f7e commit d7934bc
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 7 deletions.
5 changes: 3 additions & 2 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@
{:source-paths ["dev"]
:jvm-opts ["-Dorg.slf4j.simpleLogger.defaultLogLevel=debug"]
:dependencies
[[metosin/testit "0.4.1"]
[[criterium "0.4.6"]
[lambdaisland/kaocha "1.66.1034"]
[metosin/testit "0.4.1"]
[nubank/matcher-combinators "3.9.1"]
[org.clojure/test.check "1.1.1"]
[criterium "0.4.6"]
[org.slf4j/slf4j-api "1.7.36"]
[org.slf4j/slf4j-simple "1.7.36"]]}}

Expand Down
14 changes: 13 additions & 1 deletion src/com/nytimes/querqy/elasticsearch.clj
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,15 @@
[com.nytimes.querqy.protocols :as p])
(:import
(com.nytimes.querqy.model RawQuery)
(querqy.model BooleanQuery BoostQuery BoostedTerm DisjunctionMaxQuery ExpandedQuery MatchAllQuery Term)))
(querqy.model
BooleanQuery
BoostQuery
BoostedTerm
DisjunctionMaxQuery
ExpandedQuery
MatchAllQuery
StringRawQuery
Term)))

(defprotocol InternalEmitter
(emit* [this opts]))
Expand Down Expand Up @@ -145,6 +153,10 @@
(emit* [query _opts]
(:query query))

StringRawQuery
(emit* [query _opts]
(.getQueryString query))

BoostQuery
(emit* [query opts]
;; Create a function_score wight query
Expand Down
12 changes: 11 additions & 1 deletion src/com/nytimes/querqy/model.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
MatchAllQuery
QuerqyQuery
Query
Term)))
Term
StringRawQuery)))

;; ----------------------------------------------------------------------
;; Helpers
Expand Down Expand Up @@ -203,6 +204,11 @@
generated false}}]
(RawQuery. parent occur query generated))

(defn raws
"Create a raw string query."
[& {:keys [parent occur query generated] :or {occur should generated false}}]
(StringRawQuery. parent query occur generated))

;; ----------------------------------------------------------------------
;; Query

Expand Down Expand Up @@ -256,6 +262,10 @@
(datafy [^RawQuery q]
{:raw/query (.-query q)})

StringRawQuery
(datafy [^StringRawQuery q]
(.getQueryString q))

Input$SimpleInput
(datafy [^Input$SimpleInput i]
{:type Input$SimpleInput
Expand Down
6 changes: 6 additions & 0 deletions test/com/nytimes/querqy/common-rules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,9 @@ A8 =>

(best AND amazon AND show) =>
UP(2): amazon

(best AND airfryer AND NOT (recipe OR recipes)) =>
UP(2): wirecutter

sample raw rule =>
FILTER: * {"term": {"field": "value"}}
14 changes: 11 additions & 3 deletions test/com/nytimes/querqy/commonrules_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
(:require
[clojure.datafy :refer [datafy]]
[clojure.java.io :as io]
[clojure.test :refer [deftest is]]
[clojure.test :refer [deftest is testing]]
[com.nytimes.querqy :as querqy]
[com.nytimes.querqy.commonrules :as r :refer [boost delete filter match match* synonym]]
[com.nytimes.querqy.commonrules :as r
:refer [boost delete filter match
match* synonym]]
[matcher-combinators.test :refer [match?]]
[testit.core :refer [=> =in=> facts]])
(:import
(querqy.rewrite.commonrules.select.booleaninput BooleanInputParser)
Expand Down Expand Up @@ -203,7 +206,12 @@

(rewrite dsl-rewriter "best amazon show")
=> {:query {:should [#{{:term "best"}} #{{:term "amazon"}} #{{:term "show"}}]},
:boost [{:query {:must [#{{:term "amazon"}}]}, :boost 2.0}]}))
:boost [{:query {:must [#{{:term "amazon"}}]}, :boost 2.0}]})

(testing "Raw string rules"
(is (match? {:query {:should [#{{:term "sample"}} #{{:term "raw"}} #{{:term "rule"}}]},
:filter ["{\"term\": {\"field\": \"value\"}}"]}
(rewrite resource-rewriter "sample raw rule")))))

;; Custom Functions

Expand Down

0 comments on commit d7934bc

Please sign in to comment.