<commit_before>(ns eatme.item-store
  (:use [monger.operators])
  (:require [eatme.config :refer [config]]
            [cheshire.generate]
            [monger.core :as m]
            [monger.collection :as mc]
            [monger.joda-time]
            [eatme.item-library :as library])
  (:import [org.bson.types ObjectId]
           [com.mongodb DB WriteConcern]))

(defn init
  "Connect to Mongo"
  []
  (m/connect-via-uri! (config :MONGOLAB_URI))
  (m/set-db! (m/get-db))) ;; db name is in the uri

(defn add-item [category item]
  (mc/insert "item" (assoc item :category category)))

(defn- replace-_id [i]
  (let [id (str (:_id i))]
    (assoc (dissoc i :_id) :id id)))

(defn suggest-item [q]
  (map #(select-keys % [:Name])
       (mc/find-maps "item" {:Name {$regex (str ".*" q ".*") $options "i"}})))

;; currently only does the first 2 (of 500+) categories
(defn populate-from-library []
  (doseq [category (take 2 (library/all-shelves))]
    (doseq [p (library/products (-> category :shelf :Id))]
      (add-item category p))))<commit_msg>populate now populates everything using upsert, suggest returns top 10 only<commit_after>(ns eatme.item-store
  (:use [monger.operators])
  (:require [eatme.config :refer [config]]
            [cheshire.generate]
            [monger.core :as m]
            [monger.collection :as mc]
            [monger.query :as mq]
            [monger.joda-time]
            [eatme.item-library :as library])
  (:import [org.bson.types ObjectId]
           [com.mongodb DB WriteConcern]))

(defn init
  "Connect to Mongo"
  []
  (m/connect-via-uri! (config :MONGOLAB_URI))
  (m/set-db! (m/get-db))) ;; db name is in the uri

(defn add-item [category item]
  (mc/update "item" {:ProductId (:ProductId item)} (assoc item :category category) :upsert true))

(defn- replace-_id [i]
  (let [id (str (:_id i))]
    (assoc (dissoc i :_id) :id id)))

(defn suggest-item [q]
  (map replace-_id
       (mq/with-collection "item"
         (mq/find {:Name {$regex (str ".*" q ".*") $options "i"}})
         (mq/fields [:Name])
         (mq/limit 10))))

;; currently only does the first 2 (of 500+) categories
(defn populate-from-library []
  (doseq [category (library/all-shelves)]
    (println "Populating category" category)
    (doseq [p (library/products (-> category :shelf :Id))]
      (add-item category p))))
{'fraction_non_alphanumeric': 0.1695132365499573, 'fraction_numerical': 0.005550811272416738, 'mean_word_length': 2.85996705107084, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6478184', 'n_tokens_mistral': 879, 'n_tokens_neox': 833, 'n_words': 231}