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. eclipse No projects are found to import

    导入报:No projects are found to import 新建同名项目,然后删掉 然后:右键项目 根据需要创建资源目录: 最后复制包文件夹分别到这两个资源文件夹里:

  2. [Typescript] Improve Readability with TypeScript Numeric Separators when working with Large Numbers

    When looking at large numbers in code (such as 1800000) it’s oftentimes difficult for the human eye ...

  3. B11:解释器模式 Iterpreter

    给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言中的句子. UML: 示例代码: abstract class Expression { abstract pub ...

  4. Android调试方法总结

    Android程序调试过程中,通常需要在控制台或者AVD中弹出相关信息作为调试使用,以下是调试使用中会用到的Log类和Toast类的使用方法: 1.Toast Toast是在AVD上显示信息的一种机制 ...

  5. 在OpenERP报表中使用selection 类型字段

    OpenERP 在报表的创作中始终有一个麻烦,那就是在报表中通过对象导航的方式获取的 selection 字段只能获取到该字段的 key 而不能获取对应的用户友好的描述文本. 举个具体的例子:销售单的 ...

  6. 阿里云OSS设置跨域访问

    OSS 提供 HTML5 协议中的跨域资源共享 CORS 设置,帮助您实现跨域访问.当 OSS 收到一个跨域请求(或者 OPTIONS 请求)时,会读取存储空间对应的 CORS 规则,然后进行相应的权 ...

  7. PHP-深入理解Opcode缓存

    1.什么是opcode缓存? 当解释器完成对脚本代码的分析后,便将它们生成可以直接运行的中间代码,也称为操作码(Operate Code,opcode).Opcode cache的目地是避免重复编译, ...

  8. 改变Fragment的默认动画

    FragmentTransaction ft = getFragmentManager().beginTransaction(); //设置进入退出动画 ft.setCustomAnimations( ...

  9. charles抓包工具使用指南

    前言 移动APP抓包 PC端抓包 查看模式 其他功能 问题汇总 1. 前言: Charles是一款抓包修改工具,相比起burp,charles具有界面简单直观,易于上手,数据请求控制容易,修改简单,抓 ...

  10. [原创]如何让freeswitch转发客户端自定义的INFO消息

    如何让freeswitch转发客户端自定义的INFO消息 英文概述: this article is about how to configure freeswitch to forward self ...