how to use script_fields with django-elasticsearch-dsl - django

I'm trying to add a scripted field to my query from django-elasticsearch-dsl
I've tried to add a test field like this:
result = ModelDocument.search().script_fields(test="doc['field_name'].value + 10")
for hit in result:
print(hit.to_dict())
but it returns 10 empty dictionary
and if I try it without adding "script_fields":
result = ModelDocument.search()
for hit in result:
print(hit.to_dict())
it returns 10 dictionary filled with my model instance data.
can you please tell me what is the problem and how can i fix it?

Related

django how to use variable in query filter

I'm trying to create a Django filter using a variable as filter input for a contains query on a jsonfield. I just cannot get it to work. Form what I'm finding the solution should be using kwargs but I'm lost how to implement this.
lookupValue = "[{{\"displayName\":\"{0}\"}}]".format(user['name'])
print("value is: ")
print(lookupValue)
recent_user_calls = CallRecord.objects.filter(participants__contains = [{"displayName":"Some Name"}])
#recent_user_calls = CallRecord.objects.filter(participants__contains = lookupValue)
I create the value I want to search and put it in lookup value. In the last commented out line I try to use this value in de query. This does not work as where the previous line works perfectly. A few lines before I print the lookupvalue and it is 100% the same as the fully typed out version. I'm have been staring at this for 2 days. Can someone please point me in the right direction?
The problem is that the variable you are passing in the query is a string and not a list.
You can write:
recent_user_calls = CallRecord.objects.filter(participants__contains = [{"displayName":user['name']}])
Or you can directly filter using the key:
recent_user_calls = CallRecord.objects.filter(participants__displayName=user['name'])

Add extra field and value to django query set

Hi I am doing a query like
user_list = Myuser.objects.filter(status=active)
now I want to add a new filed matching_percentage to a user who query it , Like want to show how much it matches to your profile. Now I searched annotate function but till now I found that you cant assign a custom calculated value to new filed .
So Is there any way to assign to a filed int values to show how much it matches my profile on run time by a algorithm ?
Update
I am trying something like this
query=MyUser.objects.annotate(annotated_field=Value(MyFunction(F('id')), output_field=IntegerField()))\
.filter(id__in=ids)
F('id') is not converting to ID just passing in function as a string

How do you correctly return an aggregate data field using AX 2012 Query Service

I have been working on the AX Query Service as of late. I have a pretty good understanding of everything but it seems that the QueryDataFieldMetadata object does not like aggregates. When I build a QueryDataFieldMetadata object:
QueryDataFieldMetadata field = new QueryDataFieldMetadata();
field.TableName = "InventSum";
field.FieldName = "AvailPhysical";
field.SelectionField = SelectionField.Database;
And add it to the data source everything is fine. But when I do this:
QueryDataFieldMetadata field = new QueryDataFieldMetadata();
field.TableName = "InventSum";
field.FieldName = "AvailPhysical";
field.SelectionField = SelectionField.Sum;
And add it to the data source the field is not returned at all in the results set. I have checked the datasource itself before executing the query and it is in the fields list but nothing is returned. Does anyone know why this might be happening? Any help would be appreciated.
I just figured this one out. The problem was due to me selecting another field from the table but forgetting to put it in the "Group by" fields. It is strange to me that the query service was returning THAT field with an empty but not returning the aggregate fields at all. Basically I had made a query service query that would be equal to this:
Select wMSLocationId, SUM(AvailPhysical), RecId from InventSum group by ItemId, InventLocationId, wMSlocationId where ItemId == 'some value';
The query was returning:
InventSum.wMSLocationId = 001
InventSum.RecId = 0
The inclusion of the RecId was a mistake, I had forgotten to remove it, but didn't think it would matter as it wasn't in the group by fields and would therefore return null. Removing this selection field did result in the aggregate field returning in the query.
Anyway I hope this helps someone out there as it took me some time to figure out.

How can I get the first item out of a session list in django?

For example,
I set request.session['jump'] = [u'howhigh',u'justlow'] ( i did this by getting values from a form)
How can I get just the first value? I thought something like jump = request.session['jump'][0] would work, but its ont doing it for me.
Also how do i exclude the first value and return all but the one i excluded?

how to match a field name with another field name

I have two fields that run throughout a website that I would like to match so that when a user inputs a value either of the fields, it will match the other field. I'm using Sitecore Rocks and am trying to use a query to do this.
select ##h1#, ##Title#
from /sitecore/Content/Home//*[##h1# !="##Title#"];
update set ##h1# = ##Title# from /sitecore/Content/Home//*[##Title# = "<id>"];
What am I missing here?
This article talks about tapping in to the item:saving event which allows you to compare the fields values of the item before and after the changes:
http://www.sitecore.net/Community/Technical-Blogs/John-West-Sitecore-Blog/Posts/2010/11/Intercepting-Item-Updates-with-Sitecore.aspx
Using this, you can determine which field has been amended, then change the other to match.
I've had to do something similar to this when a new field was added, and we wanted to set the initial value equal to an existing field. It may be a bug in Sitecore Rocks, but I found it would only update a field when a static value was part of the query.
When I ran ##h1# = ##Title#, the query analyzer would return the correct number of items updated, but no values were actually updated. However, ##h1# = '<id>' worked perfectly. After trying a number of things, I found this did what I wanted.
update set ##h1# = '' + ##Title# from /sitecore/Content/Home//*[##Title# = "<id>"];
I hope that helps.