How to filter out file types


How can I, in the search, easily filter out files that I do not want to get

I would like to exclude all jpg, gif, png files for example

Is it possible to add a .Filter() or something here?


Oct 02, 2012 12:21

Hi Fredrik,

Yes you can. This is an example of how to filter away files by there extension

var result = client.Search<UnifiedFile>()
.Filter(x => !x.Extension.Match(".gif"))


If you never want to search for them, then best thing would to remove them at indexing time so they never enter the index. This could be done by setting up a convention for them.

FileIndexer.Instance.Conventions.ForInstancesOf<UnifiedFile>().ShouldIndex(x => !x.Extension.Equals(".gif"));


Hope that helps you.

Oct 02, 2012 13:02

Thanks Marcus, works fine!

Oct 02, 2012 13:38

Hi Fredrik, 

Just out of curiousity, is there any particular reason you're doing Take(300)? In general you should only Take(Number_of_hits_that_should_be_displayed) and then do paging using using Skip(). That is,

var pageSize = 10;
var pageNum = //From query string or whatever


That way you won't be sending more data than needed over the wire and users will get snappier results. Then of course, sometimes there are exceptions.. :)

Oct 02, 2012 20:34

Hi Joel,

Well that was when I was much younger and before I knew how to filter out the bad extensions (gif, jpg etc) from the resultset directly in the request (as Marcus pointed out in this thread). So I did the filtering on my end, bad performance of course, and just a nasty workaround.

Now I know better, and I surely will only fetch the items I actually need.

But there could be exceptions... ;-)


Oct 03, 2012 8:55
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* 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.