Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Dan 1285 posts 3917 karma points c-trib
    Jan 21, 2014 @ 16:20
    Dan
    0

    Clarification on limiting Examine search to particular document type

    Hi,

    I currently have an Examine index which writes tags and MNTP property values into a custom field in the index. It's working well, but now the requirement is to search only content of a particular document type. Sometimes the search will need to search only 'article' document types. Sometimes it will search only 'product' document types.

    Currently this is being done by passing the document type alias to the index like this as it builds the query:

    string luceneString = string.Format("__NodeTypeAlias:({0}) ", nodeTypeAlias);
    

    However, I'm seeing a mixture of document types appear in the results here, so what I'm thinking is that searching this way is only 'loose' in that the document type is only one of the search criteria considered, and it's not exclusive.

    Sooo... am I best creating a new index for each document type, or is there a way to make the document type part of the query exclusive, so that only document types of 'Product' will be returned if that's what's passed to it?

    My gut instinct suggests it'd be neater just to deal with a single index, but I'm open to suggestions!

    Any pointers appreciated, thanks folks.

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Jan 21, 2014 @ 16:34
    Ismail Mayat
    1

    Dan,

    I am assuming you are building the query raw using strings to give the final lucene syntax query? I on numerious occasions done doc type searches and it only comes back with the specific doc type however I have always used the fluent api e.g

    BaseSearchProvider _searcher = ExamineManager.Instance.SearchProviderCollection["myindex"];
    var criteria = _searcher.CreateSearchCriteria(IndexTypes.Content);
    
    IBooleanOperation examineQuery;
    
    IExamineValue cleanKeyword = searchTerm.MakeSearchQuerySafe().MultipleCharacterWildcard();
    examineQuery = criteria.Field(Constants.Examine.IndexFieldAliases.Contents, cleanKeyword);
    
    examineQuery = AddNodeTypeAliasFilter(nodeTypeAliases, examineQuery);
    
    /// <summary>
    /// append all nodetype aliases to the query to filter on
    /// </summary>
    /// <param name="nodeTypeAliases"></param>
    /// <param name="queryToBuild"></param>
    /// <returns></returns>
    private IBooleanOperation AddNodeTypeAliasFilter(IEnumerable<string> nodeTypeAliases,
        IBooleanOperation queryToBuild)
    {
    
        var fields = new List<string> {"nodeTypeAlias"};
    
        queryToBuild.And().GroupedOr(fields, nodeTypeAliases.ToArray());
    
        return queryToBuild;
    }
  • Dan 1285 posts 3917 karma points c-trib
    Jan 21, 2014 @ 16:40
    Dan
    0

    Thanks Ismail, yes, I'm building up a raw query using strings. Actually I've just read the old faithful blog post about Examine and I'm experimenting with this, which looks promising (adding the plus to the query to make it HAVE to include that node type alias):

    string luceneString = string.Format("+__NodeTypeAlias:({0}) ", nodeTypeAlias);
    

    I'm having a couple of server access issues at the moment so not able to test whether this is working, but I'll report back. If not, I'll try the fluent API as per your example.

    Thanks again.

  • Ismail Mayat 4511 posts 10091 karma points MVP 2x admin c-trib
    Jan 21, 2014 @ 16:43
    Ismail Mayat
    100

    Dan,

    The + will sort it. One thing i would also search on nodeTypeAlias and not __NodeTypeAlias anything in the index with __ is used for sorting purposes.  Also you can test your queries using ExamineInspector (http://our.umbraco.org/projects/backoffice-extensions/examine-inspector) if you install that then you do not need server access.  

    Regards

     

    Ismail

  • Dan 1285 posts 3917 karma points c-trib
    Jan 21, 2014 @ 17:02
    Dan
    0

    Excellent, it does indeed sort it! And thanks for your blog/forum posts etc on Examine, it's all really useful stuff. #h5yr :)

Please Sign in or register to post replies

Write your reply to:

Draft