package com.jerry.service;
import java.util.ArrayList;
import java.util.HashMap;
import org.apache.lucene.document.Document;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
import org.apache.lucene.search.TopDocs;
import org.wltea.analyzer.lucene.IKQueryParser;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
import com.jerry.adapter.ListViewAdapter;
import com.jerry.po.Book;
import com.jerry.util.HtmlUrlUtil;
import com.jerry.util.LuceneDocumentUtil;
import com.jerry.util.LuceneUtil;
import com.jerry.util.LuneceHighlighterUtil;
public class LuceneService {
private Context context = null;
private ListView bookListView = null;
private ArrayList<String> url = null;
// 定义各个查询条件的常量
private final static String[] TITLE = { "name" };
private final static String[] FIRSTTITLE = { "firstTitle" };
private final static String[] SECONDTITLE = { "secondTitle" };
private final static String[] CONTENT = { "content" };
private final static String[] ALL = { "name", "firstTitle", "secondTitle",
"content" };
public LuceneService(Context context, ListView bookListView) {
super();
this.context = context;
this.bookListView = bookListView;
}
/**
*
* @function:将书本信息添加到ListView
* @author:Jerry
* @date:2013-9-24
* @param books
*/
public void addBookToListView(ArrayList<Book> books) {
int count = books.size();
if (count > 0) {
// listItem用于存放books
ArrayList<HashMap<String, Object>> listItem = new ArrayList<HashMap<String, Object>>();
// 每条书本信息对应的详细信息连接
this.url = new ArrayList<String>();
// 循环遍历books,按键值对的形式存入HashMap
for (int i = 0; i < count; i++) {
HashMap<String, Object> item = new HashMap<String, Object>();
item.put("name", books.get(i).getName());
item.put("firstTitle", books.get(i).getFirstTitle());
item.put("secondTitle", books.get(i).getSecondTitle());
item.put("content", books.get(i).getContent());
url.add(HtmlUrlUtil.getUrl(books.get(i).getUrl()));
// 把item项的数据加到listItem中
listItem.add(item);
}
// 新建自定义ViewAdapter
ListViewAdapter listViewAdapter = new ListViewAdapter(this.context,
listItem);
// 把适配器装载到listView中
this.bookListView.setAdapter(listViewAdapter);
// 为每个item项注册点击事件
this.bookListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// 当用户点击某个item项时,用浏览器打开网页,查看更详细的信息
Intent intent = new Intent();
intent.setAction("android.intent.action.VIEW");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse(url.get(arg2)));
intent.setClassName("com.android.browser",
"com.android.browser.BrowserActivity");
context.startActivity(intent);
}
});
} else {
Toast.makeText(this.context, "没有返回数据!", Toast.LENGTH_SHORT).show();
}
}
/**
*
* @function:获取查询条件
* @author:Jerry
* @date:2013-9-24
* @param item
* @return String[]
*/
public String[] getIndexSearch(long item) {
if (item == 0) {
// 按书名查找
return TITLE;
} else if (item == 1) {
// 按章查找
return FIRSTTITLE;
} else if (item == 2) {
// 按节查找
return SECONDTITLE;
} else if (item == 3) {
// 按内容查找
return CONTENT;
} else {
// 查找全部
return ALL;
}
}
/**
*
* @function:查询书本信息
* @author:Jerry
* @date:2013-9-24
* @param key
* @param value
* @param currentPage
* @return ArrayList
*/
public ArrayList<Book> queryBook(String[] key, String value, int currentPage) {
ArrayList<Book> books = new ArrayList<Book>();
IndexSearcher indexSearcher = null;
Query query = null;
try {
// 通过工具类获取indexSearcher
indexSearcher = LuceneUtil.getIndexSearcher();
// 用IKQueryParser解析要查询的内容,并返回Query对象
query = IKQueryParser.parseMultiField(key, value);
// 按照id进行排序
Sort sort = new Sort(new SortField("id", SortField.INT, true));
TopDocs topDocs = indexSearcher.search(query, null, 1000, sort);
// ScoreDoc 存储了文档的逻辑编号和文档积分
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (ScoreDoc temp : scoreDocs) {
// 根据doc的id去获取doc文档 ,返回是Document
Document doc = indexSearcher.doc(temp.doc);
// 通过工具类设置关键字高亮
doc.getField("name").setValue(
LuneceHighlighterUtil.highlighterText(query,
doc.get("name"), 10, value));
doc.getField("firstTitle").setValue(
LuneceHighlighterUtil.highlighterText(query,
doc.get("firstTitle"), 30, value));
doc.getField("secondTitle").setValue(
LuneceHighlighterUtil.highlighterText(query,
doc.get("secondTitle"), 30, value));
doc.getField("content").setValue(
LuneceHighlighterUtil.highlighterText(query,
doc.get("content"), 30, value));
books.add(LuceneDocumentUtil.DocumentToBook(doc));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return books;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
android+lucene实现全文检索并高亮关键字
共63个文件
class:19个
java:11个
png:8个
4星 · 超过85%的资源 需积分: 35 203 下载量 181 浏览量
2013-09-28
20:07:17
上传
评论 2
收藏 4.52MB RAR 举报
温馨提示
使用lucene技术,实现android的本地文档资源的全文检索功能,并实现关键字高亮
资源推荐
资源详情
资源评论
收起资源包目录
android+lucene实现全文检索并高亮关键字.rar (63个子文件)
lucene
AndroidManifest.xml 821B
res
layout
law_item.xml 2KB
law_layout.xml 2KB
drawable-ldpi
ic_launcher.png 2KB
drawable-mdpi
ic_launcher.png 3KB
drawable-hdpi
bg.png 91KB
ic_launcher.png 4KB
values
strings.xml 184B
arrays.xml 312B
proguard.cfg 1KB
src
mydict.dic 23B
com
jerry
po
Book.java 1KB
util
LuneceHighlighterUtil.java 4KB
LuceneDocumentUtil.java 633B
LuceneUtil.java 1KB
LuceneConfigurationUtil.java 1KB
HtmlUrlUtil.java 890B
action
LuceneAction.java 1KB
service
LuceneService.java 6KB
activity
LuceneActivity.java 1KB
adapter
ListViewAdapter.java 3KB
ext_stopword.dic 153B
properties
htmlUrl.properties 560B
IKAnalyzer.cfg.xml 479B
bin
classes
mydict.dic 23B
com
jerry
po
Book.class 2KB
util
LuceneUtil.class 1KB
LuceneUtil$1.class 484B
HtmlUrlUtil.class 2KB
LuceneDocumentUtil.class 1KB
LuneceHighlighterUtil.class 3KB
LuceneConfigurationUtil.class 2KB
action
LuceneAction.class 2KB
service
LuceneService$1.class 2KB
LuceneService.class 6KB
activity
R.class 579B
R$layout.class 432B
R$attr.class 340B
R$id.class 646B
R$array.class 400B
R$string.class 427B
LuceneActivity.class 2KB
R$drawable.class 433B
adapter
ListViewAdapter.class 3KB
ext_stopword.dic 153B
properties
htmlUrl.properties 560B
IKAnalyzer.cfg.xml 479B
res
drawable-ldpi
ic_launcher.png 2KB
drawable-mdpi
ic_launcher.png 2KB
drawable-hdpi
bg.png 87KB
ic_launcher.png 4KB
classes.dex 1.15MB
resources.ap_ 100KB
lucene.apk 1.69MB
.classpath 631B
assets
project.properties 360B
.settings
org.eclipse.core.resources.prefs 250B
libs
IKAnalyzer3.2.8.jar 1.12MB
lucene-core-3.0.0.jar 998KB
lucene-highlighter-3.0.0.jar 46KB
lucene-memory-3.0.0.jar 27KB
.project 842B
gen
com
jerry
activity
R.java 1KB
共 63 条
- 1
reality_jie
- 粉丝: 112
- 资源: 11
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- Edge浏览器下载文件提示 “无法安全下载” 的解决方法
- 基于springboot+layui的医院日常耗材管理系统.zip
- 计算机毕业设计-ASP.NET教育报表管理系统-权限管理模块(源代码+)-毕设源码实例.zip
- 计算机毕业设计-ASP.NET教务信息管理系统的设计与实现(源代码+)-毕设源码实例.zip
- 免费计算机毕业设计-线上公司求职招聘系统的设计与实现(包含论文+源码)
- Eleven的精益供应链管理-碓胤咨询龚胤全.rar
- 5套光伏、储能、充电收益测算表.zip
- C2 供应链集成演示平台操作手册(详细版).rar
- 3套光储充一体化站CAD+PDF图纸.zip
- c++游戏开发,本人开发的c++小游戏飞机大战(二)源码
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
- 5
- 6
前往页