Don't miss out Virtual Happy Hour this Friday (April 26).

Try our conversational search powered by Generative AI!

Search text and property criteria together

Vote:
 

 

I want to search on a free text field within a certain filtered criteria.

I already have code which filters content using FindPagesWithCriteria() and a PropertyCriteriaCollection for filtering by date and category against a parent PageReference. I also have code which uses SearchDataSource() to perform a free text search against a parent PageReference.

Essentially I want to combine the functionality of these two searches. e.g. I want to search for the word 'green' in category = A.

I see it is possible to manually filter a List<PageData> if listPages and remove the ones you don't want i.e.

                PageData pageData = (PageData)listingItems[pageIndex];

                var categories = new List();
                foreach (var c in CurrentPage.Category)
                    categories.Add(Category.Find(c));

                bool keep = false;
                foreach (var c in categories)
                {
                    if (c.Name == "G")
                    {
                        keep = true;
                    }
                }
                if (!keep)
                    listingItems.RemoveAt(pageIndex);

But is there a more elegant way where I can reuse the PropertyCriteriaCollection I have already created and apply it to my SearchDataSource() results?

Thanks

 

#36122
Jan 14, 2010 18:23
Vote:
 

If you do a combined freetext (indexed) search and property search using a SearchDataSource, the property criteria will be used as a post-filter (i.e. the query will not run in the DBMS as with FindPagesWithCriteria, the first step (free text index search) will however of course be handled by the DBMS). If you already have a PropertyCriteriaCollection I'm sure there's a way to add it to the SearchDataSource from codebehind. I know you should be able to loop through your PropertyCriteriaCollection and create a PropertyCriteriaControl for each PropertyCriteria, I think there's a constructor accepting the PropertyCriteria. Not super-elegant, but working.

#36125
Jan 14, 2010 23:34
Vote:
 

Thanks for your reply I could extract each PropertyCriteria from the Collection and add them to the Control and it works fine

 

                var search = new SearchDataSource { SearchFiles = false };
                search.PageTypeID = SearchPageTypeID;
                search.LanguageBranches = GetSearchLocales();
                search.IncludeRootPage = false;
                search.PageLink = rootPage;
                search.SearchQuery = searchPhrase;

                PropertyCriteriaControl prpCriteriaControl;
                foreach (PropertyCriteria prpCriteria in prpCriteriaCollection)
                {
                    prpCriteriaControl = new PropertyCriteriaControl(prpCriteria);
                    search.Criteria.Add(prpCriteriaControl);
                }

#36141
Jan 15, 2010 17:51
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.