lucene中的IndexWriter.setMaxFieldLength()
lucene中的IndexWriter.setMaxFieldLength()
老版本的Lucene中,IndexWriter的maxFieldLength是指一个索引中的最大的Field个数。
这个属性在Lucene2.9.0中是不可见的,对其的修改被放在相应的setMaxFieldLength(Int l)和getMaxFiedLength()中;
当索引中的Field的个数等于这个属性时,新增的任何field都会被忽略,即使对己经存在相同的Field新增内容也是不可以的。附上一个测试类(Lucene in action)
package test;
import java.io.File;
import java.io.IOException;
import junit.framework.TestCase;
import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
public class FieldLengthTest extends TestCase {
private Directory dir;
private String[] keywords = {"1", "2"};
private String[] unindexed = {"Netherlands", "Italy"};
private String[] unstored = {"Amsterdam has lots of bridges",
"Venice has lots of canals"};
private String[] text = {"Amsterdam", "Venice"};
protected void setUp() throws IOException {
String indexDir =
System.getProperty("java.io.tmpdir", "tmp") +
System.getProperty("file.separator") + "index-dir";
dir = FSDirectory.open(new File(indexDir));
}
public void testFieldSize() throws IOException {
addDocuments(dir, 10);
assertEquals(1, getHitCount("contents", "bridges"));
addDocuments(dir, 1);
assertEquals(0, getHitCount("contents", "bridges"));
}
private int getHitCount(String fieldName, String searchString)
throws IOException {
IndexSearcher searcher = new IndexSearcher(dir, true);
Term t = new Term(fieldName, searchString);
Query query = new TermQuery(t);
TopScoreDocCollector tsdc = TopScoreDocCollector.create(10, false);
searcher.search(query, tsdc);
ScoreDoc[] hits = tsdc.topDocs().scoreDocs;
int hitCount = hits.length;
searcher.close();
return hitCount;
}
private void addDocuments(Directory dir, int maxFieldLength)
throws IOException {
IndexWriter writer = new IndexWriter(dir, new SimpleAnalyzer(),
true, IndexWriter.MaxFieldLength.LIMITED);
writer.setMaxFieldLength(maxFieldLength);
for (int i = 0; i < keywords.length; i++) {
Document doc = new Document();
doc.add(new Field("contents", unstored[i], Field.Store.YES, Field.Index.ANALYZED));
//doc.add(new Field("contents", unstored[i], Field.Store.NO, Field.Index.ANALYZED));
doc.add(new Field("country", unindexed[i], Field.Store.YES, Field.Index.NO));
doc.add(new Field("contents", unstored[i], Field.Store.NO, Field.Index.ANALYZED));
doc.add(new Field("city", text[i], Field.Store.YES, Field.Index.ANALYZED));
writer.addDocument(doc);
}
writer.optimize();
writer.close();
}
}
(转自:http://blog.sina.com.cn/s/blog_49b531af0100it66.html)
lucene中的IndexWriter.setMaxFieldLength()的更多相关文章
- lucene中Field简析
http://blog.csdn.net/zhaoxiao2008/article/details/14180019 先看一段lucene3代码 Document doc = new Document ...
- 【Lucene3.6.2入门系列】第03节_简述Lucene中常见的搜索功能
package com.jadyer.lucene; import java.io.File; import java.io.IOException; import java.text.SimpleD ...
- lucene 中关于Store.YES 关于Store.NO的解释
总算搞明白 lucene 中关于Store.YES 关于Store.NO的解释了 一直对Lucene Store.YES不太理解,网上多数的说法是存储字段,NO为不存储. 这样的解释有点郁闷:字面意 ...
- Lucene 中自定义排序的实现
使用Lucene来搜索内容,搜索结果的显示顺序当然是比较重要的.Lucene中Build-in的几个排序定义在大多数情况下是不适合我们使用的.要适合自己的应用程序的场景,就只能自定义排序功能,本节我们 ...
- 《Lucene in Action 第二版》第4章节 学习总结 -- Lucene中的分析
通过第四章的学习,可以了解lucene的分析过程是怎样的,并且可以学会如何使用lucene内置分析器,以及自定义分析器.下面是具体总结 1. 分析(Analysis)是什么? 在lucene中,分析就 ...
- Lucene中的 Query对象
"Lucene中的 Query对象": 检 索前,需要对检索字符串进行分析,这是由queryparser来完成的.为了保证查询的正确性,最好用创建索引文件时同样的分析器. quer ...
- Lucene 中的Tokenizer, TokenFilter学习
lucene中的TokenStream,TokenFilter之间关系 TokenStream是一个能够在被调用后产生语汇单元序列的类,其中有两个类型:Tokenizer和TokenFilte ...
- Lucene中Analyzer语句分析
Lucene中Analyzer语句分析,利用lucene中自带的词法分析工具Analyzer,进行对句子的分析. 源代码如下: package com.test; import java.io.IOE ...
- lucene中FSDirectory、RAMDirectory的用法
package com.ljq.one; import java.io.BufferedReader;import java.io.File;import java.io.FileInputStrea ...
随机推荐
- 【机器学习 & 数据挖掘 通俗介绍】
如何向小白介绍何谓机器学习和数据挖掘?买回芒果他就懂了 JasonZheng • 2013-01-07 22:18 买芒果 嘴馋的你想吃芒果了,于是你走到水果摊,挑了几个让老板过过秤,然后你再根据 ...
- 在Dell XPS 13安装WIN10和ubuntu双系统
新入了Dell的XPS 13超级本,之所以买这个本子,就是看中它轻便且续航持久.这款本子也是为数不多的能够和苹果的13'' mac book air一较高下的本子.在重量上,占地面积和综合性价比上,还 ...
- Selenium webdriver Java 查找元素
1.简单查找 By ID: WebElement element=driver.findElement(By.id("userId")); By Name:WebElement e ...
- ssh之<context:component-scan base-package="com.xx" />
<context:component-scan/> 配置项不但启用了对类包进行扫描以实施注释驱动 Bean 定义的功能, 同时还启用了注释驱动自动注入的功能 ( 即还隐式地在内部注册了 A ...
- POJ1037 A decorative fence 【动态规划】
A decorative fence Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6489 Accepted: 236 ...
- openerp config file
[options] addons_path = /bin/openerp/addonsadmin_passwd = admincsv_internal_sep = , db_host = False ...
- VMware中Nat方式设置静态IP
一.共享无线连接或本地连接,给VMnet8. 在网络配置中.选着无线连接,右键属性.共享. 这里默认给虚拟网卡VMnet8.分配了IP:192.168.137.1. 二,在VMware中配置VMnet ...
- Linux 平台如何查看某个进程的线程数?
Linux 平台如何查看某个进程的线程数? 三种方法:1. 使用top命令,具体用法是 top -H 加上这个选项,top的每一行就不是显示一个进程,而是一个线程. 2. 使用ps命令,具体用法是 ...
- android:ellipsize实现跑马灯效果总结
最近无意间看到了涉及到跑马灯效果的代码,于是在网上查阅了很多资料,在这里对自己看的一些文章进行一下总结,顺便加上自己的一些体会. 让我们一步步逐渐向下. 首先我们要实现走马灯这样一个效果,通常来说都是 ...
- poj 2601 Simple calculations
Simple calculations Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6559 Accepted: 32 ...