您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

how_to_setup_solr.rst 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. .. spelling::
  2. Solr
  3. ============================
  4. How to setup Solr with Oscar
  5. ============================
  6. `Apache Solr`_ is Oscar's recommended production-grade search backend. This
  7. how-to describes how to get Solr running, and integrated with Oscar. The
  8. instructions below are tested on an Debian/Ubuntu machine, but should be applicable
  9. for similar environments. A working Java or OpenJDK version 8 installation are
  10. necessary.
  11. .. _`Apache Solr`: https://lucene.apache.org/solr/
  12. Python Package
  13. ==============
  14. You will need to install the ``pysolr`` Python package. Or add it to your
  15. `requirements.txt`:
  16. .. code-block:: bash
  17. $ pip install pysolr
  18. Installing Solr
  19. ===============
  20. You first need to fetch and extract Solr. The schema included with Oscar
  21. is tested with Solr 6.6.6:
  22. .. code-block:: bash
  23. $ wget -O ${HOME}/solr-6.6.6.tgz https://archive.apache.org/dist/lucene/solr/6.6.6/solr-6.6.6.tgz
  24. $ tar xzf ${HOME}/solr-6.6.6.tgz --directory=${HOME}
  25. $ ln -s ${HOME}/solr-6.6.6 ${HOME}/solr
  26. .. note::
  27. For development this will presume the solr directory is in
  28. the users ``HOME`` directory. (For an actual deployment this may be better
  29. placed in ``/opt``).
  30. Start Solr and Create a Core
  31. ======================================
  32. Next start up Solr and create a core named ``sandbox`` this name will be used
  33. through out this howto, change ``sandbox`` to something that suits your installation.
  34. This step also sets up up the directory structure and a basic configuration.
  35. .. code-block:: bash
  36. $ cd ${HOME}/solr
  37. $ ./bin/solr start
  38. $ ./bin/solr create -c sandbox -n basic_config
  39. Integrating with Haystack
  40. =========================
  41. Haystack provides an abstraction layer on top of different search backends and
  42. integrates with Django. The Haystack connection settings in your
  43. ``settings.py`` for the config above will look like this:
  44. .. code-block:: python
  45. # Solr 6.x
  46. HAYSTACK_CONNECTIONS = {
  47. 'default': {
  48. 'ENGINE': 'haystack.backends.solr_backend.SolrEngine',
  49. 'URL': 'http://127.0.0.1:8983/solr/sandbox',
  50. 'ADMIN_URL': 'http://127.0.0.1:8983/solr/admin/cores',
  51. 'INCLUDE_SPELLING': True,
  52. },
  53. }
  54. To use Solr with the Sandbox locally comment out the ``HAYSTACK_CONNECTIONS``
  55. section using the WhooshEngine and uncomment the Solr 6.x section like this one.
  56. Build Solr schema
  57. =================
  58. Next, get Oscar to generate the ``schema.xml`` and ``solrconfig.xml`` for Solr.
  59. .. code-block:: bash
  60. $ ./manage.py build_solr_schema --configure-directory=${HOME}/solr/server/solr/sandbox/conf
  61. $ ./manage.py build_solr_schema --reload-core sandbox
  62. .. note::
  63. If using this Solr install with the Sandbox locally ensure the steps up to
  64. this point have been done prior to running ``make sandbox`` in the
  65. `Run the sandbox locally <https://django-oscar.readthedocs.io/en/latest/internals/sandbox.html#run-the-sandbox-locally>`_
  66. instructions.
  67. Rebuild search index
  68. ====================
  69. If all is well, you should now be able to rebuild the search index.
  70. .. code-block:: bash
  71. $ ./manage.py rebuild_index --noinput
  72. Removing all documents from your index because you said so.
  73. All documents removed.
  74. Indexing 201 Products
  75. If the indexing succeeded, search in Oscar will be working. Search for any term
  76. in the search box on your Oscar site, and you should get results.
  77. Add custom product attributes in faceted search
  78. ===============================================
  79. Fork Oscar's search app and add the additional fields to your search index:
  80. .. code-block:: bash
  81. $ ./manage.py fork_app search <yourapp>
  82. $ touch <yourapp>/search/search_indexes.py
  83. Edit ``<yourapp>/search/search_indexes.py``, create a subclass of Oscar's
  84. ProductIndex with your additional fields:
  85. .. code-block:: python
  86. from oscar.apps.search import ProductIndex as OscarProductIndex
  87. class ProductIndex(OscarProductIndex):
  88. format = indexes.CharField(null=True, faceted=True)
  89. color = indexes.CharField(null=True, faceted=True)
  90. def prepare_format(self, obj):
  91. return obj.attribute_values.get(attribute__code="format").value_text
  92. def prepare_color(self, obj):
  93. return obj.attribute_values.get(attribute__code="color").value_text
  94. Add the new fields to your ``schema.xml``:
  95. .. code-block:: xml
  96. <field name="format" type="text" indexed="true" stored="true" multiValued="false" />
  97. <field name="format_exact" type="string" indexed="true" stored="true" multiValued="false" />
  98. <field name="color" type="text" indexed="true" stored="true" multiValued="false" />
  99. <field name="color_exact" type="string" indexed="true" stored="true" multiValued="false" />
  100. Reload your new ``schema.xml`` & restart solr:
  101. .. code-block:: bash
  102. $ curl http://localhost:8983/solr/admin/cores?action=RELOAD&core=sandbox
  103. $ cd ${HOME}/solr
  104. $ ./bin/solr restart
  105. Rebuild the search index with the new fields:
  106. .. code-block:: bash
  107. $ cd <path to your app>
  108. $ ./manage.py rebuild_index --noinput
  109. Add the new fields in ``settings.py`` to ``OSCAR_SEARCH_FACETS``:
  110. .. code-block:: python
  111. OSCAR_SEARCH_FACETS = {
  112. "fields": OrderedDict(
  113. [
  114. # ....
  115. ("format", {"name": "Format", "field": "format"}),
  116. ("color", {"name": "Color", "field": "color"}),
  117. ],
  118. ),
  119. # ...
  120. }
  121. The new fields will appear automatically in the left column below your
  122. category tree in the search facets.