diff --git a/src/com/nytimes/querqy/context.clj b/src/com/nytimes/querqy/context.clj index 1587465..213d1d3 100644 --- a/src/com/nytimes/querqy/context.clj +++ b/src/com/nytimes/querqy/context.clj @@ -1,7 +1,7 @@ (ns com.nytimes.querqy.context (:import - (java.util Optional) - (querqy.rewrite RewriteLoggingConfig SearchEngineRequestAdapter))) + (java.util Optional) + (querqy.rewrite RewriteLoggingConfig SearchEngineRequestAdapter))) (defn optional ([] (Optional/empty)) diff --git a/src/com/nytimes/querqy/model.clj b/src/com/nytimes/querqy/model.clj index 9f5a132..cb658b4 100644 --- a/src/com/nytimes/querqy/model.clj +++ b/src/com/nytimes/querqy/model.clj @@ -111,34 +111,34 @@ (extend-protocol cp/Datafiable BooleanQuery (datafy [^BooleanQuery q] - {:type BooleanQuery - :occur (occur->kw (.getOccur q)) - :clauses (mapv datafy (.getClauses q))}) + (-> (group-by (comp occur->kw get-occur) (.getClauses q)) + (update-keys (fn [k] (keyword "bool" (name k)))) + (update-vals (partial mapv datafy)))) BoostQuery (datafy [^BoostQuery q] - {:type BoostQuery - :boost (.getBoost q) - :query (datafy (.getQuery q))}) + {:boost/amount (.getBoost q) + :boost/query (datafy (.getQuery q))}) RawQuery (datafy [^RawQuery q] - {:type RawQuery - :query (.-query q)}) + {:raw/query (.-query q)}) DisjunctionMaxQuery (datafy [^DisjunctionMaxQuery q] - {:type DisjunctionMaxQuery - :occur (occur->kw (.getOccur q)) - :clauses (mapv datafy (.getClauses q))}) + {:dismax/clauses (mapv datafy (.getClauses q))}) ExpandedQuery (datafy [^ExpandedQuery q] - {:type ExpandedQuery - :user-query (datafy (.getUserQuery q)) - :boost-up (mapv datafy (.getBoostUpQueries q)) - :boost-down (mapv datafy (.getBoostDownQueries q)) - :filter (mapv datafy (.getFilterQueries q))}) + (cond-> {:expanded/query (datafy (.getUserQuery q))} + (.getBoostUpQueries q) + (assoc :expanded/boost-up (mapv datafy (.getBoostUpQueries q))) + + (.getBoostDownQueries q) + (assoc :expanded/boost-down (mapv datafy (.getBoostDownQueries q))) + + (.getFilterQueries q) + (assoc :expanded/filter (mapv datafy (.getFilterQueries q))))) Input$SimpleInput (datafy [^Input$SimpleInput i] @@ -149,24 +149,22 @@ MatchAllQuery (datafy [^MatchAllQuery _q] - {:type MatchAllQuery - :match_all {}}) + {:match_all {}}) Query (datafy [^Query q] - {:type Query - :occur (occur->kw (.getOccur q)) - :clauses (mapv datafy (.getClauses q))}) + (-> (group-by (comp occur->kw get-occur) (.getClauses q)) + (update-keys (fn [k] (keyword "query" (name k)))) + (update-vals (partial mapv datafy)))) BoostedTerm (datafy [^BoostedTerm t] - {:type BoostedTerm - :field (.getField t) - :boost (.getBoost t) - :value (str (.getValue t))}) + {:boost/field (.getField t) + :boost/amount (.getBoost t) + :boost/value (str (.getValue t))}) Term (datafy [^Term t] - {:type Term - :field (.getField t) - :value (str (.getValue t))})) + (cond-> {:term/value (str (.getValue t))} + (.getField t) + (assoc :term/field (.getField t))))) diff --git a/src/com/nytimes/querqy/protocols.clj b/src/com/nytimes/querqy/protocols.clj index c8b2fd3..5a27a8b 100644 --- a/src/com/nytimes/querqy/protocols.clj +++ b/src/com/nytimes/querqy/protocols.clj @@ -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 QueryRewriter RewriteChain RewriterFactory SearchEngineRequestAdapter))) + (querqy.model ExpandedQuery Query) + (querqy.rewrite QueryRewriter RewriteChain RewriterFactory SearchEngineRequestAdapter))) (defprotocol Parser (parse ^Query [this ^String string] diff --git a/test/com/nytimes/querqy/commonrules_test.clj b/test/com/nytimes/querqy/commonrules_test.clj index 8921c08..c15b32b 100644 --- a/test/com/nytimes/querqy/commonrules_test.clj +++ b/test/com/nytimes/querqy/commonrules_test.clj @@ -83,6 +83,13 @@ (datafy (querqy/rewrite rw string))) (deftest rewriter-test + (facts "A1" + (rewrite resource-rewriter "A1") + => {} + + (rewrite dsl-rewriter "A1") + => {}) + (facts "DSL & Resource Rewriters have the same output" (rewrite dsl-rewriter "A1") => (rewrite resource-rewriter "A1") (rewrite dsl-rewriter "A2 B2") => (rewrite resource-rewriter "A2 B2")