Permalink
Browse files

Adds default-operator support to search function.

  • Loading branch information...
scusack committed Apr 11, 2012
1 parent 103a939 commit fb93b1d1504bdcecd7671277e77e86ab59103486
Showing with 9 additions and 3 deletions.
  1. +5 −2 src/clucy/core.clj
  2. +4 −1 test/clucy/test/core.clj
View
@@ -160,12 +160,15 @@ fragments."
(defn search
"Search the supplied index with a query string."
- [index query max-results & {:keys [highlight default-field]}]
+ [index query max-results & {:keys [highlight default-field default-operator]}]
(if (every? false? [default-field *content*])
(throw (Exception. "No default search field specified"))
(let [default-field (or default-field :_content)]
(with-open [searcher (IndexSearcher. index)]
- (let [parser (QueryParser. *version* (as-str default-field) *analyzer*)
+ (let [parser (doto (QueryParser. *version* (as-str default-field) *analyzer*)
+ (.setDefaultOperator (case (or default-operator :or)
+ :and QueryParser/AND_OPERATOR
+ :or QueryParser/OR_OPERATOR)))
query (.parse parser query)
hits (.search searcher query max-results)
highlighter (make-highlighter query searcher highlight)]
View
@@ -33,7 +33,10 @@
(testing "search fn"
(let [index (memory-index)]
(doseq [person people] (add index person))
- (is (== 1 (count (search index "name:miles" 10))))))
+ (is (== 1 (count (search index "name:miles" 10))))
+ (is (== 1 (count (search index "name:miles age:100" 10))))
+ (is (== 0 (count (search index "name:miles AND age:100" 10))))
+ (is (== 0 (count (search index "name:miles age:100" 10 :default-operator :and))))))
(testing "search-and-delete fn"
(let [index (memory-index)]

0 comments on commit fb93b1d

Please sign in to comment.