Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Index-time boost #28

Open
kay1232 opened this issue Aug 18, 2011 · 0 comments
Open

Index-time boost #28

kay1232 opened this issue Aug 18, 2011 · 0 comments

Comments

@kay1232
Copy link

kay1232 commented Aug 18, 2011

Index-time boost can be applied to document and to field - http://wiki.apache.org/solr/UpdateXmlMessages
I implemented it as _boost and _boost_fieldname for array, but this naming won't work for objects - therefore object tests are commented.
Any better ideas for objects?

diff --git a/sunburnt/schema.py b/sunburnt/schema.py
index 4d39cb9..d561273 100644
--- a/sunburnt/schema.py
+++ b/sunburnt/schema.py
@@ -518,12 +518,15 @@ class SolrUpdate(object):
         self.schema = schema
         self.xml = self.add(docs)

-    def fields(self, name, values):
+    def fields(self, name, values, boost=None):
         # values may be multivalued - so we treat that as the default case
         if not hasattr(values, "__iter__"):
             values = [values]
         field_values = [self.schema.field_from_user_data(name, value) for value in values]
-        return [self.FIELD({'name':name}, field_value.to_solr())
+        attrs = {'name':name}
+        if boost is not None:
+            attrs["boost"] = str(boost)
+        return [self.FIELD(attrs, field_value.to_solr())
             for field_value in field_values]

     def doc(self, doc):
@@ -534,9 +537,14 @@ class SolrUpdate(object):
         if not doc:
             return self.DOC()
         else:
-            return self.DOC(*reduce(operator.add,
-                                    [self.fields(name, values)
-                                     for name, values in doc.items()]))
+            body = reduce(operator.add,
+                          [self.fields(name, values, doc.get("_boost_"+name))
+                           for name, values in doc.items()
+                           if not name.startswith("_boost")])
+            if "_boost" in doc:
+                return self.DOC(boost=str(doc["_boost"]), *body)
+            else:
+                return self.DOC(*body)

     def add(self, docs):
         if hasattr(docs, "items") or not hasattr(docs, "__iter__"):
diff --git a/sunburnt/test_schema.py b/sunburnt/test_schema.py
index e5d3209..f59ea05 100644
--- a/sunburnt/test_schema.py
+++ b/sunburnt/test_schema.py
@@ -220,6 +220,14 @@ class D_with_callables(object):
     def my_arse(self):
         return self._my_arse

+#class D_with_boost(object):
+#    def __init__(self, int_field, text_field, boost=None, boost_int_field=None):
+#        self.int_field = int_field
+#        self.text_field = text_field
+#        if boost:
+#            self._boost = boost
+#        if boost_int_field:
+#            self._boost_int_field = boost_int_field

 update_docs = [
     # One single dictionary, not making use of multivalued field
@@ -263,6 +271,22 @@ update_docs = [
     # Check that strings aren't query-escaped
     (D(1, "a b", True),
      """<add><doc><field name="int_field">1</field><field name="text_field">a b</field></doc></add>"""),
+
+
+    # Seriaize _boost for whole document
+    ({"int_field":1, "text_field":"a b", "_boost": 2.5},
+     """<add><doc boost="2.5"><field name="int_field">1</field><field name="text_field">a b</field></doc></add>"""),
+
+#    (D_with_boost(1, "a b", 2.5, None),
+#     """<add><doc boost="2.5"><field name="int_field">1</field><field name="text_field">a b</field></doc></add>"""),
+
+    # Seriaize _boost for single field
+    ({"int_field":1, "text_field":"a b", "_boost_int_field": 2.5},
+     """<add><doc><field boost="2.5" name="int_field">1</field><field name="text_field">a b</field></doc></add>"""),
+
+#    (D_with_boost(1, "a b", None, 2.5),
+#     """<add><doc><field name="int_field" boost="2.5">1</field><field name="text_field">a b</field></doc></add>"""),
+
     ]

 def check_update_serialization(s, obj, xml_string):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant