From d7934bc431ac624714d4c9d3f9c4d1c7ba2dda54 Mon Sep 17 00:00:00 2001 From: Jeremiah Via Date: Thu, 14 Mar 2024 11:56:37 -0700 Subject: [PATCH] Render raw string queries --- project.clj | 5 +++-- src/com/nytimes/querqy/elasticsearch.clj | 14 +++++++++++++- src/com/nytimes/querqy/model.clj | 12 +++++++++++- test/com/nytimes/querqy/common-rules.txt | 6 ++++++ test/com/nytimes/querqy/commonrules_test.clj | 14 +++++++++++--- 5 files changed, 44 insertions(+), 7 deletions(-) diff --git a/project.clj b/project.clj index 6719271..5912786 100644 --- a/project.clj +++ b/project.clj @@ -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"]]}} diff --git a/src/com/nytimes/querqy/elasticsearch.clj b/src/com/nytimes/querqy/elasticsearch.clj index 6ee54b1..27100b4 100644 --- a/src/com/nytimes/querqy/elasticsearch.clj +++ b/src/com/nytimes/querqy/elasticsearch.clj @@ -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])) @@ -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 diff --git a/src/com/nytimes/querqy/model.clj b/src/com/nytimes/querqy/model.clj index cb59a12..0fe0944 100644 --- a/src/com/nytimes/querqy/model.clj +++ b/src/com/nytimes/querqy/model.clj @@ -17,7 +17,8 @@ MatchAllQuery QuerqyQuery Query - Term))) + Term + StringRawQuery))) ;; ---------------------------------------------------------------------- ;; Helpers @@ -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 @@ -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 diff --git a/test/com/nytimes/querqy/common-rules.txt b/test/com/nytimes/querqy/common-rules.txt index 8f540de..fee03fe 100644 --- a/test/com/nytimes/querqy/common-rules.txt +++ b/test/com/nytimes/querqy/common-rules.txt @@ -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"}} diff --git a/test/com/nytimes/querqy/commonrules_test.clj b/test/com/nytimes/querqy/commonrules_test.clj index 75c2c64..481af7f 100644 --- a/test/com/nytimes/querqy/commonrules_test.clj +++ b/test/com/nytimes/querqy/commonrules_test.clj @@ -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) @@ -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