I am using Django 1.8 and currently am working on a Blog application. When i search for tweets( just a name instead of posts) , i want to save the search results obtained after querying the database, as text instead of list. My view function is as below:
def search(request):
query = request.GET.get('q','')
if query:
qset = (
Q(text__icontains=query)
#Q(hashes__icontains=query)
#Q(artist__icontains=query)
)
results = Tweet.objects.filter(qset).distinct()
else:
results = []
number_of_results = len(results)
search_item = query
returned_items = []
for res in results:
text = res.text
returned_items.append(text)
returns = returned_items[:]
search = Search(search_item=search_item,returns=returns)
search.save()
context = {'query':query,'results':results,'number_of_results':number_of_results,'title':'Search results for '+request.GET.get('q','')}
return render_to_response("tweets/search.html",context,context_instance=RequestContext(request))
also, the snapshot of my search table in the database is as shown below:
Please help me out friends.
you should join the returned list using the comma separted values. This will return the string.
returns = ', '.join(returned_items)
This piece of code is setting returns to a list:
returns = returned_items[:]
If you want to access the first string, set it to returned_items[0]. If you want to join all strings in the list, use join()
returns = "".join(returned_items)
Related
I want to search each word in a sentence and put that in result['post'] dictionary
and of course this code only looks for last query
queries = querystring.split()
for query in queries:
results['posts'] = Post.objects.filter(text__icontains=query)
i tried append,extend and a lot of things but it didn't work as expected.
Edit Update:
I also tried
count = {}
results = {}
post_results = []
queries = querystring.split()
for query in queries:
post_results.append(Post.objects.filter(text__icontains=query))
results['posts'] = post_results
count['posts'] = results['posts'].count()
But this leads to another error count() takes exactly one argument (0 given)
Error Image
Also Post.objects.filter(text__icontains=query) this line returns a queryset value and Queryset type of django doesn't seem to have append. Is there it's own append like feature ? If so then that wouldn't change it's queryset type to list. Using temp variable like post_results = [] changes to list.
Can we change that list back to queryset? If we can then that would work.
I guess the reason is results['posts'] value is overwritten per every for loop..
How about changing code like
queries = querystring.split()
tmp = []
for query in queries:
tmp.append(Post.objects.filter(text__icontains=query))
results['posts'] = tmp
How about this:
results = {'posts': []}
count = {'posts': []}
for query in querystring.split():
qs = Post.objects.filter(text__icontains=query)
results['posts'].append(qs)
counts['posts'].append(qs.count())
Update after you answered your own question:
Oh, I was under the assumption you wanted a list of queries of each word with their respective result counts. If you only wanted to combine each string of the search term into an OR query you could use Q objects.
results = {}
count = {'posts': []}
from django import models
qobjects = models.Q()
for query in querystring.split():
qobjects |= models.Q( ('text__icontains',query) )
results['posts'] = Post.objects.filter(qobjects)
counts['posts'] = results['posts'].count()
instead of making multiple queries we can use Q object, here is the simple example
q = 'query string'
keywords = []
prevword = ""
for word in q.split(' '):
prevword = prevword + word + " "
keywords.append(prevword.lower())
query = Q(title__icontains=q)
for keyword in keywords:
query.add(Q(title__icontains=keyword), Q.OR)
services = Service.objects.distinct().filter(query).all()
Thanks all of you I tried everything mentioned at first and finally got a solution
I did a crazy thing at first.
I wanted to define an empty queryset so for results['posts'] so I did a bad but a working way results['posts']=Post.objects.filter(text_icontains='sth_that_will_never_b_true')
and I used
Django Merge QuerySets
and solved the problem and then later studied genuine way to define empty QuerySet as
django.db.models.query.QuerySet.none
And gladly final code is
count = {}
results = {}
results['posts']=Post.objects.none() # empty QuerySet
queries = querystring.split()
for query in queries:
results['posts'] = results['posts'] | Post.objects.filter(
text__icontains=query)
count['posts'] = results['posts'].count()
Now everything works as expected.
I'm using pandas as pd and python 2.7
I have a bunch of queries and each query is returning serial numbers. Then I'm using that serial number in a bunch more queries. I want to append all the results to a list. When I append to an empty list, it just returns nothing. I don't know what I'm doing wrong.
for query in list_of_queries:
results = pd.read_sql_query(query,connection,index_col=None)
for serial_number in results['SerialNumbers']:
a = []
new_query = """ SELECT * FROM blah b where b.SerialNumber = '{}' """
new_query = new_query.format(serial_number)
results = pd.read_sql_query(new_query,connection,index_col = None)
a.append(results)
You are resetting the list to be empty at the beginning of each for loop. This should be:
a = []
for serial_number in results['SerialNumbers']:
new_query = """ SELECT * FROM blah b where b.SerialNumber = '{}' """
new_query = new_query.format(serial_number)
results = pd.read_sql_query(new_query,connection,index_col = None)
a.append(results)
# 'a' will now have all the results
Furthermore, it looks like you might be clobbering results because you use it as a variable name twice (once in each loop). I would suggest changing that too!
I am trying to perform a query which returns back a document each time. The problem is that some docs have multiple instances in the database. So instead of getting one doc with a query I am getting multiple results. Thus I am trying to use find_one method which return the first query match. However, changing from find to find_one method I am facing a new problem. My code is the following:
lines = [line.rstrip() for line in open('ids.txt')]
list_names = []
names= open('name.txt', 'w')
for x in range(0,3000):
id = int(lines[x])
print x ,' ',lines[x]
for cursor in collection.find_one({"_id.uid": id}):
name = cursor['screenname']
print name
list_names.append(name)
names.write("%s\n" % name)
names.close()
I have a list of ids and I want to return the correspondant names from mongoDb. However, I am getting `name = cursor['screenname']
TypeError: string indices must be integers
What am I doing wrong here?
The find_one method does not return a cursor. It returns the document itself.
session = self.sessions.find_one({'_id': session_id})
print session # must print your document
I'm using PyES to use ElasticSearch in Python.
Typically, I build my queries in the following format:
# Create connection to server.
conn = ES('127.0.0.1:9200')
# Create a filter to select documents with 'stuff' in the title.
myFilter = TermFilter("title", "stuff")
# Create query.
q = FilteredQuery(MatchAllQuery(), myFilter).search()
# Execute the query.
results = conn.search(query=q, indices=['my-index'])
print type(results)
# > <class 'pyes.es.ResultSet'>
And this works perfectly. My problem begins when the query returns a large list of documents.
Converting the results to a list of dictionaries is computationally demanding, so I'm trying to return the query results already in a dictionary. I came across with this documentation:
http://pyes.readthedocs.org/en/latest/faq.html#id3
http://pyes.readthedocs.org/en/latest/references/pyes.es.html#pyes.es.ResultSet
https://github.com/aparo/pyes/blob/master/pyes/es.py (line 1304)
But I can't figure out what exactly I'm supposed to do.
Based on the previous links, I've tried this:
from pyes import *
from pyes.query import *
from pyes.es import ResultSet
from pyes.connection import connect
# Create connection to server.
c = connect(servers=['127.0.0.1:9200'])
# Create a filter to select documents with 'stuff' in the title.
myFilter = TermFilter("title", "stuff")
# Create query / Search object.
q = FilteredQuery(MatchAllQuery(), myFilter).search()
# (How to) create the model ?
mymodel = lambda x, y: y
# Execute the query.
# class pyes.es.ResultSet(connection, search, indices=None, doc_types=None,
# query_params=None, auto_fix_keys=False, auto_clean_highlight=False, model=None)
resSet = ResultSet(connection=c, search=q, indices=['my-index'], model=mymodel)
# > resSet = ResultSet(connection=c, search=q, indices=['my-index'], model=mymodel)
# > TypeError: __init__() got an unexpected keyword argument 'search'
Anyone was able to get a dict from the ResultSet?
Any good sugestion to efficiently convert the ResultSet to a (list of) dictionary will be appreciated too.
I tried too many ways directly to cast ResultSet into dict but got nothing. The best way I recently use is appending ResultSet items into another list or dict. ResultSet covers every single item in itself as a dict.
Here is how I use:
#create a response dictionary
response = {"status_code": 200, "message": "Successful", "content": []}
#set restul set to content of response
response["content"] = [result for result in resultset]
#return a json object
return json.dumps(response)
Its not that complicated: just iterate over the result set. For example with a for loop:
for item in results:
print item
Is there a way of getting all the unique keyword index i.e. Subject in Plone by querying the catalog?
I have been using this as a guide but not yet successful.
This is what I have so far
def search_content_by_keywords(self):
"""
Attempting to search the catalog
"""
catalog = self.context.portal_catalog
query = {}
query['Subject'] = 'Someval'
results = catalog.searchResults(query)
return results
Instead of passing the keyword, I want to fetch all the keywords
catalog = self.context.portal_catalog
my_keys = catalog.uniqueValuesFor('Subject')
reference: http://docs.plone.org/develop/plone/searching_and_indexing/query.html#unique-values