当前位置:   article > 正文

自己动手写搜索引擎(常搜吧历程三#搜索#)(Java、Lucene、hadoop)_81xamu.top

81xamu.top

Lucene的常用检索类

1、IndexSercher:检索操作的核心组件,用于对IndexWriter创建的索引执行,只读的检索操作,工作模式为接受Query对象而返回ScoreDoc对象。

2、Term:检索的基本单元,标示检索的字段名称和检索对象的值,如Term("title", "lucene")。即表示在title字段中搜索关键词lucene。

3、Query:表示查询的抽象类,由相应的Term来标识。

4、TermQuery:最基本的查询类型,用于匹配含有制定值字段的文档。

5、TopDoc:保存查询结果的类。

6、ScoreDoc(Hits):用来装载搜索结果文档队列指针的数组容器。


我们先新建一个索引类:

  1. package com.qianyan.luceneIndex;
  2. import java.io.IOException;
  3. import org.apache.lucene.analysis.Analyzer;
  4. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  5. import org.apache.lucene.document.Document;
  6. import org.apache.lucene.document.Field;
  7. import org.apache.lucene.index.IndexWriter;
  8. import org.apache.lucene.store.Directory;
  9. import org.apache.lucene.store.FSDirectory;
  10. public class IndexTest {
  11. public static void main(String[] args) throws IOException{
  12. String[] ids = {"1", "2", "3", "4"};
  13. String[] names = {"zhangsan", "lisi", "wangwu", "zhaoliu"};
  14. String[] addresses = {"shanghai", "beijing", "guangzhou", "nanjing"};
  15. String[] birthdays = {"19820720", "19840203", "19770409", "19830130"};
  16. Analyzer analyzer = new StandardAnalyzer();
  17. String indexDir = "E:/luceneindex";
  18. Directory dir = FSDirectory.getDirectory(indexDir);
  19. //true 表示创建或覆盖当前索引;false 表示对当前索引进行追加
  20. //Default value is 128
  21. IndexWriter writer = new IndexWriter(dir, analyzer, true, IndexWriter.MaxFieldLength.LIMITED);
  22. for(int i = 0; i < ids.length; i++){
  23. Document document = new Document();
  24. document.add(new Field("id", ids[i], Field.Store.YES, Field.Index.ANALYZED));
  25. document.add(new Field("name", names[i], Field.Store.YES, Field.Index.ANALYZED));
  26. document.add(new Field("address", addresses[i], Field.Store.YES, Field.Index.ANALYZED));
  27. document.add(new Field("birthday", birthdays[i], Field.Store.YES, Field.Index.ANALYZED));
  28. writer.addDocument(document);
  29. }
  30. writer.optimize();
  31. writer.close();
  32. }
  33. }

下面来看简答的检索类:

  1. package com.qianyan.lucene;
  2. import java.io.IOException;
  3. import org.apache.lucene.document.Document;
  4. import org.apache.lucene.index.Term;
  5. import org.apache.lucene.search.BooleanClause;
  6. import org.apache.lucene.search.BooleanQuery;
  7. import org.apache.lucene.search.IndexSearcher;
  8. import org.apache.lucene.search.ScoreDoc;
  9. import org.apache.lucene.search.TermQuery;
  10. import org.apache.lucene.search.FuzzyQuery;
  11. import org.apache.lucene.search.RangeQuery;
  12. import org.apache.lucene.search.PrefixQuery;
  13. import org.apache.lucene.search.TopDocs;
  14. import org.apache.lucene.search.WildcardQuery;
  15. import org.apache.lucene.store.Directory;
  16. import org.apache.lucene.store.FSDirectory;
  17. public class TestSeacher {
  18. public static void main(String[] args) throws IOException {
  19. String indexDir = "E:/luceneindex";
  20. Directory dir = FSDirectory.getDirectory(indexDir);
  21. IndexSearcher searcher = new IndexSearcher(dir);
  22. ScoreDoc[] hits = null;
  23. Term term = new Term("id", "2");
  24. TermQuery query = new TermQuery(term);
  25. TopDocs topDocs = searcher.search(query, 5);
  26. /* 范围检索: 19820720 - 19830130 。 true表示包含首尾
  27. Term beginTerm = new Term("bithday", "19820720");
  28. Term endTerm = new Term("bithday", "19830130");
  29. RangeQuery rangeQuery = new RangeQuery(beginTerm, endTerm, true);
  30. TopDocs topDocs = searcher.search(rangeQuery, 5);
  31. */
  32. /* 前缀检索:
  33. Term term = new Term("name", "z");
  34. PrefixQuery preQuery = new PrefixQuery(term);
  35. TopDocs topDocs = searcher.search(preQuery, 5);
  36. */
  37. /* 模糊查询:例如查找name为zhangsan的数据,那么name为zhangsun、zhangsin也会被查出来
  38. Term term = new Term("name", "zhangsan");
  39. FuzzyQuery fuzzyQuery = new FuzzyQuery(term);
  40. TopDocs topDocs = searcher.search(fuzzyQuery, 5);
  41. */
  42. /* 匹配通配符: * 任何条件 ?占位符
  43. Term term = new Term("name", "*g??");
  44. WildcardQuery wildcardQuery = new WildcardQuery(term);
  45. TopDocs topDocs = searcher.search(wildcardQuery, 5);
  46. */
  47. /* 多条件联合查询
  48. Term nterm = new Term("name", "*g??");
  49. WildcardQuery wildcardQuery = new WildcardQuery(nterm);
  50. Term aterm = new Term("address", "nanjing");
  51. TermQuery termQuery = new TermQuery(aterm);
  52. BooleanQuery query = new BooleanQuery();
  53. query.add(wildcardQuery, BooleanClause.Occur.MUST); //should表示"或" must表示"必须"
  54. query.add(termQuery, BooleanClause.Occur.MUST);
  55. TopDocs topDocs = searcher.search(query, 10);
  56. */
  57. hits = topDocs.scoreDocs;
  58. for(int i = 0; i < hits.length; i++){
  59. Document doc = searcher.doc(hits[i].doc);
  60. //System.out.println(hits[i].score);
  61. System.out.print(doc.get("id") + " ");
  62. System.out.print(doc.get("name") + " ");
  63. System.out.print(doc.get("address") + " ");
  64. System.out.println(doc.get("birthday") + " ");
  65. }
  66. searcher.close();
  67. dir.close();
  68. }
  69. }


下面我们来看一个全文索引的案例,data.txt 见文章最下面。首先我们建立对文章的索引:

  1. package com.qianyan.lucene;
  2. import java.io.File;
  3. import java.io.FileReader;
  4. import java.io.IOException;
  5. import org.apache.lucene.analysis.Analyzer;
  6. import org.apache.lucene.analysis.standard.StandardAnalyzer;
  7. import org.apache.lucene.document.Document;
  8. import org.apache.lucene.document.Field;
  9. import org.apache.lucene.index.IndexWriter;
  10. import org.apache.lucene.store.Directory;
  11. import org.apache.lucene.store.FSDirectory;
  12. public class TestFileReaderForIndex{
  13. public static void main(String[] args) throws IOException{
  14. File file = new File("E:/data.txt");
  15. FileReader fRead = new FileReader(file);
  16. char[] chs = new char[60000];
  17. fRead.read(chs);
  18. String strtemp = new String(chs);
  19. String[] strs = strtemp.split("Database: Compendex");
  20. System.out.println(strs.length);
  21. for(int i = 0; i < strs.length; i++)
  22. strs[i] = strs[i].trim();
  23. Analyzer analyzer = new StandardAnalyzer();
  24. String indexDir = "E:/luceneindex";
  25. Directory dir = FSDirectory.getDirectory(indexDir);
  26. IndexWriter writer = new IndexWriter(dir, analyzer, false, IndexWriter.MaxFieldLength.UNLIMITED);
  27. for(int i = 0; i < strs.length; i++){
  28. Document document = new Document();
  29. document.add(new Field("contents", strs[i], Field.Store.YES, Field.Index.ANALYZED));
  30. writer.addDocument(document);
  31. }
  32. writer.optimize();
  33. writer.close();
  34. dir.close();
  35. System.out.println("index ok!");
  36. }
  37. }

对上述追加索引进行简单搜索:

  1. package com.qianyan.lucene;
  2. import java.io.IOException;
  3. import org.apache.lucene.document.Document;
  4. import org.apache.lucene.index.Term;
  5. import org.apache.lucene.search.IndexSearcher;
  6. import org.apache.lucene.search.ScoreDoc;
  7. import org.apache.lucene.search.TermQuery;
  8. import org.apache.lucene.search.TopDocs;
  9. import org.apache.lucene.store.Directory;
  10. import org.apache.lucene.store.FSDirectory;
  11. public class TestSeacher2 {
  12. public static void main(String[] args) throws IOException {
  13. String indexDir = "E:/luceneindex";
  14. Directory dir = FSDirectory.getDirectory(indexDir);
  15. IndexSearcher searcher = new IndexSearcher(dir);
  16. ScoreDoc[] hits = null;
  17. Term term = new Term("contents", "ontology");
  18. TermQuery query = new TermQuery(term);
  19. TopDocs topDocs = searcher.search(query, 126);
  20. hits = topDocs.scoreDocs;
  21. for(int i = 0; i < hits.length; i++){
  22. Document doc = searcher.doc(hits[i].doc);
  23. System.out.print(hits[i].score);
  24. System.out.println(doc.get("contents"));
  25. }
  26. searcher.close();
  27. dir.close();
  28. }
  29. }

好了,简单的检索方式就介绍这些 。


data.txt 内容如下 :


1. Modeling the adsorption of CD(II) onto Muloorina illite and related clay minerals
Lackovic, Kurt (La Trobe University, P.O. Box 199, Bendigo, Vic. 3552, Australia)  Angove, Michael J.  Wells, John D.  Johnson, Bruce B. Source: Journal of Colloid and Interface Science, v 257, n 1, p 31-40, 2003
Database: Compendex
 
 
 
 2.  Experimental Study of the Adsorption of an Ionic Liquid onto Bacterial and Mineral Surfaces
Gorman-Lewis, Drew J. (Civ. Eng. and Geological Sciences, University of Notre Dame, Notre Dame, IN 46556-0767, United States)  Fein, Jeremy B. Source: Environmental Science and Technology, v 38, n 8, p 2491-2495, April 15, 2004
Database: Compendex
 
 
 
 3.  Grafting of hyperbranched polymers onto ultrafine silica: Postgraft polymerization of vinyl monomers initiated by pendant initiating groups of polymer chains grafted onto the surface
Hayashi, Shinji (Grad. Sch. of Science and Technology, Niigata Univ., 8050, I., Niigata, Japan)  Fujiki, Kazuhiro  Tsubokawa, Norio Source: Reactive and Functional Polymers, v 46, n 2, p 193-201, December 2000
Database: Compendex
 
 
 
 4.  The influence of pH, electrolyte type, and surface coating on arsenic(V) adsorption onto kaolinites
Cornu, Sophie (Unité de Sciences du Sol, INRA d'Orléans, av. de la Pomme de pin, Ardon, 45166 Olivet Cedex, France)  Breeze, Dominique  Saada, Alain  Baranger, Philippe Source: Soil Science Society of America Journal, v 67, n 4, p 1127-1132, July/August 2003
Database: Compendex
 
 
 
 5.  Adsorption behavior of statherin and a statherin peptide onto hydroxyapatite and silica surfaces by in situ ellipsometry
Santos, Olga (Biomedical Laboratory Science and Biomedical Technology, Faculty of Health and Society, Malm? University, SE-20506 Malm?, Sweden)  Kosoric, Jelena  Hector, Mark Prichard  Anderson, Paul  Lindh, Liselott Source: Journal of Colloid and Interface Science, v 318, n 2, p 175-182, Febrary 15, 2008
Database: Compendex
 
 
 
 6.  Sorption of surfactant used in CO2 flooding onto five minerals and three porous media
Grigg, R.B. (SPE, New Mexico Recovery Research Center)  Bai, B. Source: Proceedings - SPE International Symposium on Oilfield Chemistry, p 331-342, 2005, SPE International Symposium on Oilfield Chemistry Proceedings
Database: Compendex
 
 
 
 7.  Influence of charge density, sulfate group position and molecular mass on adsorption of chondroitin sulfate onto coral
Volpi, Nicola (Department of Animal Biology, Biological Chemistry, University of Modena and Reggio Emilia, Via Campi 213/d, 41100 Modena, Italy) Source: Biomaterials, v 23, n 14, p 3015-3022, 2002
Database: Compendex
 
 
 
 8.  Kinetic consequences of carbocationic grafting and blocking from and onto
Ivan, Bela (Univ of Akron, United States) Source: Polymer Bulletin, v 20, n 4, p 365-372, Oct
Database: Compendex
 
 
 
 9.  Assemblies of concanavalin A onto carboxymethylcellulose
Castro, Lizandra B.R. (Instituto de Química, Universidade de S?o Paulo, Av. Prof. Lineu Prestes 748, 05508-900, S?o Paulo, Brazil)  Petri, Denise F.S. Source: Journal of Nanoscience and Nanotechnology, v 5, n 12, p 2063-2069, December 2005
Database: Compendex
 
 
 
 10.  Surface grafting of polymers onto glass plate: Polymerization of vinyl monomers initiated by initiating groups introduced onto the surface
Tsubokawa, Norio (Niigata Univ, Niigata, Japan)  Satoh, Masayoshi Source: Journal of Applied Polymer Science, v 65, n 11, p 2165-2172, Sep 12
Database: Compendex
 
 
 
 11.  Photografting of vinyl polymers onto ultrafine inorganic particles: photopolymerization of vinyl monomers initiated by azo groups introduced onto these surfaces
Tsubokawa, Norio (Niigata Univ, Niigata, Japan)  Shirai, Yukio  Tsuchida, Hideyo  Handa, Satoshi Source: Journal of Polymer Science, Part A: Polymer Chemistry, v 32, n 12, p 2327-2332, Sept
Database: Compendex
 
 
 
 12.  Graft polymerization of methyl methacrylate initiated by pendant azo groups introduced onto γ-poly (glutamic acid)
Tsubokawa, Norio (Niigata Univ, Niigita, Japan)  Inagaki, Masatoshi  Endo, Takeshi Source: Journal of Polymer Science, Part A: Polymer Chemistry, v 31, n 2, p 563-568, Feb
Database: Compendex
 
 
 
 13.  The sorpt
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/162818
推荐阅读
相关标签
  

闽ICP备14008679号