关于Lucene.net 中高亮显示关键词的深究
这几天一直在学习lucene,也写了3篇自己总结的知识点,本以为很容易上手的东西,但是却遇到了一个很棘手的问题,借此,希望可以跟大家探讨一下
问题:使用盘古高亮显示组件后,如搜索“mp3 player”,搜索词中间包含空格,但是搜出来的结果,经过高亮后,空格莫名其妙的没了,具体不知道是怎么回事,如果去掉高亮的话,搜出来的结果中就有空格
代码:
////用于搜索的代码
Lucene.Net.Store.FSDirectory dir = Lucene.Net.Store.FSDirectory.GetDirectory(basePath);
Lucene.Net.Search.IndexSearcher search = new Lucene.Net.Search.IndexSearcher(dir, true);
Lucene.Net.Search.BooleanQuery boolQuery = new Lucene.Net.Search.BooleanQuery();
Lucene.Net.QueryParsers.QueryParser parser = new Lucene.Net.QueryParsers.QueryParser(Lucene.Net.Util.Version.LUCENE_29, "title", pgAnalyzer);
boolQuery.Add(parser.Parse(kw), Lucene.Net.Search.BooleanClause.Occur.MUST);
parser = new Lucene.Net.QueryParsers.QueryParser(Lucene.Net.Util.Version.LUCENE_29, "keywords", pgAnalyzer);
boolQuery.Add(parser.Parse(kw), Lucene.Net.Search.BooleanClause.Occur.SHOULD);
parser = new Lucene.Net.QueryParsers.QueryParser(Lucene.Net.Util.Version.LUCENE_29, "productsummary", pgAnalyzer);
boolQuery.Add(parser.Parse(kw), Lucene.Net.Search.BooleanClause.Occur.SHOULD);
Lucene.Net.Search.Hits hits = search.Search(boolQuery);
List<ProductInfo> lists = PageList(hits, pageIndex, pageSize);
if (lists != null && lists.Count > 0)
{
records = hits.Length();
rptList.DataSource = lists;
rptList.DataBind();
}
dir.Close();
///分页显示的结果处理,其中ProductInfo是自己定义的一个数据结果体
public List<ProductInfo> PageList(Lucene.Net.Search.Hits hits, int pageIndex, int pageSize)
{
List<ProductInfo> lists = new List<ProductInfo>();
if (hits == null)
return null;
if (hits.Length() <= 0)
return null;
int totalPages = hits.Length() % pageSize == 0 ? hits.Length() / pageSize : hits.Length() / pageSize + 1;//总页数
if (pageIndex <= 0)
pageIndex = 1;
if (pageIndex > totalPages)
pageIndex = totalPages;
for (int i = (pageIndex - 1) * pageSize; i < pageIndex * pageSize && i < hits.Length(); i++)
{
Lucene.Net.Documents.Document doc = hits.Doc(i);
ProductInfo item = new ProductInfo();
item.AttributeCustom = doc.Get("attributecustom");
item.BusinessType = doc.Get("businesstype");
item.CompanyName = doc.Get("companyname");
item.CountryName = doc.Get("countryname");
item.Guid = doc.Get("guid");
item.LocalPic = doc.Get("localpic");
item.MemberId = doc.Get("memberid");
item.ProductSummary = doc.Get("productsummary");
item.Title = doc.Get("title");
item.UrlTitle = doc.Get("urltitle");
lists.Add(item);
}
return lists;
}
///高亮方法,在repeter绑定数据源时传入当前的title调用
protected string hightLightTitle(string title)
{
if (string.IsNullOrEmpty(title))
return string.Empty;
if (string.IsNullOrEmpty(title))
return string.Empty;
PanGu.HighLight.Highlighter highter = new PanGu.HighLight.Highlighter(formatter, new PanGu.Segment());
highter.FragmentSize = 100;
return highter.GetBestFragment(kw, title);
}
比如:搜索 “China Shine Globe”,出来的结果如下
关键词中的空格莫名其妙的没了,真的很让人费解,小弟研究了好久叶没搞明白,希望各位大侠可以指导指导
关于Lucene.net 中高亮显示关键词的深究的更多相关文章
- 【Vue.js】基于vue的实时搜索,在结果中高亮显示关键词
一.搜素效果如下: 二.核心 1)利用oninput属性来触发搜素功能 2)利用RegExp来对字符串来全局匹配关键字,利用replace方法来对匹配的关键字进行嵌入高亮的<span class ...
- 【lucene系列学习二】Lucene实现高亮显示关键词
首先,导入下图所示库 然后,import org.apache.lucene.search.highlight.*; 下面,我们新建一个实现高亮显示功能的函数 public static String ...
- dedecms后台批量替换文章中的关键词
DEDECMS怎么样能快捷或者批量修改网站所有文章的超链接和锚文本,超链和所有关键词锚文本的链接需要修改? dedecms后台批量替换文章中的关键词
- JS批量替换内容中关键词为超链接,避开已存在的链接和alt、title中的关键词
懂点seo的人都知道要给内容中关键词加上链接,形成站内锚文本链接,这对seo有很大的帮助. 思路就是在数据库中录入若干个关键词和关键词对应的链接,当然链接可以根据关键词的id自动生成,或者直接用关键词 ...
- 2017.10.19 java中的关键词 及软件开发的一些重要单词
1.java中的关键词 ·final 最后的 (1)可以修饰表示该类不能被继承 (2)可以修饰方法不能被重写 ·sattic 静态的 类的 (1)修饰属性 可以通过类直接调用 可以通过对象调用 (2) ...
- php中use关键词使用场景
php中use关键词使用场景,主要使用在函数内部使用外包得变量才使用得 1,这种函数使用不到外包变量 $messge="96net.com.cn"; $exam=function ...
- Lucene 4.7 --高亮显示
jar lucene-analyzers-common-4.7.0.jar lucene-analyzers-smartcn-4.7.0.jar lucene-core-4.7.0.jar lucen ...
- springboot+lucene实现公众号关键词回复智能问答
一.场景简介 最近在做公众号关键词回复方面的智能问答相关功能,发现用户输入提问内容和我们运营配置的关键词匹配回复率极低,原因是我们采用的是数据库的Like匹配. 这种模糊匹配首先不是很智能,而且也没有 ...
- 基于Vue实现关键词实时搜索高亮显示关键词
最近在做移动real-time-search于实时搜索和关键词高亮显示的功能,通过博客的方式总结一下,同时希望能够帮助到别人~~~ 如果不喜欢看文字的朋友我写了一个demo方便已经上传到了github ...
随机推荐
- android aidl 进程间通信需要注意的地方(android.os.TransactionTooLargeException)
转自:http://blog.sina.com.cn/s/blog_4e1e357d0102wau9.html 1.bus工程实现通过service实现aidl实体类 2.actor工程通过发起bin ...
- Cause: org.apache.ibatis.builder.BuilderException: Error creating document instance. Cause: org.xm
mybatis解析xml配置文件出现异常: org.apache.ibatis.exceptions.PersistenceException: Error building SqlSession. ...
- UITabBarButton 点击失效问题
开发过程: 在创建一个UIWindow时,直接在window上添加手势动作. 开发代码: UITapGestureRecognizer *tapRecognizer=[[UITapGestureRec ...
- URL Scheme APP跳转safari以及跳回APP
上图 : 在plist文件里面设置. URL identifier 一般为反域名+项目名称 (尽可能保证少重复) URL Schemes是一个数组.一个APP可以添加多个.该参数为跳转时使用的标识. ...
- jquery的checkbox 全选和全不选
今天写了一个checkbox的全选和全不选的功能: var check_all=function(){ if(this.checked){ //alert($(".adv_check_num ...
- MIT JOS学习笔记03:kernel 02(2016.11.08)
未经许可谢绝以任何形式对本文内容进行转载! 本篇接着上一篇对kernel的分析. (5)pte_t * pgdir_walk(pde_t *pgdir, const void *va, int cre ...
- Python 常用函数
字符串->数字: float(str) int(str) 十六进制字符串转int: 不带0x前缀: x = int('dead',16) 带0x前缀: x = int('0xff', 0) 其中 ...
- oracle数据库从入门到精通之四
序列是oracle中较为重要的概念事务对于ddl是不起作用的查询,更新,数据表,约束这些个概念要掌握.在许多数据库之中都会存在一种数据类型--自动增长列,它能够创建流水号12c之前并没有提供这样一个自 ...
- DBA-mysql-init-password-5.7
1.Mysql5.7 Password; 查找临时密码:grep "A temporary password" /var/log/mysqld.log 修改临时密码:alter ...
- Yii2.0中form->field如何获取主表的一个字段并且设置为只读
<?= $form->field($model, 'last_login_time')->textInput(['readonly' => 'true']) ?>