Custom development SharePoint search function

Overview

  Customer requirements to improve SharePoint 2007 search function, we must search for the definition of self-developed search functions, doubled surveys and studies, sharepoint used KeywordSearchQuery and FullTextSearchQuery of two ways, because we have to use sharepoint list custom field conditions as misplaced , so the use of FullTextSearchQuery to develop.

 

design

FullTextSearchQuery do sharepoint 2007 secondary development to use the following components:

using Microsoft.Office.Server;
using Microsoft.Office.Server.Search;
using Microsoft.Office.Server.Search.Query;

 

 

In sharepoint 2007, we have the following scope ():

image

 

The default people and all sites have two scope. We illustrate with All sites.

 

sharepoint Content sources in the flowchart crawler (ease of understanding source scope () content)

2

 

 

The following is a flow chart to check:

image

 

 

When the user query, sharepoint users for security checks:

3

 

Described above is only done when the reader understand sharepoint query work.

Microsoft.SharePoint.Search.Query namespace includes three queries like:

  • FullTextSqlQuery use of such executable SQL syntax search queries.

  • KeywordQuery use of such executable keyword search query syntax.

Select Query class for a custom search application

To determine the custom search application (for self FullTextSqlQuery or KeywordQuery complicated appropriate class level), consider want your application code to support a search query.

    The following list identifies only by the use FullTextSqlQuery additional elements of the SQL query syntax supports class of search: FREETEXT, CONTAINS, the LIKE and ORDER BY.

Code design

    public class SharePointSearch
    {

        #region Fields

        string queryStr = @"select hitHighlightedProperties,size,Write, author, path,
                            hitHighlightedSummary,title,contentclass
                            from scope() where " +
                            "\"scope\" = '{0}' AND FREETEXT(defaultproperties, '{1}') ";
        string conditionSearchSource = " AND CONTAINS(path,'{0}')";

        #endregion

        #region Constructor

        /// <summary>
        /// Default constructor.
        /// </summary>
        public SharePointSearch()
        {
        }

        #endregion

        #region Public Methods

        /// <summary>
        /// 
        /// </summary>
        /// <param name="scope"></param>
        /// <param name="searchSource"></param>
        /// <param name="keyword"></param>
        /// <param name="rowLimit"></param>
        /// <returns></returns>
        public DataTable GetSearchResultByKeyWord(string scope, string searchSource, string keyword, int rowLimit)
        {
            string query = string.Format(this.queryStr, scope, keyword);
            if (!string.IsNullOrEmpty(searchSource))
                query += string.Format(this.conditionSearchSource, searchSource);

            return this.CustomSearch(query, rowLimit);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="query"></param>
        /// <param name="rowLimit"></param>
        /// <returns></returns>
        public DataTable GetSearchResultByQuery(string query, int rowLimit)
        {
            return this.CustomSearch(query, rowLimit);
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Excute search query
        /// </summary>
        /// <param name="query"></param>
        /// <param name="rowLimit"></param>
        /// <returns></returns>
        private DataTable CustomSearch(string query, int rowLimit)
        {
            DataTable result = new DataTable();
            string url = SPContext.Current.Web.Url;

            using (SPSite site = new SPSite(url))
            {
                ServerContext context = ServerContext.GetContext(site);

                FullTextSqlQuery fullQuery = new FullTextSqlQuery(site);
                fullQuery.Culture = System.Globalization.CultureInfo.InvariantCulture;
                fullQuery.QueryText = query;
                fullQuery.ResultTypes = ResultType.RelevantResults;
                fullQuery.EnableStemming = false;
                fullQuery.IgnoreAllNoiseQuery = true;
                fullQuery.TrimDuplicates = true;

                fullQuery.KeywordInclusion = KeywordInclusion.AnyKeyword;
                fullQuery.RowLimit = rowLimit;
                if (SPSecurity.AuthenticationMode != System.Web.Configuration.AuthenticationMode.Windows)
                    fullQuery.AuthenticationType = QueryAuthenticationType.PluggableAuthenticatedQuery;
                else
                    fullQuery.AuthenticationType = QueryAuthenticationType.NtAuthenticatedQuery;

                ResultTableCollection rt = fullQuery.Execute();
                ResultTable resultTable = rt[ResultType.RelevantResults];
                result.Load(resultTable, LoadOption.OverwriteChanges);
            }

            return result;
        }

        #endregion
    }

 

And why FullTextSqlQuery attribute is set, xml through moss can result because of FIG tool produced as follows:

<?xml version="1.0" encoding="utf-8" ?>
<QueryPacket xmlns="urn:Microsoft.Search.Query" Revision="1000">
<Query domain="QDomain">
 <SupportedFormats><Format>urn:Microsoft.Search.Response.Document.Document</Format></SupportedFormats>
 <Context>
   <QueryText language="en-US" type="MSSQLFT"><![CDATA[ SELECT Title, Rank, Size, Description, Write, Path FROM portal..scope() WHERE FREETEXT(DefaultProperties, 'test') AND  ( ("SCOPE" = 'All Sites') )   ORDER BY "Rank" DESC ]]></QueryText>
 </Context>
 <Range><StartAt>1</StartAt><Count>20</Count></Range>
 <EnableStemming>false</EnableStemming>
 <TrimDuplicates>true</TrimDuplicates>
 <IgnoreAllNoiseQuery>true</IgnoreAllNoiseQuery>
 <ImplicitAndBehavior>false</ImplicitAndBehavior>
 <IncludeRelevanceResults>true</IncludeRelevanceResults>
 <IncludeSpecialTermResults>false</IncludeSpecialTermResults>
 <IncludeHighConfidenceResults>false</IncludeHighConfidenceResults>
</Query></QueryPacket>

 

 

If the item in sharepoint content language is en-us, then, be sure to set the IE browser:

image

The english [en] into the uppermost.

 

 

 

Author: the Spring Yang

Source: http://www.cnblogs.com/springyangwc/

This article belongs to the author and blog Park total, welcome to reprint, but without the author's consent declared by this section must be retained, and given the original connection in the apparent position of the article page, otherwise the right to pursue legal responsibilities.

Reproduced in: https: //www.cnblogs.com/springyangwc/archive/2011/07/08/2101389.html

Guess you like

Origin blog.csdn.net/weixin_33890499/article/details/93340928