Adding location to Querying the Graph API with Python - python-2.7

My query currently looks like the one below... I am trying to narrow my search to a specific location (West Midlands). How can I achieve that.
pp(g.request("search", {'q' : 'student mentors', 'type': 'page', 'location': 'West Midlands'}))

There is no location parameter for query type page. You can add "West Midlands" to the query parameter itself because Facebook searches the location/address, too. So you call would be
pp(g.request("search", {'q' : 'student mentors west midlands', 'type': 'page'}))
However that specific query does not return any results, which means there is no page that includes "student mentors" in it's name and "west midlands" in it's address.

Related

Django form - the same field multiple times

how can I process a form with a field:
order = ModelChoiceField(
required=False,
queryset=OrderOd.objects.filter(Q(status='DN') | Q(status='DI')),
widget=Select(
attrs={
"class": "form-select form-select-md form-select-solid",
"data-control": "select2",
"data-multiple": "true",
"multiple": "multiple",
"data-placeholder": _("Vyberte objednávku ..."),
"id": 'order'
}
)
)
In front-end, I can select multiple orders (looks like pills/tags) and in the request sent to the server it looks like this:
movement: f2b7c234-fbdb-4059-bcb6-8ada46cef72c
account: dbabefb7-f053-4edf-a2e3-787bf6bfc371
date: 2022-09-12
order: eb2fc726-3e97-4af2-a8b2-08f20771cfef
order: 8398925b-fca6-4b25-8c48-e12940a5b5c3
order: bfa35391-5cf8-4ed8-8c44-a797da875cb4
order: 07be93ac-20b3-459c-8038-c8b023db6d66
When I inspect self.data, I got
'order': ['eb2fc726-3e97-4af2-a8b2-08f20771cfef', '8398925b-fca6-4b25-8c48-e12940a5b5c3', 'bfa35391-5cf8-4ed8-8c44-a797da875cb4', '07be93ac-20b3-459c-8038-c8b023db6d66'],
but when I check the output of logger.info(self.data['order']), it gives me only the first UUID.
[INFO] form.py 123: 07be93ac-20b3-459c-8038-c8b023db6d66
What I need is to access all UUIDs in the array (order) and process them instance by instance.
Any idea, how to do it?
Thanks
You can use self.data.getlist('order') to return the data in the array form.
see more info in Django documentation

Mongodb Search Query with Populate and regex

I want regex search on mongodb with NodeJS.
First One is Users and Second one is categories. Here I have saved categories id in Users
collection. I want a search Query that filter my records.
Users collection
{_id:'mongo_id', name:"User First",cat_id:2}
{_id:'mongo_id', name:"User Second",cat_id:2}
{_id:'mongo_id', name:"User Third",cat_id:1}
{_id:'mongo_id', name:"User Fourth",,cat_id:4}
Categories
Suppose id is numeric. For demo purpose I have written numeric values.
{_id:'1', name:"Salesman",}
{_id:'2', name:"Carpanter"}
{_id:'3', name:"Plumber"}
{_id:'4', name:"Engineer"}
I have a text input if I will type Carpanter or Carpan(regex) then I want 2 records, or
When I type User Second or second I want 1 Record
var query = {
'name': {
$regex: req.body.name,
$options: 'i'
}
};
innerQuery = {
path: 'category',
select: 'name'
}
Users.find(query)
.populate(innerQuery)
.sort({
'createdDate': -1
})
Your user record looks like this:
{_id:'mongo_id': name:"User First",cat_id:2}
I'll assume the extra : is a typo.
Aside from that, you are attempting to query for title. There is no path title in that record, so you can't query for it.
You're also trying to populate the path category, but there is no field with that name in the user records. The only related field I see is cat_id.
If you want to, for example, query by name and populate the cat_id, you can do something like this:
var query = {
'name': {
$regex: req.body.title,
$options: 'i'
}
};
var innerQuery = {
path: 'cat_id',
model: 'Category',
select: 'name'
};
This is entirely dependent on whether you named your model Category, and also what your schemas actually look like.
(PS, you're also trying to sort by createdDate, but I don't see that field in any of your records, either).

Can creationTime and other Directory meta-fields be used in a query?

I'm trying to filter the list of users returned from Directory.Users.List, and want to use the creationTime value in the filter. According to this:
Search for users
...you can use Date fields in comparisons, however when I try something like:
creationTime>=2016-06-30 (or the same value quoted)
...I get:
Caused by: com.google.api.client.googleapis.json.GoogleJsonResponseException: 400 Bad Request
{
"code" : 400,
"errors" : [ {
"domain" : "global",
"message" : "Invalid Input: creationTime>=2016-06-30",
"reason" : "invalid"
} ],
"message" : "Invalid Input: creationTime>=2016-06-30"
}
The message isn't particularly helpful - is this a syntax issue with the query, or is it the case that the field isn't available for query (it isn't listed in that article, but it is part of the JSON response)?
The article specifies the format for Date field queries, however this field also includes a time component, so I tried to also replicate the exact format that shows in the JSON, with the same result.
Also, same result in the Try it! section of the Users reference page.
Also also, tried using a numeric value (Date.getTime(), in effect) - same error.
Thanks...
P.S. Interestingly, Google does document how to search on date fields for Chromebooks, and it's entirely different than they imply for users. I still can't use creationTime with this, but it does work for Chromebooks. The other part of this is that it refers to fields that aren't documented, and why I try to use the documented field (e.g. lastSync vs. sync), it fails with the same message. This whole thing seems half-baked.
View Chrome device information
Only the fields listed in the table are searchable via the users.list search.
A workaround (I don't know if this is suitable for you) may be to push the data you want to use in a search into custom user fields. These fields are searchable.

NgramField returning resutls based on substring of the query term

I have a Warehouse Model which is getting index as follows
class WarehouseIndex(SearchIndex, Indexable):
"""
SearchIndex Class that stored indexes for Model Warehouse
"""
text = CharField(document=True, use_template=True)
search_auto = NgramField()
....
def get_model(self):
return WareHouse
In my shell I am running the following sqs query.
>>> sqs = SearchQuerySet().models(WareHouse)
>>> sqs.filter(customers=3).filter(search_auto='pondicherry')
This returns result consisting of results that do not have exact term pondicherry it also provides me some results that match terms like ich, che, ndi, etc.
I have even tried using __exact and Exact but all return the same result?
EDIT: Index mapping, Index Setting
How can I avoid this and provide result only of term pondicherry?
It seems to be related to this open issue
This is because your search_auto ngram field has the same index and search analyzer and hence your search term pondicherry also gets ngramed at search time. The only way to fix this is to set a different search_analyzer for your search_auto field, standard would be a good fit.
You can change your search_auto field mapping with this:
curl -XPUT localhost:9200/haystack/_mapping/modelresult -d '{
"properties": {
"search_auto": {
"type": "string",
"analyzer": "ngram_analyzer",
"search_analyzer": "standard"
}
}
}'
As #Val has stated in the above answer, the error was because search_analyzer and indexed_analyzer are same which caused the issue,
As we all know haystack is very inflexible in setting up the basic elasticsearch configuration, I installed elasticstack and in my setting.py changed the backend to it's elasticsearch_backend as suggest and additionally added the following 2 configurations
# elasticslack setting
ELASTICSEARCH_DEFAULT_ANALYZER = 'snowball'
ELASTICSEARCH_DEFAULT_NGRAM_SEARCH_ANALYZER = 'standard'
this seemed to solve my problem.

How do I select records where its related records do not have a specific attribute-value?

I want to select all of the users who do not have associated posts where the title is, 'test123'. The following syntax is not working:
User.includes(:posts).where.not(posts: { title: 'test123' })
How do I select users whose associated posts do not have a specific title?
UPDATE
Originally, I tried to isolate where exactly I thought I was having the problem, but I want to show the query that most closely reflects what I am doing. The problem is still with the, where.not, clause. It's returning an empty array when I know there are records with posts that have other titles.
User.where("users.created_at >= ? and users.created_at <= ?", 1.month.ago.utc, 1.week.ago.utc)
.where(active: true)
.includes(:comments)
.where('comments.id is not null')
.includes(:posts)
.where.not(posts: { title: 'test123'} )
.references(:posts)
From the API Doc
If you want to add conditions to your included models you’ll have to
explicitly reference them
So the below query will work
User.includes(:posts).where.not(posts: { title: 'test123' }).references(:posts)
I believe the problem was with executing inner vs outer joins with Active Record Query Syntax. The problem with this query:
User.includes(:posts).where.not(posts: { title: 'test123' })
is that it evaluates, where.not(posts: { title: 'test123'}), only after retrieving all the user records with an associated post -- leaving behind all of the other user records who do not have a corresponding post. I wanted a query that would not exclude the Users without posts.
This is how I did it:
User.where("users.id NOT IN (SELECT DISTINCT(user_id) from posts where title = 'test123')")