NotFoundError(404, 'index_not_found_exception', 'no such index [productinventory]', productinventory, index_or_alias)
shakori999 opened this issue · 2 comments
now I'm at part 9 , and before adding more codes , i just merged my branches and try to see if everything works before move on ,
my DRF and ninja work fine without any problem, but when I tried to navigate to http://127.0.0.1:8000/api/search/widstar/
i got this error
NotFoundError(404, 'index_not_found_exception', 'no such index [productinventory]', productinventory, index_or_alias)
and these are my files , i think it related to the problem, just for notice it was work fine before, (at the end of part 7 as I remembered )
serach/documents.py
from django_elasticsearch_dsl import Document, fields
from django_elasticsearch_dsl.registries import registry
from inventory.models import ProductInventory
@registry.register_document
class ProductInventoryDocument(Document):
product = fields.ObjectField(
properties={"name": fields.TextField(), "web_id": fields.TextField()}
)
brand = fields.ObjectField(properties={"name": fields.TextField()})
class Index:
name = "productinventory"
class Django:
model = ProductInventory
fields = [
"id",
"sku",
"store_price",
"is_default",
]
search/views.py
from django.http import HttpResponse
from drf.serializer import ProductInventorySearchSerializer
from search.documents import ProductInventoryDocument
from elasticsearch_dsl import Q
from rest_framework.pagination import LimitOffsetPagination
from rest_framework.views import APIView
class SearchProductInventory(APIView, LimitOffsetPagination):
productinventory_serializer = ProductInventorySearchSerializer
search_document = ProductInventoryDocument
def get(self, request, query=None):
try:
q = Q(
"multi_match",
query=query,
fields=["product.name", "product.web_id", "brand.name"],
fuzziness="auto",
) & Q(
should=[
Q("match", is_default=True),
],
minimum_should_match=1,
)
search = self.search_document.search().query(q)
response = search.execute()
results = self.paginate_queryset(response, request, view=self)
serializer = self.productinventory_serializer(results, many=True)
return self.get_paginated_response(serializer.data)
except Exception as e:
return HttpResponse(e, status=500)
config/urls.py
from django.conf import settings
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path, include
from drf.views import *
from search.views import SearchProductInventory
urlpatterns = [
# Djago admin
# User management
path("accounts/", include("allauth.urls")),
# Local apps
path("", include("dashboard.urls")),
path("books/", include("books.urls")),
path("order/", include("order.urls")),
path("checkout/", include("checkout.urls")),
# API endponts
path("api/inventory/category/all/", CategoryList.as_view()),
path("api/inventory/products/category/<str:query>/", ProductByCategory.as_view()),
path("api/inventory/<int:query>/", ProductInventoryByWebId.as_view()),
# ealsticsearch
path("api/search/<str:query>/", SearchProductInventory.as_view()),
path("ninja/", include("dninja.urls")),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
if settings.DEBUG:
import debug_toolbar
urlpatterns = [
path("__debug__", include(debug_toolbar.urls)),
] + urlpatterns
parts from settings.py
INSTALLED_APPS = [
"rest_framework",
"django_elasticsearch_dsl",
"ninja",
# Local
"accounts",
"pages",
"books",
"order",
"checkout",
# New apps for updateing database
"dashboard",
"inventory",
"demo",
# new apps for DRF
"drf",
"search",
"dninja",
]
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql_psycopg2",
"NAME": "ecommerce",
"USER": "ecommerce",
"PASSWORD": "password",
"HOST": "db",
"PORT": "5432",
"TEST": {
"NAME": "testdatabase",
},
}
}
ELASTICSEARCH_DSL = {
"default": {
"hosts": "esearch",
}
}
dokcer-compose.yml
version: '3.8'
services:
web:
restart: always
container_name: web
build: .
command: gunicorn config.wsgi -b 0.0.0.0:8000
volumes:
- .:/usr/src/app
ports:
- "8000:8000"
depends_on:
- db
- redis
image: app:djnago
environment:
- "DJANGO_SECRET_KEY=t8wrj!=x&em6+x%z1_!m6n8m3$$jis8!wkhxzn##!_(-%-u9t2t"
- "DJANGO_DEBUG=True"
- "DJANGO_SECURE_SSL_REDIRECT=False"
- "DJANGO_SECURE_HSTS_SECONDS=0"
- "DJANGO_SECURE_HSTS_INCLUDE_SUBDOMAINS=False"
- "DJANGO_SECURE_HSTS_PRELOAD=False"
- "DJANGO_SESSION_COOKIE_SECURE=False"
- "DJANGO_CSRF_COOKIE_SECURE=False"
- USE_S3=false
db:
image: postgres:14
container_name: db
restart: always
volumes:
- postgres_data:/var/lib/postgresql/data/
environment:
- "POSTGRES_HOST_AUTH_METHOD=trust"
- POSTGRES_DB=ecommerce
- POSTGRES_USER=ecommerce
- POSTGRES_PASSWORD=password
ports:
- "5432:5432"
esearch:
container_name: esearch
image: elasticsearch:7.14.2
environment:
- cluster.name=ecommerce-elasticsearch
- discovery.type=single-node
- bootstrap.memory_lock=true
- ES_JAVA_OPTS=-Xms128m -Xmx128m
ports:
- "9200:9200"
volumes:
- ./elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml
depends_on:
- db
redis:
container_name: redis
restart: always
image: redis:alpine
ports:
- "6379:6379"
volumes:
postgres_data:
esearch:
Dockerfile
#Pull base image
FROM python
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Set work directory
WORKDIR /usr/src/app
# Install dependencies
RUN pip install --upgrade pip
COPY ./requirements.txt /usr/src/app/requirements.txt
RUN pip install -r requirements.txt
# Copy project
COPY . /usr/src/app/
I hope if you can find the problem, I tried and checked many times and still doesn't work.
actully I didn't do anything new , i just tried many time to delete indexes and rebuild them , and restart my docker, now it works( which is i did the same steps before many time and didn't work)