Beispiel #1
0
        public List <Document> SeacherForClass(string strqueryString, int websiteid, out int TotalCount)
        {
            string          queryString = strqueryString;
            Directory       d           = CreateIndex.CreateDir(Table);
            TopDocs         docs;
            BooleanQuery    query;
            List <Document> doc = new List <Document>();

            TotalCount = 0;
            try
            {
                query = new BooleanQuery();
                IndexSearcher mysea = new IndexSearcher(IndexReader.Open(d, true));


                if (queryString != "")
                {
                    Query       query1;
                    QueryParser qp = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "SearchFor", new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30));
                    query1 = qp.Parse(queryString + "*");
                    query.Add(query1, Occur.MUST);
                }
                docs = mysea.Search(query, null, 500);
                ScoreDoc[] hits = docs.ScoreDocs;  //获取命中的文档信息对象
                //TotalCount = docs.totalHits;
                TotalCount = hits.Length;
                for (int i = 0; i < hits.Length; i++)
                {
                    Document hitDoc = mysea.Doc(hits[i].Doc); //根据命中的文档的内部编号获取该文档
                    doc.Add(hitDoc);
                }
                mysea.Dispose();
            }
            catch (Exception e)
            {
                LogUtil.Log("搜索失败", e.Message);
                query = null;
                docs  = null;
            }
            return(doc);
        }
Beispiel #2
0
        /// <summary>
        /// 取得搜索信息
        /// </summary>
        /// <param name="queryString"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public List <Document> seacher(string strqueryString, int websiteid, int brandid, int classid, int jishi, out int TotalCount)
        {
            string          queryString = strqueryString;
            Directory       d           = CreateIndex.CreateDir(Table);
            TopDocs         docs;
            BooleanQuery    query;
            List <Document> doc = new List <Document>();

            TotalCount = 0;
            try
            {
                query = new BooleanQuery();
                //IndexSearcher mysea = new IndexSearcher(d, true);
                IndexSearcher mysea = new IndexSearcher(IndexReader.Open(d, true));
                if (queryString != "")
                {
                    IList <string> querystrattr = StringUtils.GetLuceneChinaStrAttr(queryString);
                    foreach (string querystr in querystrattr)
                    {
                        Query       query1;
                        QueryParser qp = new QueryParser(Lucene.Net.Util.Version.LUCENE_30, "SearchFor", new StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_30));
                        query1 = qp.Parse(querystr);
                        query.Add(query1, Occur.MUST);
                    }
                }
                //if (websiteid > 0)
                //{
                //    //不能用BrandId来索引,会有重复数据.
                //    Query query2 = new TermQuery(new Term("SiteId", websiteid.ToString()));
                //    query.Add(query2, Occur.MUST);
                //}
                //else
                //{
                //    Query query2 = new TermQuery(new Term("SiteId", "1"));
                //    query.Add(query2,  Occur.MUST);
                //}
                if (brandid > 0)
                {
                    //不能用BrandId来索引,会有重复数据.
                    Query query3 = new TermQuery(new Term("BrandId", brandid.ToString()));
                    query.Add(query3, Occur.MUST);
                }
                if (classid > 0)
                {
                    //不能用BrandId来索引,会有重复数据.
                    Query query4 = new TermQuery(new Term("ClassId", classid.ToString()));
                    query.Add(query4, Occur.MUST);
                }
                if (jishi > 0)
                {
                    //不能用BrandId来索引,会有重复数据.
                    Query queryjishisong = new TermQuery(new Term("JiShiSong", jishi.ToString()));
                    query.Add(queryjishisong, Occur.MUST);
                }
                docs = mysea.Search(query, null, 500);
                ScoreDoc[] hits = docs.ScoreDocs;  //获取命中的文档信息对象
                //TotalCount = docs.totalHits;
                TotalCount = hits.Length;

                for (int i = 0; i < hits.Length; i++)
                {
                    Document hitDoc = mysea.Doc(hits[i].Doc); //根据命中的文档的内部编号获取该文档
                    doc.Add(hitDoc);
                }
                mysea.Dispose();
            }
            catch (Exception e)
            {
                LogUtil.Log("搜索失败", e.Message);
                query = null;
                docs  = null;
            }
            return(doc);
        }