Lucene Simple Summary

Source: Internet
Author: User
Tags create directory

Luceneapidocument

Documents: Document object, which is an original data

Document Number Document Content
1 The father of Google Maps job-hopping Facebook
2 The father of Google Maps joins Facebook
3 Google Maps founder Russ leaves Google to join Facebook
4 Google Maps father-to-be Facebook and wave project cancellation related
5 Google Maps father Lars joins social networking site Facebook

= = A record is a document,document each field is a field==

Field

Create an index
  Private final static file Index_file = new file ("E:\\developtools\\indexdir");p ublic static void Indexcreate ( Document DOC) throws Exception {//Create directory object, specify the location of the index library; fsdirectory file system; ramdirectory Memory Directory dir = Fsdirectory.ope    N (index_file);    To create a word breaker Object Analyzer Analyzer = new Ikanalyzer (); Create an index writer configuration object, first parameter version version.latest, second parameter word breaker indexwriterconfig conf = new Indexwriterconfig (version.latest, analyzer    );    Create an index writer IndexWriter indexwriter = new IndexWriter (dir, conf);    Writes a Document Object Indexwriter.adddocument (DOC) to the index library;    Submit Indexwriter.commit (); Close Indexwriter.close ();}  
@Testpublic void createTest() throws Exception {    //    Document doc = new Document();    doc.add(new LongField("id", 1, Store.YES));    doc.add(new TextField("title", "谷歌地图之父跳槽FaceBook", Store.YES));    doc.add(new TextField("context", "据国外媒体报道,曾先后负责谷歌地图和Wave开发工作的拉斯·拉斯姆森(Lars Rasmussen)已经离开谷歌,并将加盟Facebook。", Store.YES));    indexCreate(doc);}
Query index
public static void Indexsearcher (query query, Integer N) throws IOException {//Initialize index library object Directory dir = Fsdirector    Y.open (Index_file);    Index reading tool Indexreader Indexreader = Directoryreader.open (dir);       Index Search Object Indexsearcher indexseracher = new Indexsearcher (Indexreader);    Performs a search operation that returns a value of Topdocs topdocs topdocs = indexseracher.search (query, n);    The total number of records matching the search criteria System.out.println ("Altogether hit:" + topdocs.totalhits + "bar data");    Get the score document array object, score Document object contains score and document number scoredoc[] Scoredocs = Topdocs.scoredocs;        for (Scoredoc scoredoc:scoredocs) {int docID = Scoredoc.doc;        FLOAT score = Scoredoc.score;        SYSTEM.OUT.PRINTLN ("Document number:" + DocID);        System.out.println ("Document score:" + score);        Gets the document object, reading the tool through the index. Document document = Indexreader.document (DocID);        SYSTEM.OUT.PRINTLN ("ID:" + document.get ("id"));        System.out.println ("title:" + document.get ("title"));   SYSTEM.OUT.PRINTLN ("Context:" + document.get ("context")); } indexreader.close ();} 
@Testpublic void searchTest() throws Exception {    //单一字段的查询解析器    // 创建查询解析器对象    QueryParser parser = new QueryParser("title", new IKAnalyzer());    // 创建查询对象    Query query = parser.parse("谷歌");    //根据Query搜索,返回评分最高的n条记录    indexSearcher(query, 10);        /*多字段的查询解析器    MultiFieldQueryParser parser = new MultiFieldQueryParser(new String[]{"id","title"}, new IKAnalyzer());    Query query = parser.parse("1");*/}
Various other query methods
//词条查询,查询条件必须是最小粒度不可再分割的内容Query query = new TermQuery(new Term("title", "谷歌"));//通配符查询, ?:匹配一个字符, *:匹配多个字符Query query = new WildcardQuery(new Term("title", "*歌*"));//模糊查询, 参数:1-词条,查询字段及关键词,关键词允许写错;2-允许写错的最大编辑距离,并且不能大于2(0~2)Query query = new FuzzyQuery(new Term("title", "facebool"), 1);//数值范围查询,查询非String类型的数据或者说是一些继承Numeric类的对象的查询,参数1-字段;2-最小值;3-最大值;4-是否包含最小值;5-是否包含最大值Query query = NumericRangeQuery.newLongRange("id", 2l, 4l, true, true);//组合查询, 交集: Occur.MUST + Occur.MUST, 并集:Occur.SHOULD + Occur.SHOULD, 非:Occur.MUST_NOTBooleanQuery query = new BooleanQuery();Query query1 = NumericRangeQuery.newLongRange("id", 2l, 4l, true, true);Query query2 = new WildcardQuery(new Term("title", "*歌*"));query.add(query1, Occur.SHOULD);query.add(query2, Occur.SHOULD);
modifying indexes
//本质先删除再添加,先删除所有满足条件的文档,再创建文档, 因此,修改索引通常要根据唯一字段public static void indexUpdate(Term term, Document doc) throws IOException {    Directory dir = FSDirectory.open(INDEX_FILE);    Analyzer analyzer = new IKAnalyzer();    IndexWriterConfig conf = new IndexWriterConfig(Version.LATEST, analyzer);    IndexWriter indexWriter = new IndexWriter(dir, conf);    indexWriter.updateDocument(term, doc);    indexWriter.commit();    indexWriter.close();    }
@Testpublic void updateTest() throws Exception {    Term term = new Term("title", "facebook");    Document doc = new Document();    doc.add(new LongField("id", 1L, Store.YES));    doc.add(new TextField("title", "谷歌地图之父跳槽FaceBook", Store.YES));    doc.add(new TextField("context", "河马程序员加盟FaceBook", Store.YES));    indexUpdate(term, doc);}
Delete Index
// 执行删除操作(根据词条),要求id字段必须是字符串类型public static void indexDelete(Term term) throws IOException {    Directory dir = FSDirectory.open(INDEX_FILE);    Analyzer analyzer = new IKAnalyzer();    IndexWriterConfig conf = new IndexWriterConfig(Version.LATEST, analyzer);    IndexWriter indexWriter = new IndexWriter(dir, conf);    indexWriter.deleteDocuments(term);    indexWriter.commit();    indexWriter.close();    }public static void indexDeleteAll() throws IOException {    Directory dir = FSDirectory.open(INDEX_FILE);    Analyzer analyzer = new IKAnalyzer();    IndexWriterConfig conf = new IndexWriterConfig(Version.LATEST, analyzer);    IndexWriter indexWriter = new IndexWriter(dir, conf);    indexWriter.deleteAll();    indexWriter.commit();    indexWriter.close();    }
@Testpublic void deleteTest() throws Exception {    /*     * Term term = new Term("context", "facebook"); indexDelete(term);     */    indexDeleteAll();}

Lucene Simple Summary

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.