4

Does Django haystack work with the latest Solr update (8.5.1)? Plus how do I setup with my Django blog project

Charles
  • 63
  • 1
  • 6

2 Answers2

7

CentOS 8, Solr 8.7, Django Oscar 3.0

1 ) Install Java

yum update
yum install java-1.8.0-openjdk lsof

2 ) Install Solr

cd /tmp
wget https://downloads.apache.org/lucene/solr/8.7.0/solr-8.7.0.tgz
tar xzf solr-8.7.0.tgz solr-8.7.0/bin/install_solr_service.sh --strip-components=2
./install_solr_service.sh solr-8.7.0.tgz

systemctl enable solr
systemctl start solr

3 ) Limits

Tune your linux limits for solr user. You can do it here /etc/security/limits.conf. Add rows at the end of the file:

solr        soft    nofile      65535
solr        hard    nofile      65535

Or here /etc/systemd/system.conf set the vars:

DefaultLimitNOFILE=65000
DefaultLimitNPROC=65000

4 ) Firewall

Close the port 8983 in your iptables or firewalld

5 ) Reboot

reboot

6 ) Creating solr config files

cd /opt/solr-8.7.0/
sudo -i -u solr /opt/solr-8.7.0/bin/solr create -c haystack

Configuration files directory will be created here: /var/solr/data/haystack/

systemctl restart solr

7 ) Haystack settings in settings.py

INSTALLED_APPS = [
    "django.contrib.admin",
    ...
    "haystack",
]

HAYSTACK_CONNECTIONS = {
    "default": {
        "ENGINE": "haystack.backends.solr_backend.SolrEngine",
        "URL": "http://127.0.0.1:8983/solr/haystack",
        "INCLUDE_SPELLING": True,
    },
}

Restart supervisord:

systemctl restart supervisord

8 ) Switch to virtualenv

source path_to_your_venv_activate_script # (example: source /tmp/venv/django/bin/activate)
cd path_to_your_manage_py_location_directory

9 ) Create schema

cp -p /var/solr/data/haystack/conf/managed-schema /var/solr/data/haystack/conf/managed-schema.copy
python manage.py build_solr_schema > /var/solr/data/haystack/conf/managed-schema

In the file /var/solr/data/haystack/conf/managed-schema replace

<field name="django_ct" type="string" indexed="true" stored="true" multiValued="false"/>

with

<!-- <field name="django_ct" type="string" indexed="true" stored="true" multiValued="false"/> -->
<field name="django_ct" type="text_general" indexed="true" stored="true" multiValued="false"/>

In the file /var/solr/data/haystack/conf/managed-schema find last </fieldType> and add after it

<!-- NRR manual insert start -->
<!-- Lines from origin managed-schema: -->
<fieldType name="pdate" class="solr.DatePointField" docValues="true"/>
<fieldType name="pdates" class="solr.DatePointField" docValues="true" multiValued="true"/>
<fieldType name="pdouble" class="solr.DoublePointField" docValues="true"/>
<fieldType name="pdoubles" class="solr.DoublePointField" docValues="true" multiValued="true"/>
<fieldType name="pfloat" class="solr.FloatPointField" docValues="true"/>
<fieldType name="pfloats" class="solr.FloatPointField" docValues="true" multiValued="true"/>
<fieldType name="pint" class="solr.IntPointField" docValues="true"/>
<fieldType name="pints" class="solr.IntPointField" docValues="true" multiValued="true"/>
<fieldType name="plong" class="solr.LongPointField" docValues="true"/>
<fieldType name="plongs" class="solr.LongPointField" docValues="true" multiValued="true"/>
<!-- NRR manual insert end -->

Copy currency.xml to your working dir:

cp -p /opt/solr-8.7.0/example/example-DIH/solr/solr/conf/currency.xml /var/solr/data/haystack/conf/

chown solr:solr /var/solr/data/haystack/conf/currency.xml

10 ) Restart solr:

systemctl restart solr

11 ) Rebuild index

python manage.py rebuild_index --noinput
Rufat
  • 536
  • 1
  • 8
  • 25
  • Please can you explain why should I update the managed-schema.xml? – Nwawel A Iroume Jun 17 '22 at 19:06
  • The lines for the managed-schema are copied from the source file and are required for normal operation. – Rufat Jun 19 '22 at 05:13
  • 1
    Ok. so please I don't understand why it is not included in the output schema from `python manage.py build_solr_schema`? because every time I update my schema definition from this command I have to update manually again and again.... and this is boring. – Nwawel A Iroume Jun 19 '22 at 08:54
  • It's almost correct! the build_solr_schema should be name schemas.xml and placed inside the conf dir, but the managed-schemas should not be touched – Leonardo Hermoso Feb 21 '23 at 21:54
3
Step 1:- Install Package

pip install pysolr
pip install django-haystack

Step 2:- Changes in settings.py for configuration


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    '...',
    'haystack',
]

HAYSTACK_CONNECTIONS = {
    'default': {
        'ENGINE': 'haystack.backends.solr_backend.SolrEngine',
        'URL': 'http://127.0.0.1:8983/solr/blog',
    },
}

HAYSTACK_SIGNAL_PROCESSOR = 'haystack.signals.RealtimeSignalProcessor'

step 3:-Install Apache Solr

apt-get install solr-tomcat

# Update Tomcat's configuration to set the servlet connector port to a sensible value:

vim /etc/tomcat7/server.xml

# Change the value of the Catalina service's Connector port to 8983 (at the time of writing, it defaults to 8080). Restart tomcat.
service tomcat6 restart



Step 4:- Build and install the solr schema

python manage.py build_solr_schema > schema.xml
sudo cp schema.xml /usr/share/solr/conf/schema.xml
sudo systemctl restart tomcat7


step 5:- Build the index for the first time:

python manage.py rebuild_index


Step 6: Update Data in Solr

# Update Solr Index

# Changes to the Database Aren't Reflected in Search Results

python manage.py update_index

# This command updates the Solr index with any changes which are not currently reflected.


# When the Solr Schema Definition has been Changed

python manage.py rebuild_index
Mukul Kumar
  • 2,033
  • 1
  • 7
  • 14
  • You can refer this repository on github:- https://github.com/mukulkkumar/django-pysolr – Mukul Kumar May 21 '20 at 16:32
  • following this, and when manage.py rebuild_index, it gives error like TypeError: index_queryset() got an unexpected keyword argument 'using'.. – Rupal Shah Jul 09 '21 at 11:16