added search fields highlight on search

This commit is contained in:
Alexander Karpov 2022-10-22 22:29:49 +03:00
parent da2b9e16d4
commit 0767c56197
5 changed files with 83 additions and 16 deletions

View File

@ -14,6 +14,7 @@ from search.api.serializers import (
AutoCompleteResponseSerializer,
)
from search.models import Product
from search.services.colors import group
from search.services.search import process_search
from search.services.autocomplete_schema import autocomplete_schema
@ -32,10 +33,13 @@ class SearchApi(APIView):
serializer = SearchSerializer(data=request.data)
serializer.is_valid(raise_exception=True)
return Response(
process_search(
group(
process_search(
serializer.data["body"],
serializer.data["limit"],
serializer.data["offset"],
),
serializer.data["body"],
serializer.data["limit"],
serializer.data["offset"],
),
status=status.HTTP_200_OK,
)

View File

@ -52,7 +52,7 @@ class Product(models.Model):
id = models.IntegerField(
"ID CTE", primary_key=True, unique=True, blank=False, null=False, db_index=True
)
name = models.TextField("Название CTE", unique=True, blank=False)
name = models.TextField("Название CTE", blank=False)
category = models.ForeignKey(
Category, related_name="products", on_delete=models.CASCADE
)

View File

@ -0,0 +1,49 @@
from typing import List, Dict
def group(data: List[Dict], search_fields_d: List[Dict]) -> List[Dict]:
search_fields = []
search_fields_dict = []
for x in search_fields_d:
dat = dict(x)
search_fields.append(dat["value"].lower())
search_fields_dict.append(dat)
re = {}
n = 0
for el in data:
for field in search_fields:
if field in el["name"].lower():
if field in re:
re[field].append((n, 0, el["name"].lower().index(field)))
else:
re[field] = []
re[field].append(
[x["type"] for x in search_fields_dict if x["value"] == field][
0
]
)
re[field].append((n, 0, el["name"].lower().index(field)))
m = 1
for char in el["characteristic"]:
if field in str(char["value"]).lower():
if field in re:
re[field].append(
(n, m, str(char["value"]).lower().index(field))
)
else:
re[field] = []
re[field].append(
[
x["type"]
for x in search_fields_dict
if x["value"].lower() == field
][0]
)
re[field].append(
(n, m, str(char["value"]).lower().index(field))
)
m += 1
n += 1
data += [re]
return data

View File

@ -23,12 +23,10 @@ def load():
def load_excel():
df = pd.read_excel("data.xlsx", sheet_name="Запрос1")
df = pd.read_excel("media/data.xlsx", sheet_name="Запрос1")
for row in range(df.shape[0]):
try:
print(df.iat[row, 0], df.iat[row, 1], df.iat[row, 2])
if Product.objects.filter(id=df.iat[row, 0]).exists():
Product.objects.filter(id=df.iat[row, 0]).delete()
product = Product(id=df.iat[row, 0])
product.name = df.iat[row, 1]
category = Category.objects.get_or_create(name=df.iat[row, 2])[0]
@ -36,16 +34,35 @@ def load_excel():
product.save()
if df.iat[row, 4]:
for cat in literal_eval(df.iat[row, 4]):
try:
if "Value" in cat:
if "Unit" in cat:
ProductUnitCharacteristic.objects.get_or_create(
pr = ProductUnitCharacteristic.objects.get_or_create(
characteristic=UnitCharacteristic.objects.get_or_create(
name=cat["Name"],
value=cat["Value"],
unit=cat["Unit"],
)[0],
product=product,
)[0]
nums = re.findall(
"[-+]?[.]?[\d]+(?:,\d\d\d)*[\.]?\d*(?:[eE][-+]?\d+)?",
cat["Value"],
)
if len(nums) == 1:
pr.numeric_value_min = int(
float(nums[0].replace(",", "."))
)
pr.numeric_value_max = int(
float(nums[0].replace(",", "."))
)
pr.save()
elif len(nums):
nums = [int(float(x.replace(",", "."))) for x in nums]
min_num = min(nums)
max_num = max(nums)
pr.numeric_value_min = min_num
pr.numeric_value_max = max_num
pr.save()
else:
ProductCharacteristic.objects.get_or_create(
characteristic=Characteristic.objects.get_or_create(
@ -53,13 +70,11 @@ def load_excel():
)[0],
product=product,
)
except KeyError:
# Empty Value
continue
except BaseException:
# malformed node or string: nan \ duplicate key
print("СКОРОСШИВАТЕЛЬ")
continue
try:
product.delete()
except Exception:
continue
def process_unit_character():

View File

@ -57,7 +57,6 @@ def apply_qs_search(text: str):
| Product.objects.filter(name__unaccent__icontains=word)
)
products = products.order_by("-score")
print(products)
return products