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",
"TIMEOUT": 60 * 5,
"INCLUDE_SPELLING": True,
},
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
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="{% static 'search/layout/search.css' %}">
<title>dattabot data</title>
</head>
{% include 'search/layouts/navigation.html' %}
{% block content %}
<body>
<div>
<form method="get" action="."><center>
<table>
{{ form.as_table }}
<tr>
<td> </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 }}&page={{ page.previous_page_number }}">{% endif %}« Previous{% if page.has_previous %}</a>{% endif %}
|
{% if page.has_next %}<a href="?q={{ query }}&page={{ page.next_page_number }}">{% endif %}Next »{% 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
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