Django-haystack: how do I select which index to use in a SearchQuerySet? -
I'm looking through Haystack documentation, but I do not understand exactly how I can use it .
The main model in this example is Here is my search in code> Views.py : How to set a different view of a SearchQuerySet from a specific view Can i do Histacks (and other auto-generated) documentation is not a good example of clarity and is a phone call The book is equally exciting to read. I think the section you specify on "Multiple index" is about access to various backend search engines (e.g., Josh, Sollter, etc.) for the query exactly. But your question is "Searchindex" for different models I think you have to see that in your example, you have a search query for "proposal" and Want to have one more question for "Proposal" + "comments". That's how to filter queries returned by search. A method is called that you can facilitate a model class or list of class classes to limit query results. For example, in your search view, you are called a query string parameter, "content" which specifies that the search is for "proposal" or "everything" (proposal and comments) or not. The need to supply additional content parameters to your frontend (or you can use different views for different searches) while calling for viewing. Your query needs to look something like strings: and content query string parameters to get the model (s) to filter your search If you have a lot of search indexes (i.e. many models Lots of content), you probably do not want to trudge the model into your view, but using the django.db from the get_model function to bring the model class dynamically Please. proposal . I have to index two searches that return a list of proposals: the one who searches only for the proposals themselves, and one who searches in proposals with his comments, I set
search_indexes.py Like this:
class ProposalIndexBase (indexes.SearchIndex, indexes.Indexable) title = indexes.CharField (model_attr = "title", boost = 1.1) text = indexes.NgramField (document = true) , Use_template = true) date = indexes.DateTimeField (model_attr = 'createdAt') def get_model (self): Return proposal class ProposalIndex (ProposalI NdexBase): Comment = indexes.MultiValueField () Def prepare_comments (self, object): comment [edit] comment for the comment in object.comments.all ()] Class SimilarProposalIndex (ProposalIndexBase): passed
DIF search (request): if request.method == "GET": if in "q" The request is made. GET: query = str (Request GET.get ("q")) Results = SearchQuerySet (). all (). Filter (content = query) return render (request, "search / search.html", {"result": result})
/ search /? Q = python & amp; Content = proposal / search /? Q = python and content = everything
# Import your model squares so that you can use them in your search view. I'm just guessing what these are in your project) from the proposals. Import proposals from models comments. Modify the import DEF Search (request): if request.method == "GET": If in the request "q" GET: query = str (request.GET.get ("q")) parse query parameter "content" To add additional code here ... # content_type = get content type for request.GET.get (search) Specify the model or models in the list for the "model" call search_model = [] if content_type "proposal" is: search_models = [proposal] elif content_type "everything": search_models = [proposal, Comment] # * Add a "model" to narrow the search results for a particular model. Call Results = SearchQuerySet () all (). Filter (content = query). Models (* search_models) submit returns (request, "search / search.html", {"result": result})
Comments
Post a Comment