django solr with haystack problem

94 views
Skip to first unread message

Welly Iskand

unread,
Aug 10, 2018, 8:31:07 AM8/10/18
to Django users
i have problem how to search date_name and table_name and query with django and  solr.
but i just testing search query with django and solr and view it.

i used
solr 7.4
django         2.0.7                
django-haystack 2.8.2.
pysolr 3.7.0

my solr and django have integrated  i set my setting.py 

HAYSTACK_CONNECTIONS = {
    "default": {
        # For Solr:
        "ENGINE": "haystack.backends.solr_backend.SolrEngine",
        "URL": "http://127.0.0.1:8983/solr",
        "TIMEOUT": 60 * 5,
        "INCLUDE_SPELLING": True,
    },

I install haystack http://haystacksearch.org/  and pip3 django-haystack
after installed I copy folder haystack to my apps and set haystack in setting look like this:


# Application definition
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # Update the INSTALLED_APPS in your settings.py file:
    # search apps
    'search',
    'haystack',

    # add widget_tweaks
    # 'widget_tweaks',



my package list used

certifi         2018.4.16            
chardet         3.0.4                
Django          2.0.7                
django-haystack 2.8.2.dev52+g6c4a854 /home/dattabot/djangoadmin/src/django-haystack
freeze          1.0.10               
idna            2.7                  
pip             18.0                 
pysolr          3.7.0                
pytz            2018.5               
requests        2.19.1               
setuptools      40.0.0               
six             1.11.0               
urllib3         1.23                 
wheel           0.31.1  

so i test with ./manage.py  i see haystack command

   [haystack]
        build_solr_schema
        clear_index
        haystack_info
        rebuild_index
        update_index


so i created model.py 

from django.db import models
from django.contrib.auth.models import User


#Create your models here#
class Note(models.Model):
    user=models.ForeignKey(User,on_delete=models.CASCADE)
    pub_date=models.DateTimeField()
    title=models.CharField(max_length=200)
    body =models.TextField()
    description =models.TextField()

    def __unicode__(self):
        return self.title 


in search_indexes.py code

import datetime
from haystack import indexes
from search.models import Note


class NoteIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True,use_template=True)
    author = indexes.CharField(model_attr='user')
    pub_date = indexes.DateTimeField(model_attr='pub_date')


def get_model(self):
    return Note

def index_queryset(self, using=None):
    """Used when the entire index for model is update."""
    return self.get_model().objects.filter(pub_date__lte=datetime.datetime.now())


my structure template 

stucture.jpg


in my base.html 

{% load static %}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <link rel="stylesheet" href="{% static 'search/layout/search.css' %}">
<title>dattabot data</title>
</head>
{% include 'search/layouts/navigation.html' %}
{% block content %}
<body>
<div>
    <h1 align="center" xmlns="http://www.w3.org/1999/html" xmlns="http://www.w3.org/1999/html">Search</h1>
    <form method="get" action="."><center>
    <table>
    {{ form.as_table }}
            <tr>
               <td>&nbsp;</td>
               <td>
                    <input type"search" placeholder="search open data" size="50" maxlength="2048">
                    <input type="submit" value="Search">
                </td>
            </tr>
    </table>

{% if query %}
            <h3>Results</h3>

            {% for result in page.object_list %}
                <p>
                    <a href="{{ result.object.get_absolute_url }}">{{ result.object.title }}</a>
                </p>
            {% empty %}
                <p>No results found.</p>
            {% endfor %}


            {% if page.has_previous or page.has_next %}
                <div>
                    {% if page.has_previous %}<a href="?q={{ query }}&amp;page={{ page.previous_page_number }}">{% endif %}&laquo; Previous{% if page.has_previous %}</a>{% endif %}
                    |
                    {% if page.has_next %}<a href="?q={{ query }}&amp;page={{ page.next_page_number }}">{% endif %}Next &raquo;{% if page.has_next %}</a>{% endif %}
                </div>
            {% endif %}
        {% else %}
            {# Show some example queries to run, maybe query syntax, something else? #}
        {% endif %}

</form>
   

 {% include 'search/layouts/footer.html' %}
</div>


    </body>
    </html>

{% endblock %}


in my urls

#created many pages urls
from django.urls import path
from django.conf.urls import url,include 
from . import views


urlpatterns = [
  #  url(r'^&',views.index()),
    path('',views.base,name ='base'),
    path('about/',views.about,name='about'),
    path('contact/',views.contact,name='contact'),
    url(r'^search/',include('haystack.urls')),


my view site that normal but when i  create schema ,cause solr it backend ./manage.py build_solr_schema  >schema.xml
i will testing  ./manage.py  rebuild_index it's same

i get the error like this 
error.jpg









i test with python shell 

$ python
>> import django
>> django.VERSION
>>import haystack 

it's working ,what;s problem?
does anyone have experience about .django,solr with haystack ?


Welly Iskand
FullStack Developer
@wellyiskand IG

Jason

unread,
Aug 10, 2018, 10:54:09 AM8/10/18
to Django users
if your code indentation is accurate, check get_model and index_queryset are actually part of the NoteIndex class

class NoteIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True,use_template=True)
    author = indexes.CharField(model_attr='user')
    pub_date = indexes.DateTimeField(model_attr='pub_date')


def get_model(self):
    return Note

def index_queryset(self, using=None):
    """Used when the entire index for model is update."""
    return self.get_model().objects.filter(pub_date__lte=datetime.datetime.now())

as you posted suggests that is not the case.
Reply all
Reply to author
Forward
0 new messages