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()的更多相关文章

  1. lucene中Field简析

    http://blog.csdn.net/zhaoxiao2008/article/details/14180019 先看一段lucene3代码 Document doc = new Document ...

  2. 【Lucene3.6.2入门系列】第03节_简述Lucene中常见的搜索功能

    package com.jadyer.lucene; import java.io.File; import java.io.IOException; import java.text.SimpleD ...

  3. lucene 中关于Store.YES 关于Store.NO的解释

    总算搞明白 lucene 中关于Store.YES  关于Store.NO的解释了 一直对Lucene Store.YES不太理解,网上多数的说法是存储字段,NO为不存储. 这样的解释有点郁闷:字面意 ...

  4. Lucene 中自定义排序的实现

    使用Lucene来搜索内容,搜索结果的显示顺序当然是比较重要的.Lucene中Build-in的几个排序定义在大多数情况下是不适合我们使用的.要适合自己的应用程序的场景,就只能自定义排序功能,本节我们 ...

  5. 《Lucene in Action 第二版》第4章节 学习总结 -- Lucene中的分析

    通过第四章的学习,可以了解lucene的分析过程是怎样的,并且可以学会如何使用lucene内置分析器,以及自定义分析器.下面是具体总结 1. 分析(Analysis)是什么? 在lucene中,分析就 ...

  6. Lucene中的 Query对象

    "Lucene中的 Query对象": 检 索前,需要对检索字符串进行分析,这是由queryparser来完成的.为了保证查询的正确性,最好用创建索引文件时同样的分析器. quer ...

  7. Lucene 中的Tokenizer, TokenFilter学习

      lucene中的TokenStream,TokenFilter之间关系   TokenStream是一个能够在被调用后产生语汇单元序列的类,其中有两个类型:Tokenizer和TokenFilte ...

  8. Lucene中Analyzer语句分析

    Lucene中Analyzer语句分析,利用lucene中自带的词法分析工具Analyzer,进行对句子的分析. 源代码如下: package com.test; import java.io.IOE ...

  9. lucene中FSDirectory、RAMDirectory的用法

    package com.ljq.one; import java.io.BufferedReader;import java.io.File;import java.io.FileInputStrea ...

随机推荐

  1. 开源知识库管理系统选型 centos6.4 搭建knowlededgeroot-1.0.4知识库平台

    开源知识库管理系统选型,除了使用wiki外,还有下面可选: http://www.knowledgebase-script.com/ https://github.com/lordlamer/know ...

  2. 读取Properties配置文件, 四种方式都可以得到webroot/WEB-INF/classes这个路径

    下面四种方式都可以得到webroot/WEB-INF/classes这个路径,有什么区别,哪种方式最好? String path = this.getClass().getResource(" ...

  3. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-如何配置虚拟轴 TC2

    右击NC- Configuration,然后Append Task,然后右击Axis,Append Axis   轴的类型可以分为:Continuous Axis,默认的类型,NC可以连续闭环控制该轴 ...

  4. CMake 设置Target输出目录和后缀名

    一. SET(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/../bin) 上面的语句能设置可执行文件的输出目录 在Win + VS环境下,会自动在你所设置 ...

  5. try....exception....finally

    class MyException(Exception): def __init__(self,msg): self.msg=msg def __str__(self): return self.ms ...

  6. javascript - 全局与局部作用域

    // 全局作用域 var globalNumber = 1; // 挂载在window上的变量或函数 -> 全局作用域 function InternalScope() { // 局部作用域 / ...

  7. 使用zbar 解码二维码 条形码

    #!/usr/bin/env python # coding: u8 import os import zbar import Image import urllib import uuid def ...

  8. 51单片机 | 1-Wire总线及应用实例

    ———————————————————————————————————————————— 1-Wire总线 - - - - - - - - - - - - - - - - - - - - - - - ...

  9. mongoDB 数据导出之mongoexport的用法

     http://tuozixuan.iteye.com/blog/1321994     实战代码: #mongo导出 mongoexport --port 33001  -d bsdf_soe -c ...

  10. 【MyBatis学习05】SqlMapConfig.xml文件中的配置总结

    经过上两篇博文的总结,对mybatis中的dao开发方法和流程基本掌握了,这一节主要来总结一下mybatis中的全局配置文件SqlMapConfig.xml在开发中的一些常用配置,首先看一下该全局配置 ...