Advanced search for specific content-type and specific fields on that type

Topics: Customizing Orchard, Writing modules
Aug 14, 2011 at 9:14 AM

I'm working on an advanced-search module, which will give the user a content-part to be attached to any content-type, then some settings to select which fields of that type will be rendered in inputs to be searched. The content-type has this part could be searched by going to {content-type}/search, that page will have the fields selected rendered in many ways, some of them will be rendered to be searched directly for specific entered data, but others will have multi-input for a range of search, i.e. age between x1 and x2, or age >= x1 .. etc...

I managed to build the direct search fields, but cannot figure out how to narrow the search for only a specific type and specific fields, especially range fields. I need something like: WHERE [ContentType] = 'Article' AND [PublishDate] BETWEEN (D1 AND D2) AND [Views] >= N1 AND [Pages] <= N2 AND ([Body] CONTAINS 'T1' OR [Title] CONTAINS 'T1'). of course all of that will be fully dynamically for render and query build.

Coordinator
Aug 14, 2011 at 5:59 PM

You should look at ISearchBuilder first. You can do everything with it. You just need to know which fields are available.

Aug 16, 2011 at 1:42 PM

Thanks dear Sebastien, I figured out how to use ISearchBuilder by specifying fields with values or ranges, but still can't find where to specify a specific content-type?

Waiting answer very urgently.

Aug 16, 2011 at 6:27 PM

Well actually i think you can't with a single indexset. You can filter the result by ContentType though. I think you can do this by

foreach (searchresult){

var content = get(searchedcontentId)

if(content.type == "contenttypename")

{

        Do something.....

}

}

Not exact code but you get the point.

Coordinator
Aug 16, 2011 at 7:02 PM

If your content type have the Common part, then this code is ran for any content item:

.Add("type", commonPart.ContentItem.ContentType).Analyze().Store()
.Add("author", commonPart.Owner.UserName).Analyze().Store()
.Add("created", commonPart.CreatedUtc ?? _clock.UtcNow).Store()
.Add("published", commonPart.PublishedUtc ?? _clock.UtcNow).Store()
.Add("modified", commonPart.ModifiedUtc ?? _clock.UtcNow).Store()

Which makes the content type available in the index, as the "type" field.

Aug 17, 2011 at 9:08 AM

@sebastienros you right! i used : GetString("type") == "TypeName"

Aug 17, 2011 at 10:07 AM

Thanks Sebastien, I will try your code and hope to build it correctly and work. :)

Aug 17, 2011 at 10:34 AM

Another question, in the Settings > Search, I can see some checkboxes for fields. What I can do with these? I un-checked some fields, but still can filter search by them. So what the purpose of these checks?

Aug 17, 2011 at 10:37 AM

Sorry, I rebuilt the search index, then the un-checked fields can't be used any more in search filtration. Is that correct?

Aug 17, 2011 at 11:52 AM

Thanks guys, specially the great Sebastien. It works !!!!!

Coordinator
Aug 17, 2011 at 7:20 PM

Yes, those checkboxes are defining which fields will be used by the default Search Box coming sith the search module. It doesn't prevent you from using the same index completely differently in your own modules.

Sep 19, 2011 at 10:18 PM

I'm trying to make also an advanced search module. I would like to put a taxonomy field as a dropdownlist so when the user select it, it will search only in this taxonomy.

How can I do it? Any advices? Thanks.

Coordinator
Sep 19, 2011 at 10:27 PM

Should be really simple, just adding a WithField contidion in your controller to the SearchBuilder, using the corresponding taxonomy field, and voila.

Sep 22, 2011 at 5:48 PM
Edited Sep 22, 2011 at 5:51 PM

Thanks Sebastien. I found where you have to use WithField. I should read more carefully the code next time.

if (filterCulture) {
                /* TODO: (sebros) Implementations details after Alpha
                var culture = _cultureManager.GetSiteCulture();

                // use LCID as the text representation gets analyzed by the query parser
                searchBuilder
                    .WithField("culture", CultureInfo.GetCultureInfo(culture).LCID)
                    .AsFilter();
                 */
            }

However, I don't understand how to implement it. I did the tutorial with the Taxonomies and I try to put in the condition

searchBuilder.WithField("section", Taxonomies.Section);

But it's said that "Taxonomies" does not exist in the current context so I add "using Contrib.Taxonomies;" I got an error that "Contrib could not be found"
> What did I miss??

I'm just starting with Orchard and found it's very interesting CMS but it's a little complex when you want to modify the existing code.
I worked with ASP and C# before but never with MVC. Any advices on what I should check to understand better the Orchard's implementation?
I've already done the tutorials on the Orchard's website and currently checking about the MVC 3.