|
|
@@ -106,3 +106,74 @@ If all is well, you should now be able to rebuild the search index.
|
|
106
|
106
|
|
|
107
|
107
|
If the indexing succeeded, search in Oscar will be working. Search for any term
|
|
108
|
108
|
in the search box on your Oscar site, and you should get results.
|
|
|
109
|
+
|
|
|
110
|
+Add custom product attributes in faceted search
|
|
|
111
|
+===============================================
|
|
|
112
|
+
|
|
|
113
|
+Fork Oscar's search app and add the additional fields to your search index:
|
|
|
114
|
+
|
|
|
115
|
+.. code-block:: bash
|
|
|
116
|
+
|
|
|
117
|
+ $ ./manage.py fork_app search <yourapp>
|
|
|
118
|
+ $ touch <yourapp>/search/search_indexes.py
|
|
|
119
|
+
|
|
|
120
|
+Edit ``<yourapp>/search/search_indexes.py``, create a subclass of Oscar's
|
|
|
121
|
+ProductIndex with your additional fields:
|
|
|
122
|
+
|
|
|
123
|
+.. code-block:: python
|
|
|
124
|
+
|
|
|
125
|
+ from oscar.apps.search import ProductIndex as OscarProductIndex
|
|
|
126
|
+
|
|
|
127
|
+ class ProductIndex(OscarProductIndex):
|
|
|
128
|
+
|
|
|
129
|
+ format = indexes.CharField(null=True, faceted=True)
|
|
|
130
|
+ color = indexes.CharField(null=True, faceted=True)
|
|
|
131
|
+
|
|
|
132
|
+ def prepare_format(self, obj):
|
|
|
133
|
+ return obj.attribute_values.get(attribute__code="format").value_text
|
|
|
134
|
+
|
|
|
135
|
+ def prepare_color(self, obj):
|
|
|
136
|
+ return obj.attribute_values.get(attribute__code="color").value_text
|
|
|
137
|
+
|
|
|
138
|
+Add the new fields to your ``schema.xml``:
|
|
|
139
|
+
|
|
|
140
|
+.. code-block:: xml
|
|
|
141
|
+
|
|
|
142
|
+ <field name="format" type="text" indexed="true" stored="true" multiValued="false" />
|
|
|
143
|
+ <field name="format_exact" type="string" indexed="true" stored="true" multiValued="false" />
|
|
|
144
|
+
|
|
|
145
|
+ <field name="color" type="text" indexed="true" stored="true" multiValued="false" />
|
|
|
146
|
+ <field name="color_exact" type="string" indexed="true" stored="true" multiValued="false" />
|
|
|
147
|
+
|
|
|
148
|
+Reload your new ``schema.xml`` & restart solr:
|
|
|
149
|
+
|
|
|
150
|
+.. code-block:: bash
|
|
|
151
|
+
|
|
|
152
|
+ $ curl http://localhost:8983/solr/admin/cores?action=RELOAD&core=sandbox
|
|
|
153
|
+ $ cd ${HOME}/solr
|
|
|
154
|
+ $ ./bin/solr restart
|
|
|
155
|
+
|
|
|
156
|
+Rebuild the search index with the new fields:
|
|
|
157
|
+
|
|
|
158
|
+.. code-block:: bash
|
|
|
159
|
+
|
|
|
160
|
+ $ cd <path to your app>
|
|
|
161
|
+ $ ./manage.py rebuild_index --noinput
|
|
|
162
|
+
|
|
|
163
|
+Add the new fields in ``settings.py`` to ``OSCAR_SEARCH_FACETS``:
|
|
|
164
|
+
|
|
|
165
|
+.. code-block:: python
|
|
|
166
|
+
|
|
|
167
|
+ OSCAR_SEARCH_FACETS = {
|
|
|
168
|
+ "fields": OrderedDict(
|
|
|
169
|
+ [
|
|
|
170
|
+ # ....
|
|
|
171
|
+ ("format", {"name": "Format", "field": "format"}),
|
|
|
172
|
+ ("color", {"name": "Color", "field": "color"}),
|
|
|
173
|
+ ],
|
|
|
174
|
+ ),
|
|
|
175
|
+ # ...
|
|
|
176
|
+ }
|
|
|
177
|
+
|
|
|
178
|
+The new fields will appear automatically in the left column below your
|
|
|
179
|
+category tree in the search facets.
|