hzwjava / ik-analyzer

Automatically exported from code.google.com/p/ik-analyzer
0 stars 0 forks source link

设置 isMaxWordLength 为 true 搜索有值,但是高度后值为null #12

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
IKAnalyzer analyzer = new IKAnalyzer(true);
        QueryParser parser = new QueryParser(Version.LUCENE_29, "title",
                new IKAnalyzer());
        Directory fsDir = FSDirectory.open(new File("D:/java/server/apache-tomcat-6.0.18/bin/solr/data/index"));
        IndexReader reader = null;

        try {
            reader = IndexReader.open(fsDir, true);

            Query query = parser.parse("title:【搜索引擎】Google购物搜索");

            IndexSearcher searcher = new IndexSearcher(reader);

            TopDocs hits = searcher.search(query, null, 10000);

            Document doc = null;

            ScoreDoc[] scoreDocs = hits.scoreDocs;
            int length = scoreDocs.length;

            for (int i = 0; i < length; i++) {
                doc = searcher.doc(scoreDocs[i].doc);
                //有值
                String value = doc.get("title");
                //System.out.println(doc.get("id"));
                //System.out.println(doc.getBoost());

                SimpleHTMLFormatter sHtmlF = new SimpleHTMLFormatter(
                        "<b>", "</b>");
                Highlighter highlighter = new Highlighter(sHtmlF, new QueryScorer(
                        query));
                highlighter.setTextFragmenter(new SimpleFragmenter(100000));
                if (value != null) {
                    TokenStream tokenStream = analyzer.tokenStream("title",
                            new StringReader(value));
                    String str = highlighter.getBestFragment(tokenStream, value);
                                //值为null
                    System.out.println(str);
                }

            }

            searcher.close();

Original issue reported on code.google.com by panxiuya...@gmail.com on 28 Jun 2010 at 3:17

GoogleCodeExporter commented 9 years ago
这个应该是高亮api算法的问题了。

Original comment by linliang...@gmail.com on 8 Jul 2010 at 7:19