热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Umbraco中的ExamineSearch功能讲解

转载原地址:http:24days.inumbraco2013getting-started-with-examineEverytimeIreadthewordExa

转载原地址: http://24days.in/umbraco/2013/getting-started-with-examine/

Everytime I read the word Examine or Lucene it is always combined with doing some crazy data extravaganza that sounds magical but requires 2 strong men, a Tesla Roadster, some squirrels(N amount) and 400 man-hours to get done.

So once upon a time I got a call from a customer, "We want some simple search thingy on our page", and I was like "sure thing we got packages for that". I installed a brilliant package and they were happy, until they figured out they wanted handling for typos, when their customers searched for products and the speed was also so-so because of the node amount. So I set out to find another way around, and I found it by doing something quite simple with examine.

I'm no examine/lucene ninja, but I hope my 2 cents here can get people started on playing around on their own because with examine there is so many possibilities to use your content (sorting tons of content based on different parameters, searching etc.).

So the idea with this is giving you a little intro to what is needed to get started with examine with a very hands-on aproach :)

Examine (based on Lucene) is an indexing/search engine that takes our content/data and puts it into a "phonebook" of sorts(index), so that we can search/lookup through it with blazing speed even on large amount of data/content.

So we want to do 2 things, first we are gonna have a look on the configuration (what sort of stuff do we want in our index?), secondly we want to make it searchable.

Part 1 : The configuration 

So in every Umbraco installation there is a folder called "Config". Its filled with (you guessed it) configuration files that does tons of different stuff to your solution, and don't worry it's not dangerous to play around in here.

Two of these files we want to pop open and poke around inside.

/Config/ExamineIndex.config

/Config/ExamineSettings.config


ExamineIndex.config

In this file we want to define a new indexset, and the indexset contains the info on which doctypes and fields we want to index. For the example it could look something like this:

"MySearch" IndexPath="~/App_Data/ExamineIndexes/MySearch/">"id" />"nodeName"/>"updateDate" />"writerName" />"nodeTypeAlias" />"bodyText"/>"siteName"/>"umbHomePage" />"umbNewsItem" />"umbTextPage" />

This block is just placed under the other .

SetName is the reference, or the alias if you like that we want to remember when were gonna call the index from our providers.

IndexAttributeFields defines all the default Umbraco fields that a node contains such as name, nodetype and more.

IndexUserFields is the alias of the custom fields you have added to your doctypes.

IncludeNodeTypes is the alias of the doctypes you want to search through.

So now we have defined an indexset that takes 3 doctypes and looks for 2 properties. This is what we can search later on.

That was one file down and one to go.

ExamineSettings.config

So inside the examinesettings.config file we want to do 2 things, and that's adding a few providers (Index and search provider). These two handles, you guessed it, indexing our data/content and giving us the option to search it.

Index provider 

"MySearchIndexer" type="UmbracoExamine.UmbracoContentIndexer, UmbracoExamine"supportUnpublished="false"supportProtected="true"interval="10"analyzer="Lucene.Net.Analysis.Standard.StandardAnalyzer, Lucene.Net"indexSet="MySearch"/>

Paste it in just before

We have different settings here, should it index unpublished or protected nodes? and how often. One thing that is important is the IndexSet equals the Index SetName that we defined inside examineindex.config right before.

Search provider

The next thing we need to config is the search provider.

"MySearchSearcher" type="UmbracoExamine.UmbracoExamineSearcher, UmbracoExamine"analyzer="Lucene.Net.Analysis.Standard.StandardAnalyzer, Lucene.Net" indexSet="MySearch" enableLeadingWildcards="true"/>

This should just be added right before


Again some settings on how the search works, though not something we will talk about since we just run with defaults, but again we want to reference the IndexSet Name and enable leading wildcards.

If you want to dive deeper into the different params and get a better understanding for all this crazyness I recommend that you read this one :

http://umbraco.com/follow-us/blog-archive/2011/9/16/examining-examine.aspx

 Part 2 : Let the search begin

So now we have configured all the stuff that we need and can start by cooking up a new razor macro called something as magical as "Search" which we can embed into our awesome search template.

So now we want to create a SIMPLE search that can handle some spelling mistakes and does the "basic" job we want.

First solution
So lets look at how this can be done in a simple example first:

@inherits umbraco.MacroEngines.DynamicNodeContext
@using Examine.LuceneEngine.SearchCriteria
@{
if (!string.IsNullOrEmpty(Request.QueryString["search"])){//Fetching what eva searchterm some bloke is throwin' our wayvar q = Request.QueryString["search"];//Fetching our SearchProvider by giving it the name of our searchprovider var Searcher = Examine.ExamineManager.Instance.SearchProviderCollection["MySearchSearcher"];//Searching and ordering the result by score, and we only want to get the results that has a minimum of 0.05(scale is up to 1.)var searchResults = Searcher.Search(q, true).OrderByDescending(x => x.Score).TakeWhile(x => x.Score > 0.05f);//Printing the results

}
}

 Just to do a quick run down the code we 3 things.

We fetch the term some dude just searched for from our query. We select which searchprovider to use (the one we setup in the config files remember), and last we search, oh yeah and print out (4 things sorry).

When we do the "search" we ask for results ordered by "score". Everytime we do something with Lucene, it's being returned with a score on how close it was to our search. I'm also asking it that we only want the items above a specific threshold so we ensure some kind of quality to our results.

So now when the customer searches for "geting started" instead of "getting started" it still finds the results.

Quite simple.

Little bonus: Also notice that we have something called "fields" where I'm fetching the ID of the node. But inside fields we can add all sorts of properties, so if you just needed to display the name of a page and the URL, we don't need to look up the node to fetch it, we could save some machine powa' by just adding the properties to the list of fields. That is what we did inside examineindex.config.

So this one would probably do it for most simple sites but let's say you wanna dive a bit deeper, you want to control which fields it should search through, and if some fields are more important than others. 

Second solution

Let's go a bit deeper down the rabbit hole with this one, so let's do something similar just where we have a few more options. 

In Lucene we can build our own queries for content. This can be done in 2 ways, and I will show the "Fluent" (chaining) way, while the other one is writing raw Lucene queries (you can look into this through some of the links at the bottom).

So this next example is quite similar to the first one but instead we are controlling which fields we wanna look into and if a field is more important.

@inherits umbraco.MacroEngines.DynamicNodeContext
@using Examine.LuceneEngine.SearchCriteria
@{
if (!string.IsNullOrEmpty(Request.QueryString["search"])){//Fetching what eva searchterm some bloke is throwin' our wayvar q = Request.QueryString["search"].Trim();//Fetching our SearchProvider by giving it the name of our searchprovider var Searcher = Examine.ExamineManager.Instance.SearchProviderCollection["MySearchSearcher"];var searchCriteria = Searcher.CreateSearchCriteria(Examine.SearchCriteria.BooleanOperation.Or);var query = searchCriteria.Field("nodeName", q.Boost(3)).Or().Field("bodyText", q.Fuzzy());//Searching and ordering the result by score, and we only want to get the results that has a minimum of 0.05(scale is up to 1.)var searchResults = Searcher.Search(query.Compile()).OrderByDescending(x => x.Score).TakeWhile(x => x.Score > 0.05f);//Printing the results

}
}

So the difference here is that we can now control which fields we look at.

We do this trough something we call a SearchCriteria. Inside the SearchCriteria we tell which fields we want to look at like "nodeName" and bodyText.

Also we are telling the search that if it finds something in nodeName it's more important than bodyText since we are giving it a "boost". To a boost we add a value to buff the "score" of a resultitem.

We are also saying that we want to look at the bodyText, and we are adding a "fuzzy" option to it. This tells the search it should match on items that "looks like it". This is where we get some sort of spelling help.

The whole idea here is that we can keep on adding fields, and also if we want something specific to be true (could be inside a certain date range, only look at specific nodetypes etc.).


转载于:https://www.cnblogs.com/wphl-27/p/5805453.html


推荐阅读
  • Centos7.6安装Gitlab教程及注意事项
    本文介绍了在Centos7.6系统下安装Gitlab的详细教程,并提供了一些注意事项。教程包括查看系统版本、安装必要的软件包、配置防火墙等步骤。同时,还强调了使用阿里云服务器时的特殊配置需求,以及建议至少4GB的可用RAM来运行GitLab。 ... [详细]
  • Windows 7 部署工具DISM学习(二)添加补丁的步骤详解
    本文详细介绍了在Windows 7系统中使用部署工具DISM添加补丁的步骤。首先需要将光驱中的安装文件复制到指定文件夹,并进行挂载。然后将需要的MSU补丁解压并集成到系统中。文章给出了具体的命令和操作步骤,帮助读者完成补丁的添加过程。 ... [详细]
  • CEPH LIO iSCSI Gateway及其使用参考文档
    本文介绍了CEPH LIO iSCSI Gateway以及使用该网关的参考文档,包括Ceph Block Device、CEPH ISCSI GATEWAY、USING AN ISCSI GATEWAY等。同时提供了多个参考链接,详细介绍了CEPH LIO iSCSI Gateway的配置和使用方法。 ... [详细]
  • 本文介绍了在CentOS 6.4系统中更新源地址的方法,包括备份现有源文件、下载163源、修改文件名、更新列表和系统,并提供了相应的命令。 ... [详细]
  • 本文介绍了如何在使用emacs时去掉ubuntu的alt键默认功能,并提供了相应的操作步骤和注意事项。 ... [详细]
  • ElasticSerach初探第一篇认识ES+环境搭建+简单MySQL数据同步+SpringBoot整合ES
    一、认识ElasticSearch是一个基于Lucene的开源搜索引擎,通过简单的RESTfulAPI来隐藏Lucene的复杂性。全文搜索,分析系统&# ... [详细]
  • 本文介绍了在无法联网的情况下,通过下载rpm包离线安装zip和unzip的方法。详细介绍了如何搜索并下载合适的rpm包,以及如何使用rpm命令进行安装。 ... [详细]
  • 程序员如何选择机械键盘轴体?红轴和茶轴对比
    本文介绍了程序员如何选择机械键盘轴体,特别是红轴和茶轴的对比。同时还介绍了U盘安装Linux镜像的步骤,以及在Linux系统中安装软件的命令行操作。此外,还介绍了nodejs和npm的安装方法,以及在VSCode中安装和配置常用插件的方法。最后,还介绍了如何在GitHub上配置SSH密钥和git的基本配置。 ... [详细]
  • 本文由编程笔记#小编为大家整理,主要介绍了markdown[软件代理设置]相关的知识,希望对你有一定的参考价值。 ... [详细]
  • charles3.11.1抓https包
    结论先行:用的是安卓测试机,没加固之前的生产环境的安装包,可以抓到https请求加固之后的包【也就是要上应用市场的包】,抓不到https请求电脑上的操作:1.安装证书【电脑上安装了 ... [详细]
  • Pico Neo 3教程☀️ 六、项目的配置总结及交互开发
    文章目录🟥SDK的导入和项目的设置1️⃣项目的部分配置2️⃣PlayerSettings设置✨将项目切换到Gamma颜色空间✨MinimumAPILevel ... [详细]
  • 本文介绍了在rhel5.5操作系统下搭建网关+LAMP+postfix+dhcp的步骤和配置方法。通过配置dhcp自动分配ip、实现外网访问公司网站、内网收发邮件、内网上网以及SNAT转换等功能。详细介绍了安装dhcp和配置相关文件的步骤,并提供了相关的命令和配置示例。 ... [详细]
  • 本文介绍了在Linux下安装Perl的步骤,并提供了一个简单的Perl程序示例。同时,还展示了运行该程序的结果。 ... [详细]
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • Java自带的观察者模式及实现方法详解
    本文介绍了Java自带的观察者模式,包括Observer和Observable对象的定义和使用方法。通过添加观察者和设置内部标志位,当被观察者中的事件发生变化时,通知观察者对象并执行相应的操作。实现观察者模式非常简单,只需继承Observable类和实现Observer接口即可。详情请参考Java官方api文档。 ... [详细]
author-avatar
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有