请您先登录,才能继续操作

[lucene] prefixquery前缀搜索中文问题

langke93 2009-08-13

//创建索引

public void createIndex(){  

      MMAnalyzer mmanalyzer=new MMAnalyzer(2); 
     
       IndexWriter iwriter = new IndexWriter(path, mmanalyzer, true);

 

        // *测试数据
        Document doc1=new Document();
        Field field1=new Field("CP_GJZ", "abbbbb", Store.YES, Index.TOKENIZED);
        doc1.add(field1);
        iwriter.addDocument(doc1);
        
        Document doc6=new Document();
        Field field6=new Field("CP_GJZ", "abdfdsf", Store.YES, Index.TOKENIZED);
        doc6.add(field6);
        iwriter.addDocument(doc6);         

        Document doc2=new Document();
        Field field2=new Field("CP_GJZ", "bccdfdsfa", Store.YES, Index.TOKENIZED);
        doc2.add(field2);
        iwriter.addDocument(doc2);         
       
        Document doc3=new Document();
        Field field3=new Field("CP_GJZ", "中国共产中", Store.YES, Index.TOKENIZED);
        doc3.add(field3);
        iwriter.addDocument(doc3);
        
        Document doc4=new Document();
        Field field4=new Field("CP_GJZ", "工有的作在工", Store.YES, Index.TOKENIZED);
        doc4.add(field4);
        iwriter.addDocument(doc4);  
        
        Document doc5=new Document();
        Field field5=new Field("CP_GJZ", "有的是工作", Store.YES, Index.TOKENIZED);
        doc5.add(field5);
        iwriter.addDocument(doc5);  

 

           //对索引进行优化
           iwriter.optimize();  
           //关闭索引,注意这步很重要,就像使用数据连接.
           iwriter.close();

 }

 

 

//索引查询

 

    public void search(String queryString) {             
     StringBuffer strbuf=new StringBuffer();
     
        Hits hits = null;

            IndexSearcher is = null;           
            is = new IndexSearcher(path);
            
            Term Prefix=new Term("CP_GJZ",queryString);
            PrefixQuery query=new PrefixQuery(Prefix);            
                        
            hits = is.search(query);
            
            for(int i = 0 ;i<hits.length();i++){             
             System.out.println(hits.doc(i).get("CP_GJZ"));
            } 

    }     

 

 

 

查询英文  输入英文"a"都正常把a开头的前缀查询出来

 

 

问题中文查询时问题???

 

例如:输入中文"工"字我想把"工"字开头的全查出来,结果查询出来的是下面这样的

 

1个中文

结果如下:

工有的作在工

有的是工作

 

1个中文

例如:输入中文"有"字时

结果如下:

工有的作在工

有的是工作

 

2个中文

但是查询中文"工有"两个字时

结果如下:

工有的作在工

 

3个中文

但是查询中文"工有的"三个字时

结果如下:

工有的作在工

 

1个中文查询有问题,两个中文以上的查询都正常

 

 

望各位帮忙看下

 

 

 

 

 

 

 

 

langke93 2009-08-14
看看吧,高手们
leibnitz 2009-08-17
prefixquery是对tokened的terms进行查询的。
langke93 2009-08-21
搞定了,不进行分词就行了
Global site tag (gtag.js) - Google Analytics