Use IKAnalyzer word and index maintenance

1. Default standard analyzer analyzer all classes are ultimately inherited Analyzer

1.1 AnalyzerTest writing class

 // default standard analyzer
         // 1. Create a target Analyzer 
        Analyzer Analyzer = new new StandardAnalyzer ();
         // 2. calls Analyzer object tokenStream method to get TokenStream object that contains all the segmentation result 
        TokenStream tokenStream = analyzer.tokenStream ( "", "spring springmvc mysql database freamework mybatis Oh" );
         // 3. tokenStyream to set a pointer to the object, where the current to hate on which a word 
        charTermAttribute charTermAttribute = tokenStream.addAttribute (charTermAttribute. class );
         // 4. the method of tokenStream call reset object, top pointer, no calls will be given 
        tokenStream.reset ();
         // 5. the use of a while loop, to get the word list results incrementToken method returns true if completion of the reading is not representative of false means reading is completed 
        while (tokenStream.incrementToken()){
            System.out.println(charTermAttribute.toString());
        }
        //6.关闭
        tokenStream.close();
    }

 

1.2 View segmentation effect as can be seen all the data is the word but the Chinese data into a word by word so we can carry on using the word IKAnalyzer

 

 

 

1.3 Use IKAnalyzer segment words

// use IKAnalyzer word 
        Analyzer Analyzer = new new IKAnalyzer ();
         // 2. calls Analyzer object tokenStream method to get TokenStream object that contains all the segmentation result 
        TokenStream tokenStream = analyzer.tokenStream ( "", "spring springmvc mysql database freamework mybatis huh " );
         // 3. tokenStyream to set a pointer to the object, where the current to hate on which a word 
        CharTermAttribute charTermAttribute = tokenStream.addAttribute (CharTermAttribute. class );
         // 4. the method tokenStream call reset object, top pointer, no calls will be given 
        tokenStream.reset ();
         // 5. the use of a while loop, to get the word list results incrementToken method returns false if the reading is not representative of true representative of completion of the reading is completed 
        while (tokenStream.incrementToken ( )) {
            System.out.println(charTermAttribute.toString());
        }
        //6.关闭
        tokenStream.close();
    }

 

1.4 View segmentation effect than the figure for self-experience

 

 

 

2. Maintenance index: add, delete, modify,

2.1 Add index

 // add index 
    @Test
     public  void the createDocument () throws IOException { 


        // 1. Create a IndexWriter object parameters: the index repository location two parameters: Match 
        IndexWriter IndexWriter = new new IndexWriter (FSDirectory.open ( new new File ( "C: the Users \\ Desktop Administrator \\ \\ \\ index ") toPath ()),. new new IndexWriterConfig ( new new IKAnalyzer ()));
         // create a document object 
        the document the document = new new the document (); 
        document.add ( new new TextField ( "fieldName", "haha.text" , Field.Store.YES)); 
        document.add ( new newStoredField ( "The fieldPath", "G: //haha.text" )); 
        document.add ( new new LongPoint ( "FieldSize", 123 )); 
        document.add ( new new StoredField ( "FieldSize", 123 )); 
        Document. the Add ( new new TextField ( "fieldName", "solutions ojdbc14 and ikanalyzer of maven can not find the manual release oJdbc14 to maven repository, manually publish ikanalyzer to maven, while this tutorial is for all the jar packages downloads IKAnalyzer released in conjunction with the use of Lucene and using the example of a simple performance tests 11-26 Reads 1890 IKAnalyzer alone is an open source JAVA based language " , Field.Store.YES));
         // create the index, add documents to which the index library 
        indexWriter.addDocument (the document);
         / / off 
        indexWriter.close (); 
    }

 

2.2 View performance index database there are 15 documents so executed after 16

 

 

 

2.3 Delete all indexes

  //删除所有索引
    @Test
    public void delDocument() throws IOException {
    //1.创建IndexWriter对象 参数一:索引库位置   参数二: 指定配置
            IndexWriter indexWriter=new IndexWriter(FSDirectory.open(new File("C:\\Users\\Administrator\\Desktop\\index").toPath()), new IndexWriterConfig(new IKAnalyzer()));
    //删除索引

        indexWriter.deleteAll();
    //关闭
        indexWriter.close();
    }

 

2.4 查看效果  当前已无索引

 

 

 

2.5 根据域和关键词删除索引   删除前先把索引文档恢复成15个文档

 @Test
    public void delDocumentByTerm() throws IOException {
        //1.创建IndexWriter对象 参数一:索引库位置   参数二: 指定配置
        IndexWriter indexWriter=new IndexWriter(FSDirectory.open(new File("C:\\Users\\Administrator\\Desktop\\index").toPath()), new IndexWriterConfig(new IKAnalyzer()));
       //定义一个删除条件,定义一个查询对象
        Query query=new TermQuery(new Term("fieldName","全文检索"));

        //删除索引
        indexWriter.deleteDocuments(query);
        //关闭
        indexWriter.close();
    }

 

2.6 首先我们删除前查看带有全文检索的文档名称又几个  我么查询到带有全文检索的文档名称又两个

 

 

 

2.7 执行删除  查询效果  现在文档数变为13

 

 

 

2.8 修改索引   修改首先将匹配到的文档删除后再添加一个

//修改索引,修改fieldName域中关键词匹配到全文索引的文档
    @Test
    public void updaDocument() throws IOException {
        //1.创建IndexWriter对象 参数一:索引库位置   参数二: 指定配置
        IndexWriter indexWriter=new IndexWriter(FSDirectory.open(new File("C:\\Users\\Administrator\\Desktop\\index").toPath()), new IndexWriterConfig(new IKAnalyzer()));
        Document document=new Document();
        document.add(new TextField("fieldName","new.txt", Field.Store.YES));
        document.add(new StoredField("fieldPath","c://new.txt"));
        document.add(new LongPoint("fieldSize",456));
        document.add(new StoredField("fieldSize",456));
        document.add(new TextField("fieldContent","修改fieldName为全文检索的文档,进行文档替换,先删除掉fieldName为全文检索的两个文档,再添加一个fileName为new的新文档", Field.Store.YES));
        //修改  参数一为条件  参数二为修改的文档值
        indexWriter.updateDocument(new Term("fieldName","全文检索"),document);
        //关闭
        indexWriter.close();
    }

 

2.9 查询效果  15个文档共有2个fielName带有全文检索所将两个删除后又添加一个为14个

 

 

 

2.10 根据域和关键词查询

private IndexSearcher indexSearcher;
    private IndexReader indexReader;
    @Before
    public void getSearcher() throws IOException {
        //1.创建Directory对象,指定索引库位置
        Directory directory= FSDirectory.open(new File("C:\\Users\\Administrator\\Desktop\\index").toPath());
        //2.创建IndexReader对象,读取索引库内容
        indexReader= DirectoryReader.open(directory);
        //3.创建IndexSearcher对象
        indexSearcher=new IndexSearcher(indexReader);

    }
//根据域和关键词进行查询
    @Test
    public void TermQuery() throws IOException {
        //创建查询条件
        Query query=new TermQuery(new Term("fieldName","new"));
        //执行查询
        TopDocs topDocs=indexSearcher.search(query,10);
        System.out.println("返回的文档个数:"+topDocs.totalHits);
        //获取到文档集合
        ScoreDoc[] scoreDocs=topDocs.scoreDocs;
        for (ScoreDoc doc:scoreDocs){
            //获取到文档
            Document document=indexSearcher.doc(doc.doc);
            //获取到文档域中数据
            System.out.println("fieldName:"+document.get("fieldName"));
            System.out.println("fieldPath:"+document.get("fieldPath"));
            System.out.println("fieldSize:"+document.get("fieldSize"));
            System.out.println("fieldContent:"+document.get("fieldContent"));
            System.out.println("======================================================================");
        }
        //关闭
        indexReader.close();
    }

 

2.11 查询效果

 

 

2.12 匹配一行数据  这行数据会自动分词

//匹配一行数据,这一行数据会自动进行分词
    @Test
        public void QueryParser() throws IOException, ParseException {
        //创建一个QueryParser对象 参数一:查询的域  参数二:使用哪种分析器
        QueryParser parser=new QueryParser("fieldContent",new IKAnalyzer());
        //设置匹配的数据条件
        Query query = parser.parse("Lucene是一个开源的基于Java的搜索库");
        //查询
        TopDocs topDocs = indexSearcher.search(query, 10);
        System.out.println("返回的文档个数:"+topDocs.totalHits);

        //获取到文档集合
        ScoreDoc [] scoreDocs=topDocs.scoreDocs;
        for (ScoreDoc doc:scoreDocs) {
            //获取到文档
            Document document = indexSearcher.doc(doc.doc);
            //获取到文档域中数据
            System.out.println("fieldName:"+document.get("fieldName"));
            System.out.println("fieldPath:"+document.get("fieldPath"));
            System.out.println("fieldSize:"+document.get("fieldSize"));
            System.out.println("fieldContent:"+document.get("fieldContent"));
            System.out.println("==============================================================");
        }


        //关闭
        indexReader.close();
    }

 

2.13 查询效果

 

 

//添加索引
@Test
public void createDocument() throws IOException {


//1.创建IndexWriter对象 参数一:索引库位置 参数二: 指定配置
IndexWriter indexWriter=new IndexWriter(FSDirectory.open(new File("C:\\Users\\Administrator\\Desktop\\index").toPath()), new IndexWriterConfig(new IKAnalyzer()));
//创建一个文档对象
Document document=new Document();
document.add(new TextField("fieldName","haha.text", Field.Store.YES));
document.add(new StoredField("fieldPath","G://haha.text"));
document.add(new LongPoint("fieldSize",123));
document.add(new StoredField("fieldSize",123));
document.add(new TextField("fieldName","ojdbc14ikanalyzermaven找不到的解决办,动发oJdbc14maven仓库,动发ikanalyzermaven,程适用于所有jar布 下 IKAnalyzerLucene使用和单独使用例子 简单性能测试 11-26 阅读数 1890 IKAnalyzer是一个开源基于JAVA言的 ",Field.Store.YES));
//创建索引,将文档添加到索引库当中
indexWriter.addDocument(document);
//关闭
indexWriter.close();
}

Guess you like

Origin www.cnblogs.com/1314Justin/p/12362479.html