By default, the ajax behavior in autocomplete queries 10 first objs of the list and by scrolling you would see further next 10 results. But the scrolling doesn't work smoothly so I need to actually see all the result on the list by default. (it's loading 10 objs per scroll)
Is there any option to change that or is there any way to manipulate the query to avoid chunked result?
paginate_by = XXX
taking the same example as per the doc :
class CountryAutocomplete(autocomplete.Select2QuerySetView):
paginate_by = 20
def get_queryset(self):
# Don't forget to filter out results depending on the visitor !
if not self.request.user.is_authenticated():
return Country.objects.none()
qs = Country.objects.all()
if self.q:
qs = qs.filter(name__istartswith=self.q)
return qs
Related
I have two tables in the analysis. I am using the script below to be able to filter table A and pass those filter selections to the matching filter in table B. Table A and B are visualized in a bar chart. I am triggering the code when the value of a document property changes, following instructions here.
I am running into two problems.
1) After the script runs, clicking Reset All Filters results in only table A being displayed in the visualization. Clicking Reset All Filters again fixes the issue.
2)When I add a second filter (commented out in the code below), making a selection in the Type_A or or Type_B filter wipes out the type B data from the visualization. I think the problem is in how IncludeAllValues is being handled, but I don't know how to fix it. Any help will be appreciated.
from Spotfire.Dxp.Application.Filters import *
from Spotfire.Dxp.Application.Visuals import VisualContent
from System import Guid
#Get the active page and filterPanel
page = Application.Document.ActivePageReference
filterPanel = page.FilterPanel
theFilterA = filterPanel.TableGroups[0].GetFilter("Type_A")
lbFilterA = theFilterA.FilterReference.As[ListBoxFilter]()
theFilter2A = filterPanel.TableGroups[1].GetFilter("Type_A")
lb2FilterA = theFilter2A.FilterReference.As[ListBoxFilter]()
lb2FilterA.IncludeAllValues = False
lb2FilterA.SetSelection(lbFilterA.SelectedValues)
#########################Type_B###########################
# theFilterB = filterPanel.TableGroups[0].GetFilter("Type_B")
# lbFilterB = theFilterB.FilterReference.As[ListBoxFilter]()
# theFilter2B = filterPanel.TableGroups[1].GetFilter("Type_B")
# lb2FilterB = theFilter2B.FilterReference.As[ListBoxFilter]()
# lb2FilterB.IncludeAllValues = False
# lb2FilterB.SetSelection(lbFilterB.SelectedValues)
I am hitting an elastic database and I want to return results one by one using scroll(), which requires me to fetch a scroll_id using the search() method.
The result size can range from 0-40000, that's why I can't use search() for the whole thing.
I didn't notice the problem when there were a large number of hits, but when there is only one hit, the scroll doesn't return anything.
query = {"query":{"match":{"message": "Techtotic"}}, "sort": [{"#timestamp":"asc"}]}
scrollid = es.search(index="ts-shanks*", size=2, scroll="50m", body=query)
hits = scrollid["hits"]["total"]
scrollid = scrollid["_scroll_id"]
for i in range(hits):
entry = es.scroll(scroll_id=scrollid, scroll="1m", filter_path=req_data)
try:
print(entry["hits"]["hits"][0]["_source"]["message"])
except Exception:
print(json.dumps(entry, indent=4))
The output displays the matched hit in the print corresponding to the '''search()''' but not in the one with the scroll().
Also, no matter how many hits, we get the last output of the scroll always triggers the except block because it's empty.
I heard that search_type="scan" would fix it, but it seams that it has been deprecated.
Trying to paginate a large queryset so I can return to the same position I was in previously even if data has been added to the database.
Currently I have as my pagination class:
from rest_framework.pagination import CursorPagination
class MessageCursorPagination(CursorPagination):
page_size = 25
ordering = '-date'
In my View I have:
from rest_framework.generics import GenericAPIView
from rest_framework.authentication import TokenAuthentication, BasicAuthentication
class MessageViewSet(GenericAPIView):
permission_classes = (IsAuthenticated, )
authentication_classes = (TokenAuthentication,)
pagination_class = pagination.MessageCursorPagination
serializer_class = serializers.MessageSerializer
def get(self, request, **kwargs):
account_id = kwargs.get('account_id', None)
messages = models.Message.objects.filter(
account=account_id)
paginated_messages = self.paginate_queryset(messages)
results = self.serializer_class(paginated_messages, many=True).data
response = self.get_paginated_response(results)
return response
While testing to see if I'd set it up right, I got the results I was expecting with a next link and a null for the previous link.
After going to the next link I get a new next link, the next set of results, and a previous link.
When I continue to the next link I get the same previous link as before, but with the next, next link and the next set of data.
No matter how many times I go to the next, next link the previous link remains the same.
Why doesn't the previous link update?
-- Update --
It looks like the cause to my issue is that I have a lot of messages on the same date. Ordering by date it tries to step back to the date before the current cursor. How can I order by date but step through the list using the cursor pagination like I would with ids?
From the Documentation
Proper usage of cursor pagination should have an ordering field that satisfies the following:
Should be an unchanging value, such as a timestamp, slug, or other field that is only set once, on creation.
Should be unique, or nearly unique. Millisecond precision timestamps are a good example. This implementation of cursor pagination uses a smart "position plus offset" style that allows it to properly support not-strictly-unique values as the ordering.
Should be a non-nullable value that can be coerced to a string.
This is more of a clarification type question rather than actual problem regarding LinqHelper.CreateQuery method.
So,
This method has 3 overloads. The 2 in question here, are:
1.LinqHelper.CreateQuery<SearchResultItem>(searchContext, searchStringModel)
2.LinqHelper.CreateQuery<SearchResultItem>(searchContext, searchStringModel, startLocationItem) [I haven't used any additional context here so used the default null]
Now,
In order to search for items with in a specific location of the content tree ( for example under a particular folder you have 1000 items) I can use method 1 using the query:
query = "location:{FOLDER_GUID};+custom:my_filed_name|bla_bla"
Which works perfectly.
But (from what I understood from the method signature is that) I should also be able to use method 2 like the following:
SitecoreIndexableItem folderID = SitecoreIndexableItem)contextDatabase.GetItem({FOLDER_GUID});
var index = ContentSearchManager.GetIndex(new SitecoreIndexableItem(Sitecore.Context.Item));
using (var context = index.CreateSearchContext())
{
List<SearchStringModel> searchStringModel = new List<SearchStringModel>();
searchStringModel.Add(new SearchStringModel("my_field_name", "bla_bla"));
List<Sitecore.Data.Items.Item> resultItems = LinqHelper.CreateQuery(context, searchStringModel, folderID).Select(toItem => toItem.GetItem()).ToList();
}
Problem is for the above method (method 2) the searching works fine, what doesn't work is the "startLocationItem" (folderID in this case).
FOR EXAMPLE,
IF in my entire sitecore tree has total 3 items containing "my_filed_name=bla_bla"
BUT, only 1 item contains "my_filed_name=bla_bla" in the Folder ({FOLDER_GUID}, "the perticular folder" in this case)
THEN,
Method 1 returns 1 item (WHICH IS CORRECT)
BUT, Method 2 returns 3 items, despite "startLocationItem = {FOLDER_GUID} ... (WHICH I DONT THINK IS CORRECT)
Question is :
1. What is the exact purpose of "startLocationItem" in Method 1 ?
2. And what's the benefit of using "location" filter or "startLocationItem for method 2" ?
LinqHelper is an internal helper class and should not be used in normal operation. It is to help the Sitecore UI talk to the search back-end. Its syntax could be changed at any time so could potentially break things based on it and it is also not documented.
You would be better to convert your query into a normal Linq query ie
using (var context = index.CreateSearchContext)
{
context.GetQueryable<SearchResultItem>().Where(x =>x.Paths.Contains(ID.Parse("your GUID Here")))
}
The 'location' in the LinqHelper string is equivalent to the Paths (or _path) field stored in the index.
This field contains a list of all the parent items of an item, held as a list of GUIDs.
By filtering by _path you restrict the query to a certain node of the tree without effecting the score, for example:
/home (id:1234)
/animals (id:5678 = path:1234 / 5678
/cats (id:1111) = path: 1234 / 5678 / 1111
/dogs (id:2222) = path: 1234 / 5678 / 2222
/cars (id:4567) = path: 1234 / 4567
/sports (id:3333) = path: 1234 / 4567 / 3333
If you filter on animals (ie 5678) you restrict the search only that item and its children.
Using a filter means you can restrict the context of a search without that part effecting the scoring of the main query, so you would end up with:
using (var context = index.CreateSearchContext)
{
context.GetQueryable<SearchResultItem>().Where(x =>Name.Contains("Exciting"))
.Filter(y => y.Paths.Contains(ID.Parse("your GUID Here")
}
This would search only inside the part of the tree you have filtered by for where name contains 'exciting'.
Hope that helps :)
I'm trying to get a limited set of results in a sub collection
Basically I have this:
user = Model::User.find(token)
playlists = user.playlists
playlists.each do |playlist|
criteria = playlist.tracks.limit(4) #I Want to limit these to return max 4 tracks
#the criteria is perfect here but the decorator still returns all the tracks
#setting this criteria on the user saves the new tracks list limited by 4
#like this:
playlists[index].tracks = criteria
end
decorator = Decorator::PlaylistCollection.new(playlist)
response_with decorator
This isn't working, and my question is how can I limit every playlist to return 4 tracks max
What I want is that the response contains all the playlists with 4 tracks max in it (also the queries on mongo should be optimized)
display all the playlist
every playlist contains max 4 tracks
it's required to prevent the call for all the tracks of the playlist from mongoid/mongo
Thanks!
I got the solution, mongoid creates an array with ids (track_ids) with all the ids of the tracks in the list.
By overriding the track_ids instead of tracks nothing is queried or stored in mongoid.
user = Model::User.find(token)
playlists = user.playlists
playlists.map do |playlist|
playlist.track_ids = playlist.track_ids.take(4)
end
decorator = Decorator::PlaylistCollection.new(playlist)
response_with decorator
I think that the following code would do the trick.
user = Model::User.find(token)
playlists = user.playlists[0..3]
decorator = Decorator::PlaylistCollection.new(playlist)
response_with decorator