How to implement spelling insensitive search in django & PostgreSQL? - python

What I wanted to achieve is, if user enters search for "laptp" then database should return results with actual word "Laptop". Similarly if user enters "ambroidery", then database should return results with both "embroidery" and "embroidred" words containing strings. Hope it clears!!
So what I tried is, I went through whole django documentation and closest thing I found is "Trigram Similarity" search. I followed documentation and tried this:
data = 'silk'
data= Product.objects.annotate(
similarity=TrigramSimilarity('description', data)).filter(similarity__gt=0.3).order_by('-similarity')
In my database, I have Products whose description contains word "silky" but every time I runs this query I get empty query set. Even when I put data value "silky", again I got empty query set.
So first of all suggest me that whether this is right approach for what I wanted to achieve and secondly if it is, then why it is returning empty query set?

Related

Python code to find all fields in a document where the subfield matches a certain value

The picture above shows the structure of my firestore database.
I want to write a query in python that will retrieve all fields in the document "handOffInsulinByGatwork" in which the subfield "medDate" is equal to "2022-02-08T08:51".
I read the examples here: https://firebase.google.com/docs/firestore/query-data/queries, and wrote the query below. Unsurprisingly the query below is producing a blank list (it looks wrong even to my untrained eye).
testVar = db.collection(u'simmer').where('medDate', u'==', '2022-02-08T08:51').get()
What is the correct way to write a query to do the following: In the document "handOffInsulinByGatwork", retrieve all fields in which the subfield "medDate" is equal to "2022-02-08T08:51"?
The medDate fields seems to be under another field in your document. If that's the case, you'll need use dot syntax to address the nested field:
db.collection(u'simmer').where('medSubmutRestOfFieldName.medDate', u'==', '2022-02-08T08:51')

Django Database querying

I'm having an issue here that I want clarification with, see, I'm making a program that does analysis of data.
I wanted to query data from different users, the data is numerical by the way, whenever I'd get subject marks from a user, I want the system to return the name of the user who has those marks.
It was all working fine until I tried querying the users with the same marks, and all I could get was an error
analyzer.models.ResultsALevel.MultipleObjectsReturned: get() returned more than one
ResultsALevel -- it returned 4!
So I was trying to find a way to still query and return the name of users with that subject mark, even if they have the same marks.
I believe that should be possible since the users have different id's and stuff, help would be much appreciated!
Here's my views.py
biology_marks = []
for student in ResultsALevel.objects.all():
biology_marks.append(student.advanced_biology_1)
value_1_biology =
ResultsALevel.objects.get(advanced_biology_1=biology_marks[-1]).student_name.last_name
value_2_biology =
ResultsALevel.objects.get(advanced_biology_1=biology_marks[-2]).student_name.last_name
value_3_biology =
ResultsALevel.objects.get(advanced_biology_1=biology_marks[-3]).student_name.last_name
value_4_biology =
ResultsALevel.objects.get(advanced_biology_1=biology_marks[-4]).student_name.last_name
ObjectManager.get() is used to retrieve single instances, usually selected by a primary key field.
Using ObjectManager.get() on a field where multiple data exists which matches the query, an error is returned (MultipeObjectsReturned)
Use ObjectManager.filter() in stead.

How to search for values in TinyDB

I would like to search my database for a value. I know you can do this for a key with db.search() but there does not seem to be any kind of similar function for searching for a value
I've tried using the contains() function but I have the same issue. It checks if the key is contained in the database. I would like to know if a certain value is contained in the database.
I would like to do something like this that would search for values in tinydb
db.search('value')
If I was able to execute the above command and get the value(if it does exist) or Nothing if it doesn't that would be ideal. Alternatively, if the able returned True or False accordingly, that would be fine as well
I don't know if this is what you are looking for but which the following command you can check for a specific field value:
from tinydb import Query
User = Query()
db.search(User.field_name == 'value')
I'm new here (doing some reading to see if TinyDB would even be applicable for my use case) so perhaps wrong, and also aware that this question is a little old. But I wonder if you can't address this by iterating over each field and searching within for your value. Then, you couldget the key or field wherein a value match was located.

Split Django input field and store into separate rows of record

Here's what I am trying achieve but not sure how to make it happen.
I am building a literature analytic tool. User will paste some text into a field, the system will split word by word into a database table one row for each word, and they have to be sequential. By doing so, later then we can edit and define the translation/annotation of each word.
I am new to Django and hvn't figure out the whole logic behind it yet, but I am curious if it is achievable.
First things first, from the tutorial I just been through from the official Django website, every field created equals to a "cell" in a database table, so what happens if I want to split the content in the input field and store them rows of data in a database table with an ID incremented to index the order of them?
Second, if the above is achieved, when i need to add a word(record) in the middle of the database, like adding something in the middle of an array, is it achievable?
Not sure if what you're asking but this is how I would do it based on what you have said
# models
class Word(models.Model):
word = models.CharField("""field args""")
# Views
words = form.cleaned_data['sentance'].split(" ")
for word in words:
_ = Word.objects.create(word=word)

Searching a list of tuples in python

I'm having a database (sqlite) of members of an organisation (less then 200 people). Now I'm trying to write an wx app that will search the database and return some contact information in a wx.grid. The app will have 2 TextCtrls, one for the first name and one for the last name. What I want to do here is make it possible to only write one or a few letters in the textctrls and that will start to return result. So, if I search "John Smith" I write "Jo" in the first TextCtrl and that will return every single John (or any one else having a name starting with those letters). It will not have an "search"-button, instead it will start searching whenever I press a key.
One way to solve this would be to search the database with like " SELECT * FROM contactlistview WHERE forname LIKE 'Jo%' " But that seems like a bad idea (very database heavy to do that for every keystroke?). Instead i thought of use fetchall() on a query like this " SELECT * FROM contactlistview " and then, for every keystroke, search the list of tuples that the query have returned. And that is my problem: Searching a list is not that difficult but how can I search a list of tuples with wildcards?
selected = [t for t in all_data if t[1].startswith('Jo')]
but, measure, don't guess. I think that in some cases, the query would be faster - specially if you have too many records. Maybe you should use a query on the first char, and then start using python-side filter, since you already have the results.
I think that generally, you shouldn't be afraid of giving tasks to a database. It's quite possible that the LIKE clause will be very fast. Sqlite is implemented in fairly robust C code, and will happily deal with queries like this.
If you're worried about sending too many requests, why not send a query once a user has entered a threshold of characters, such as three?
A list comprehension is probably the best way to return the result if you want to do added filtering.
If you are searching for a string matching the start using LIKE, eg 'abc%' (rather than anywhere in the string - '%abc%'), the search should be quite fast if you have an index on the field, as the db can use the index to help find the matches.

Categories

Resources