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. java源码阅读LinkedBlockingQueue

    1类签名与简介 public class LinkedBlockingQueue<E> extends AbstractQueue<E> implements Blocking ...

  2. 集合—ArrayList

    ArrayList也叫作数组列表 public static void main(String[] args) { List list1 = new ArrayList<String>() ...

  3. Android实现批量照片上传至server,拍照或者从相冊选择

    近期因为项目需求,须要完毕批量照片上传,折腾了一段时间,最终完毕了,达到了例如以下效果 主界面主要有GridView组成和button组成,当按下一个格点时,会调用相机或者相冊,拍照或者选择相冊照片, ...

  4. 【Qt编程】基于Qt的词典开发系列&lt;八&gt;--用户登录及API调用的实现

    在上一篇文章<调用网络API>中,我仅仅讲述了怎样直观的使用API接口以及调用API后返回的结果,本文则从程序实现的角度来实现API的调用.当然本程序的实现也是借助于扇贝网的API接口文档 ...

  5. 在 Word 中的受支持的区域设置标识符的列表

    这篇文章包含当前可用的区域设置 ID (LCID) 语言代码的列表.   概要 这篇文章包含当前可用的区域设置 ID (LCID) 语言代码的列表. 区域设置 ID (LCID): 由 Microso ...

  6. Invalid code signing entitlements. Your application bundle's signature contains

    http://code4app.com/requirement/54128041933bf0e0308b5204 Invalid code signing entitlements. Your app ...

  7. ant-design table 分页(tableProps)

    1.布局 <Table dataSource={this.state.tableDetailList} scroll={{ y: '200px' }} style={{tableLayout: ...

  8. 利用Perlin nosie 完毕(PS 滤镜—— 分成云彩)

    %%%% Cloud %%%% 利用perlin noise生成云彩 clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image ...

  9. S2S3H4 整合代码示例

    主要代码列举:   web.xml <?xml version="1.0" encoding="UTF-8"?> <web-app versi ...

  10. Failed to Stop or Restart Nginx Server Through Serevice Command(nginx进程不能停止重启)

    Many people are accustomed to start a Nginx web server through init scripts and then they can contro ...