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源码阅读Hashtable

    1类签名与注释 public class Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>, C ...

  2. iOS开发中经常使用的Xcode插件

    1.全能搜索家CodePilot 2.0 你要找的是文件?是目录?是代码?Never Mind,CMD+SHIFT+X调出CodePilot,输入不论什么你想到搜的东西吧! 想搜appFinishLa ...

  3. 使用pip安装tensorflow 0.80,python 使用tensorflow 0.80遇到的问题及处理方法

    http://blog.csdn.net/levy_cui/article/details/51251095 1.python 版本切换到2.7 推荐使用pythonbrew,http://blog. ...

  4. java.net.ConnectException: failed to connect to /10.0.2.2 (port 80): connect

    在使用GENYMOTION作为Android程序调试模拟器连接web服务器时,报了:java.net.ConnectException: failed to connect to /10.0.2.2 ...

  5. react-native + teaset(Drawer)实现侧边菜单

    1.代码 /** * 购物车 */ import React, {Component} from 'react'; import { View, Image, } from 'react-native ...

  6. 【高德地图API】从零開始学高德JS API(二)地图控件与插件——測距、圆形编辑器、鼠标工具、地图类型切换、鹰眼鱼骨

    不管是控件还是插件,都是在一级API接口的基础上,进行二次开发,封装的一系列更加便于开发人员使用.降低开发人员工作量的二级API接口.除了官方通用的鱼骨.鹰眼控件,还有大量官方开发的地图插件,相似谷歌 ...

  7. 【Datastage】NULL VALUE TO A NOT NULL VALUE

    使用ds是报错如上图: 造成这个问题的原因是: 在写SQL时候没有给字段别名与DS中对应一致 例如有下表: CREATE TABLE DataInfo( ID_1 ), ID_2 ) ) CREATE ...

  8. blog url.txt

    java 回收机制与虚拟机http://www.cnblogs.com/zhanglei93/p/6636831.html Java 编程经验(牛人写的)  上/下http://www.thinksa ...

  9. 《Mining the Web:Transforming Customer Data into Customer Value》读后札记

    <Mining the Web:Transforming Customer Data into Customer Value> <Web数据挖掘:将客户数据转化为客户价值> — ...

  10. web本地存储localStorage和sessionStorage

    用谷歌浏览器调试网页程序时候发现有一个这个栏目 记录本地存储的相关信息(cookie,sessionStorage,LocalStorage等)的存储信息 1.LocalStorage localst ...