📜 ⬆️ ⬇️

Integrating your data sources with Windows 7 federated search is easy!

How often do you have to look for different information? Most likely this happens every day. It should be noted that the task of finding information is far from trivial. The situation is complicated by the fact that information can be found in completely different sources - in files, in e-mail messages, in documents, etc. It is no secret that most of the information is in the network - local and global.

To simplify information retrieval in Windows Vista, a Windows Search tool was developed that allows you to easily and conveniently search for information based on previously prepared indexes. In Windows 7, the search theme was continued and the Windows 7 Federated Search tool appeared in the new operating system.


')
Windows 7 Federated Search is a tool that allows you to search for information on the web based on Windows Explorer. In this case, anything can be a source of data - a corporate website, an online store, online auctions, etc.

The difference between federated search and Windows Search is that the federated search mechanisms do not index data sources, but simply ask them to perform a search query. Federated search is focused on distributed information sources. When it comes to remote sources, their indexing can be inefficient and lead to unnecessary consumption of traffic. That is why an approach was chosen in which the task of processing a search query falls on the shoulders of a remote source. Thus, it becomes possible to connect all remote sources and search for them without leaving the usual Windows Explorer.

Despite all the convenience of such a search, implementing a search provider is a very simple task. Federated search in Windows 7 is based on the OpenSearch 1.1 standard and works as follows. To perform a search, Windows 7 accesses an external web service built on the basis of the REST approach. This means that the search string, as well as other data required for the search, is passed to the URI when accessing this web service. The web service based on this data should perform a search in its data source and return the result in RSS or AtomPub format. After that, Windows 7 will present the search results from the received data as files and display them to the user.

To add your search provider in Windows 7, you must create a description file for this provider. The format of this file is based on XML and contains information about this search service, incl. URI format for accessing the service.



Thus, to implement your own federated search provider in Windows 7, you need to perform two simple steps - create a REST service to search for information and make a description file for it.

Let's look at the process of creating a search provider using the following example. There is a list of books with a description, author and other information. In this case, this list is contained in the XML file (for a demo). Anything can be used as a data source. Make a search provider for this list.

The search provider description file is an XML file and has the extension “.osdx”. This file has the following structure.

<?xml version="1.0" encoding="utf-8" ?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
<ShortName>Federated Search sample provider</ShortName>
<Url type="application/rss+xml" template="http://somehost.com/search/?q={searchTerms}&start={startIndex}&count={count}" />
</OpenSearchDescription>


In the Url section of this file, an address pattern is defined that will be used when accessing a web service. It can be seen that this address can take a completely different form. The address pattern uses several sections into which values ​​will be inserted. The main section is the “searchTerms” section. This section will be substituted for the string to search. Federated search Windows 7 retrieves data page by page, so there are sections “count” and “startIndex”, which specify the size and number of the page. This is necessary so that Windows can get the first search results, display them to the user, and then do the rest of the items.

The demo will create a web service that will be hosted on a local machine. For this reason, the description file of the search provider will look like this.

<?xml version="1.0" encoding="utf-8" ?>
<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/">
<ShortName>Federated Search sample provider</ShortName>
<Url type="application/rss+xml" template="http://localhost:8731/FederatedSearchProviderSample/search/{searchTerms}/?start={startIndex}&count={count}" />
</OpenSearchDescription>


The last thing you need to do is create the service itself that will perform the search. In this case, there is no binding to a specific technology and the only requirement is that the service must return the result in RSS / Atom format. It is clear that the service itself can be built on completely different platforms and technologies. This could be PHP, Ruby, Native CGI, etc. If we consider a set of technologies for building REST services from Microsoft, then the best choice in this case is to use WCF features to build REST services. Much attention has already been paid to the construction of such services, so I will not dwell on this in detail, but will only describe the key steps.

The first thing to do is to define a contract. The contract will contain two operations - search and obtain detailed information.

[ServiceContract]
[ServiceKnownType(typeof(Atom10FeedFormatter))]
[ServiceKnownType(typeof(Rss20FeedFormatter))]
public interface ISearchProvider
{
[OperationContract]
[WebGet(UriTemplate = "search/{searchTerms}/*")]
SyndicationFeedFormatter Search(string searchTerms);
[OperationContract]
[WebGet(UriTemplate = "details/{id}")]
Stream Description(string id);
}


The most important thing to pay attention to at this moment is the definition of the URI pattern. As you can see in this case, the “search / {searchTerms} / *” template is fully consistent with what was defined in the description file.

It remains only to implement this service. When implementing, it is necessary to take into account the specified parameters when accessing the service (searchTerms, start, count) and break the search result into pages if necessary. LINQ - Take / Skip methods are ideal for this. Thus, the implementation of the service will be as follows.

public class SearchProvider : ISearchProvider
{
public SyndicationFeedFormatter Search(string searchTerms)
{
int count;
int startIndex;
int.TryParse(WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters.Get("count"), out count);
int.TryParse(WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters.Get("start"), out startIndex);
var result = SearchBooks(searchTerms);
if (count > 0)
{
if (startIndex >= 0)
{
result = result.Skip(count * (startIndex - 1)).Take(count);
}
else
{
result = result.Take(count);
}
}
return new Rss20FeedFormatter(
new SyndicationFeed("Federated search sample", String.Empty, null,
from item in result
select new SyndicationItem(item.Element(XName.Get("name")).Value,
item.Element(XName.Get("description")).Value,
new Uri(WebOperationContext.Current.IncomingRequest.UriTemplateMatch.BaseUri.ToString() + @"/details/" + item.Element(XName.Get("id")).Value))
{
PublishDate = DateTimeOffset.Parse(item.Element(XName.Get("date")).Value),
}));
}
//...
}


Also in the service there are methods for displaying detailed information and performing a search. If you wish, you can download an example and see their implementation there.

After the service is ready and running, you need to open the service description file (.osdx) in Windows and agree with the suggestion to add a search provider. After that, this provider will appear in the general list of search providers.




Now that the web service is running and the search provider has been successfully added, you can search the network resource directly from Windows Explorer. In addition, you can use the preview window. By default, the content that is located at the address to which this item refers is loaded into it (in the “demo application, this is“ / details / {id} ”). This address can be located HTML-content that will be displayed to the user.



As you can see, the implementation of the search provider for Windows 7 Federated Search is very simple, however, it can make using your data much more convenient and easier. One of the most successful examples of implementing a federated search by external sources is the search provider for Sharepoint-based corporate sites. Why don't we also implement this functionality for our applications?

Demo application:
FederatedSearchProviderSample.zip

Source: https://habr.com/ru/post/62027/


All Articles