`

luence

 
阅读更多
@SuppressWarnings({ "unchecked", "deprecation" })
    public static List<Goods> SearchForContent(String indexPath, String keyword, int start, int max,
            String orderByField, String orderByType) throws Exception
    {
   
        List<Goods> goodsList = new ArrayList<Goods>();
        String path = indexPath + "index";
        IndexSearcher indexSearcher = new IndexSearcher(path);
        IndexSearcher indexSearchers[] = { indexSearcher };
        MultiSearcher searcher = new MultiSearcher(indexSearchers);
        if (searcher != null)
        {
            String[] fields = { "saleName", "advertiseWords" };
            BooleanClause.Occur[] clauses = { BooleanClause.Occur.SHOULD, BooleanClause.Occur.SHOULD };
            Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT);
            keyword = LabelUtil.filter(keyword);
            Query query = MultiFieldQueryParser.parse(keyword, fields, clauses, analyzer);
            boolean orderby = true;
            if ("asc".equalsIgnoreCase(orderByType))
            {
                orderby = false;
            }
            SortField sortField = new SortField(orderByField, orderby);
            Sort sort = new Sort(sortField);
            Hits top = null;
            if ("sales".equals(orderByField))
            {
                top = searcher.search(query);
            }
            else
            {
                top = searcher.search(query, sort);
            }
            SimpleHTMLFormatter simpleHTMLFormatter = new SimpleHTMLFormatter(LS, LE);
            Highlighter highlighter = new Highlighter(simpleHTMLFormatter, new QueryScorer(query));
            highlighter.setTextFragmenter(new SimpleFragmenter(100));
            if (max > top.length())
                max = top.length();
            for (int i = start; i < max; i++)
            {
                Document doc = top.doc(i);
                List<Field> list = doc.getFields();
                Goods good = new Goods();
                for (Field d : list)
                {
                    String filedName = d.name();
                    String value = d.stringValue();
                    if ("goodsId".equals(filedName))
                    {
                        String goodsId = RequestUtil.VailStr(value, "");
                        good.setGoods_id(Long.parseLong(goodsId));
                    }
                    if ("saleName".equals(filedName))
                    {
                        String filterStr = LabelUtil.filter(RequestUtil.VailStr(d.stringValue(), ""));
                        String highliStr = highlighter.getBestFragment(LuceneContacts.analyzer, d.name(), filterStr);
                        if (StringUtil.isEmpty(highliStr))
                        {
                            good.setSale_name(RequestUtil.VailStr(value, ""));
                        }
                        else
                        {
                            good.setSale_name(highliStr);
                        }
                    }
                    if ("name".equals(filedName))
                    {
                        good.setName(RequestUtil.VailStr(value, ""));
                    }
                    if ("advertiseWords".equals(filedName))
                    {
                        String filterStr = LabelUtil.filter(RequestUtil.VailStr(d.stringValue(), ""));
                        String highliStr = highlighter.getBestFragment(LuceneContacts.analyzer, d.name(), filterStr);
                        if (StringUtil.isEmpty(highliStr))
                        {
                            good.setAdvertise_words(RequestUtil.VailStr(value, ""));
                        }
                        else
                        {
                            good.setAdvertise_words(highliStr);
                        }
                    }
                    if ("price".equals(filedName))
                    {
                        if (value != null && !"".equals(value) && !"null".equals(value))
                        {
                            good.setPrice(new BigDecimal(value));
                        }
                        else
                        {
                            good.setPrice(new BigDecimal(0));
                        }
                    }
                    if ("onShelfTime".equals(filedName))
                    {
                        Date date = null;
                        if (value != null && !"".equals(value))
                        {
                            date = new Date(RequestUtil.FormatDaeHH(value));
                        }
                        good.setOn_shelf_time(date);
                    }
                    if ("primaryPicName".equals(filedName))
                    {
                        good.setPrimaryPicName(RequestUtil.VailStr(value, ""));
                    }
                    if ("imgPath".equals(filedName))
                    {
                        good.setImgPath(RequestUtil.VailStr(value, ""));
                    }
                    if ("picType".equals(filedName))
                    {
                        good.setPicType(RequestUtil.VailStr(value, ""));
                    }
                }
                goodsList.add(good);
            }
            searcher.close();
        }
        return goodsList;
    }
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics