Include or Exclude content from Search

Topics: Administration, General
Jul 8, 2011 at 9:07 PM

Howdy Orcharders,

I have a question (and please expect more as Im getting more and more impressed with this as I work with it) about using search.

I am building a support site for our support team to use. The site will start off relatively small with some random product content and lame FAQs... but one part of this will
be a Knowledge Base. I want the search mechanism to only search the Knowledge Base content but I am not seeing an obvious way to do this (at least nothing obvious to me).

What would be the technique I use to explicitly include or exclude content from being searched?

Also, If they wanted me to add another form that searches product content that doesnt include the KB content, is it possible to set this up?

Any info you have would be very helpful.

THanks!

> Brad Thornhill
   UI Developer/Graphic Artist

Coordinator
Jul 8, 2011 at 10:41 PM

There are two solutions:
- Use the current default search "index", and create a specific controller which will use SearchBuilder to apply a filter on the search query, like WithField("ContentType", "FAQ")
- Create specific indexes, and handle the indexing so that every piece of information goes to the desired index

The easier method right now is to do the first one. Then you can provide an extended Search controller like the current one, which would take some advanced filtering, by default, or user provided.

I wrote a blog post about Indexing a few months ago: http://www.dotnetguru2.org/sebastienros/index.php/2011/02/17/orchard-indexing-1

Jul 11, 2011 at 3:04 PM

Thank you for the pointer... Ill pass this to my developer partner so that he can implement this.

I appreciate your time.

Sep 9, 2011 at 3:31 PM

Hi Sebastien, read your blog post and can't find the second post you promised ;-)

My problem is, I can't get my head around searching for my own ContentPart's properties... I imagined it to be as simple as calling

 

searchBuilder.WithField("DogsNotAllowed", true)

 

but it won't give me any results. What I've done so far:

  1. Added indexing to my PartHandler on the field I want to search for like this:
    public class MyPartHandler : ContentHandler
    {
    	public MyPartHandler(IRepository<MyPartRecord> repository)
    	{
    		Filters.Add(StorageFilter.For(repository));
    
    		OnIndexing<MyPart>(
    			(context, part) =>
    			context.DocumentIndex
    				.Add("type", part.ContentItem.ContentType).Analyze().Store()
    				.Add("DogsNotAllowed", part.DogsNotAllowed == true).Store()
    			);
    	}
    }
    
  2. In Orchard.Search.SearchService added the WithField call to the search like this (only for testing purposes:
    IPageOfItems<T> ISearchService.Query<T>(string query, int page, int? pageSize, bool filterCulture, string[] searchFields, Func<ISearchHit, T> shapeResult) {
    
        [...]
    
        var searchBuilder = Search().Parse(searchFields, query).WithField("DogsNotAllowed", true);
        [...]
    }
    
  3. I went to the Search setting at http://discoverize/Admin/Settings/Search and checked the box for the new field DogsNotAllowed.
  4. I saved a ContentItem with the DogsNotAllowed property set to true.
  5. I searched ... and got zero result. (I expected to always get that ContentItem back after my modification to SearchService.)

Any idea what I'm missing here?

Coordinator
Sep 12, 2011 at 8:28 PM

This might be due to a bug in the search with booleans. I have fixed it for anothe project, and will include the solution in the 1.x  branch. In the meantime I suggest you index a 1/0 integer value instead, and search for 1 or 0. This is how booleans will be done with the fix.

Sep 12, 2011 at 10:02 PM
sebastienros wrote:

This might be due to a bug in the search with booleans. I have fixed it for anothe project, and will include the solution in the 1.x  branch. In the meantime I suggest you index a 1/0 integer value instead, and search for 1 or 0. This is how booleans will be done with the fix.

Thanks for answering, Sebastien! After quite a lot of investigation I also found out that it's a bug and filed it here: http://orchard.codeplex.com/workitem/18120.

My solution was to use "true" and "false" directly in the handler, but using 1 and 0 is an even better idea because it prevents from all spelling mistakes in the future :-)

Mar 8, 2014 at 12:13 AM
I know this is an older question, but just for reference, in later versions of Orchard this is quite easy to do, since you can now limit searching by content type.

For example, I needed to have some pages that were only accessible via direct links from an email (not publicly listed in the site map, website links or searching).

In this case I simply created a new content type called "Hidden Page" that was a duplication of the Page content type and in the definition for this content type ensured that the option to index this type in searches is disabled.
Mar 27, 2014 at 7:45 PM
sebastienros wrote:
I wrote a blog post about Indexing a few months ago: http://www.dotnetguru2.org/sebastienros/index.php/2011/02/17/orchard-indexing-1
That link is no longer valid.
Coordinator
Mar 27, 2014 at 8:53 PM