query = {
"is_deleted" : False,
"status_value" : "PROSES"
}
if keyword != "":
query["$text"] = { "$search" : keyword }
db.db_konten.find(query)
I have a pymongo query using the text search of MongoDB. It works fine. But I found out that it cant searh the word very ? Is there a reason why. Im using python3 and Flask
When your text index is set to English, it will exclude certain stop words, including very.
If you're interested, the full list is in the MongoDB source code.
You can also override it by setting the $language parameter when creating the text index.
Related
I can't apply a custom analyzer when using query match with elasticsearch-py.
I customized an analyzer called custom_lowercase_stemmed and used es.indices.put_settings to update the index setting.
However, it couldn't find the analyzer when I do a search.
I also looked into the parameter analyzer in es.search, but it returns an error:
..unrecognized parameter: [analyzer]
Can I get any suggestions here in terms of a customized analyzer? Thank you!
query_body = {
"query": {
"match": {
"employer":{
"query": txt,
"fuzziness": 'AUTO',
"analyzer" : 'custom_lowercase_stemmed'
}
}
}
}
es.search(index='hello',body=query_body)
Here is the full error:
RequestError: RequestError(400, 'search_phase_execution_exception', '[match] analyzer [custom_lowercase_stemmed] not found')
I think you have to make sure you have the following:
Have your Configuration set properly. In your case, you should have in your settings the field "custom_lowercase_stemmed" as analyzer. You can also define the words you want to stop.
With the Python ES Client, you have to send the analyzer as a parameter of the method .search(). Check the docs. However, you can try to run your query as it is. I haven't played that much with analyzer.
Hope this is helpful! :D
Ensure that you have specified your analyzer at your mapping and ensure your querying the correct field as well.
For your question on the matching problem for removing duplicate names, at term level and short words, fuzziness and wildcard parameters would be the best fit!
Cheers,
Min Han (:
I am looking to implement full text search in my python application using pymongo. I have been looking at this question but for some reason I am unable to implement this in my project as I am getting an error no such cmd: text. Can anyone direct me on what I am doing wrong?
Here is my code:
db = client.test
collection = db.videos
def search_for_videos(self, search_text)
self.db.command("text", "videos",
search=search_text,
limit=10)
The collection I am trying to search is called videos however I am not sure if I am putting this in the correct parameter, and I also am not sure if I need the line project={"name": 1, "_id": 0}.
The documentation here I believe is using the mongo shell to execute commands, however I wish to perform this action in my code.
I have looked at using the db.videos.find() function, but cannot seem to implement it correctly either.
How to I use PyMongo Full Text Search from my Python Code?
First be sure that you have a text index created on the field as mentioned here or you can just do it with pymongo too :
collection.create_index([('your field', 'text')])
Using pymongo you can do this to search:
collection.find({"$text": {"$search": your search}})
your function should look like this:
def search_for_videos(search_text):
collection.find({"$text": {"$search": search_text}}).limit(10)
I hope this helps you.
First create a text index based on the field you want to do the search on.
from pymongo import TEXT
db = MongoClient('localhost',port = 27017).DBNAME
db.collection.create_index([('FIELD_NAME',TEXT)],default_language ="english")
once you create the text index use the following query to search text. Depending on the size of your database, it might take long to create the text index.
db.collection.find({"$text": {"$search": your search}})
I'm trying to add a search option to my website but it doesn't work. I looked up solutions but they all refer to using an actual string, whereas in my case I'm using a variable, and I can't make those solutions work. Here is my code:
cursor = source.find({'title': search_term}).limit(25)
for document in cursor:
result_list.append(document)
Unfortunately this only gives back results which match the search_term variable's value exactly. I want it to give back any results where the title contains the search term - regardless what other strings it contains. How can I do it if I want to pass a variable to it, and not an actual string? Thanks.
You can use $regex to do contains searches.
cursor = collection.find({'field': {'$regex':'regular expression'}})
And to make it case insensitive:
cursor = collection.find({'field': {'$regex':'regular expression', '$options':'i'}})
Please try cursor = source.find({'title': {'$regex':search_term}}).limit(25)
$text
You can perform a text search using $text & $search. You first need to set a text index, then use it:
$ db.docs.createIndex( { title: "text" } )
$ db.docs.find( { $text: { $search: "search_term" } } )
$regex
You may also use $regex, as answered here: https://stackoverflow.com/a/10616781/641627
$ db.users.findOne({"username" : {$regex : ".*son.*"}});
Both solutions compared
Full Text Search vs. Regular Expressions
... The regular expression search takes longer for queries with just a
few results while the full text search gets faster and is clearly
superior in those cases.
I think this is a fairly basic question, but I can't figure this part out. So, basically, in my code, I have isolated a document, like so:
document = collection.find_one(name)
And now that I have this document, I want to know how to print out a certain key for this specific document. So, basically, this document looks like this right now:
{
"_id" : ObjectID("...")
"name": ABCD
"info": {
"description" : "XYZ"
"type" : "QPR"
}}
And I want to know how I can extract and print "XYZ" using the variable "document" that has the entire document in it.
document is a regular Python dictionary:
print(document["info"]["description"])
This, as well as, a lot of relevant PyMongo basic usage information is covered in the PyMongo Tutorial - make sure to study it.
It is just a dictionary
print(document['info']['description'])
or if you are not sure if your document contains the right keys
info = document.get('info', None)
if info:
print(document.get('description', 'No description'))
Hi I am experimenting with python and mongodb with tornado framework. I am having entry module where user can insert the data of students in academic and sports field. In mongodb terminal I did search with
db.student.find( { $or: [ { "academy": name }, { "sports": name } ] } )
but when I try to do the same with python along with MOTOR driver I end up with error.
My python command is
doc = yield db.student.find_one({ $or: [{"academy": name}, {"sports": name}]})
Can anyone guide me how I can do the search with or condition in python motor?
The or condition is used to check whether the data of particular student is entered in both the fields or not.
You write, "I end up with an error", but it is very difficult for anyone to answer your question if you don't tell us what the error is!
In this particular case I think I know the problem. In Python, all field names must be quoted. The proper syntax is:
doc = yield db.student.find_one({ "$or": [{"academy": name}, {"sports": name}]})