您的位置:首页 > 其它

Lucene的简单使用

2017-12-17 18:59 281 查看
1.相关jar包

2.简单查询

//查询sql
private static String sql="select * from book";
public List<Book> findAllBook() throws Exception {
//使用jdbc连接数据库
Connection connection;

//预编译statement
PreparedStatement preparedStatement;

//结果集
ResultSet resultSet;

ArrayList<Book> books = new ArrayList<Book>();
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//连接数据库
connection= DriverManager.getConnection("jdbc:mysql://localhost:3306/lucence", "root", "123456");
preparedStatement = connection.prepareStatement(sql);
resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
Book book = new Book();
book.setId(resultSet.getInt("id"));
book.setName(resultSet.getString("name"));
book.setPrice(resultSet.getFloat("price"));
book.setPic(resultSet.getString("pic"));
book.setDescription(resultSet.getString("description"));
books.add(book);
}
return books;

3.简单测试

@Test
public void creatIndex() throws Exception {
//一个document对应一条book记录
//新建一个list用于存储document
ArrayList<Document> list = new ArrayList<Document>();
//得到所有的book
List<Book> books = new BookDaoImpl().findAllBook();
//遍历集合,然后对每一条数据进行操作
for (Book book : books) {
Document document = new Document();
//创建field域
//参数:域名,field域中存储的value值,是否存储
//图片id,不分词,要索引,要存储
StringField id = new StringField("id", book.getId().toString(), Field.Store.YES);
//图书名称的field,要分词,索引,存储
TextField name = new TextField("name", book.getName(), Field.Store.YES);
//图书价格,要分词,索引,存储
FloatField price = new FloatField("price", book.getPrice(), Field.Store.YES);
//图书图片,不分词,不要索引,要存储
StoredField pic = new StoredField("pic", book.getPic());
//图书的描述,要分词,要索引,不存储
TextField description = new TextField("description", book.getDescription(), Field.Store.NO);
//将field加入到document中
document.add(id);
document.add(name);
document.add(price);
document.add(pic);
document.add(description);
//将document加入到list集合中
list.add(document);
}

//开始创建索引
//需要一个分词器
StandardAnalyzer analyzer = new StandardAnalyzer();
//创建索引目录的流对象,指定索引目录的位置
Directory d = FSDirectory.open(new File("D:\\develop\\projects\\springMVC\\indexdata"));
//IndexWriter配置对象
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_10_4, analyzer);

//创建索引操作对象,起到承上启下的作用,通过这个可以看到需要什么东西
IndexWriter indexWriter = new IndexWriter(d, config);
//通过indexWriter创建索引
for (Document document : list) {
//创建索引
indexWriter.addDocument(document);
}
//提交
indexWriter.commit();
//关闭资源
indexWriter.close();
}

//搜索
@Test
public void searchIndex() throws ParseException, IOException {
//分词,搜索过程使用的分词器要和索引时使用的分词器一致
StandardAnalyzer analyzer = new StandardAnalyzer();

//查询分词器
//第一个参数:指定默认搜索的域,第二个:分词器
QueryParser queryParser = new QueryParser("description", analyzer);
//创建查询对象()
Query query = queryParser.parse("description:java");
//组合条件查询,Occur.MUST 查询条件必须满足,相当于and +(加号);Occur.SHOULD 查询条件可选,相当于or 空(不用符号);
//Occur.MUST_NOT 查询条件不能满足,相当于not非 -(减号)
// Query query = queryParser.parse("name:+java description:+spring");
//根据数字范围搜索,参数:域名,最小值,最大值,是否包含最小值,是否包含最大值
//NumericRangeQuery<Float> query = NumericRangeQuery.newFloatRange("price", 0f, 60f, true, true);
/* 组合查询
String[] fields={"name","description"};
MultiFieldQueryParser multiFieldQueryParser = new MultiFieldQueryParser(fields, analyzer);
//从name,description匹配
Query query1 = multiFieldQueryParser.parse("lucene");*/
//创建索引目录 的流对象,指定索引目录 的位置
Directory d = FSDirectory.open(new File("D:\\develop\\projects\\springMVC\\indexdata"));
//索引读取对象,自定索引读取的目录
DirectoryReader indexReader = DirectoryReader.open(d);

//创建索引搜索对象
IndexSearcher indexSearcher = new IndexSearcher(indexReader);

//执行搜索
//第一个参数:query查询对象,第二个:取出匹配度高的前100条
TopDocs topDocs = indexSearcher.search(query, 100);

//取出匹配的文档
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for (ScoreDoc scoreDoc : scoreDocs) {
//获取document的id
int docId = scoreDoc.doc;
//通过docId获取document,通过indexReader获取
Document document = indexReader.document(docId);

//取出doc中的field域的内容
//参数指定field域名
String id = document.get("id");
String name = document.get("name");
float price = Float.parseFloat(document.get("price"));
String pic = document.get("pic");

System.out.println("=====================");
System.out.println("图书的id:" + id);
System.out.println("图书的name:" + name);
System.out.println("图书的价格:" + price);
System.out.println("图书的图片:" + pic);

}
//关闭资源
indexReader.close();
}

//删除索引
@Test
public void deleteIndex() throws IOException, ParseException {
//需要一个分词器
StandardAnalyzer analyzer = new StandardAnalyzer();
//创建索引目录的流对象,指定索引目录的位置
Directory d = FSDirectory.open(new File("D:\\develop\\projects\\springMVC\\indexdata"));
//IndexWriter配置对象
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_10_4, analyzer);

//创建索引操作对象,起到承上启下的作用,通过这个可以看到需要什么东西
IndexWriter indexWriter = new IndexWriter(d, config);

//删除所有索引
//indexWriter.deleteAll();

//查询分析器
//参数:第一个--默认搜索的域,第二个--分词器
QueryParser queryParser = new QueryParser("description", analyzer);
//创建查询对象
Query query = queryParser.Query("description;java");
//删除符合查询条件的索引,删除符合查询条件的所有document
indexWriter.deleteDocuments(query);
//提交
indexWriter.commit();
//关闭资源
indexWriter.close();
}

//更新索引(更新思路:先查询、再删除、再添加。)
@Test
public void updateIndex() throws IOException {
//需要一个分词器
StandardAnalyzer analyzer = new StandardAnalyzer();
//创建索引目录的流对象,指定索引目录的位置
Directory d = FSDirectory.open(new File("D:\\develop\\projects\\springMVC\\indexdata"));
//IndexWriter配置对象
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_10_4, analyzer);

//创建索引操作对象,起到承上启下的作用,通过这个可以看到需要什么东西
IndexWriter indexWriter = new IndexWriter(d, config);

Term term = new Term("id", "1");
//创建新的document,替换id为1的document
Document doc = new Document();
//图书id,不要分词,要索引,要存储
StringField id = new St
4000
ringField("id", "1".toString(), Field.Store.YES);
//图书名称:要分词,要索引,要存储
TextField name = new TextField("name", "java编程思想第三版", Field.Store.YES);
doc.add(id);
doc.add(name);

//通过这个方法可以看出需要term和doc
// 思路:先查询,再删除,在添加(意思就是旧的被完全替换)
indexWriter.updateDocument(term,doc);

//提交
indexWriter.commit();
//关闭资源
indexWriter.close();
}

4.小参考

Field类
数据类型
Tokenized是否分词
Indexed
是否索引
Stored
是否存储
说明
StringField(FieldName, FieldValue,Store.YES))
字符串
N
Y
Y或N
这个Field用来构建一个字符串Field,但是不会进行分析,会将整个串存储在索引中,比如(订单号,姓名等)
是否存储在文档中用Store.YES或Store.NO决定
LongField(FieldName, FieldValue,Store.YES)
Long型
Y
Y
Y或N
这个Field用来构建一个Long数字型Field,进行分析和索引,比如(价格)
是否存储在文档中用Store.YES或Store.NO决定
StoredField(FieldName, FieldValue)
重载方法,支持多种类型
N
N
Y
这个Field用来构建不同类型Field
不分析,不索引,但要Field存储在文档中
TextField(FieldName, FieldValue, Store.NO)

TextField(FieldName, reader)
字符串


Y
Y
Y或N
如果是一个Reader, lucene猜测内容比较多,会采用Unstored的策略.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  lucene