Lucene添加索引实际应用方法 kiddy

View Code
/// <summary>
    /// 添加索引
    /// </summary>
    /// <param name="TypeValue">1,企业-2,产品-3,供求、商机-4,资讯</param>
    /// <param name="ID">所要添加索引的记录的ID</param>
    /// <returns>0,失败-1,成功-2,Type值错误,3-索引添加出错</returns>
    public static int AddSearchIndex(int TypeValue, string IDS)
    {
        int returnValue = 0;
        string Searchindex = "";
        string strArticleType = "Articles";
        DataTable dt = new DataTable();
        switch (TypeValue)
        {
            case 1:
                Searchindex = "Enterprise";
                dt = Com.Netmarch.SQLHelperDAL.SQLHelper.GetDataSet(" select User_ID ,User_Name,GSMemo,Photo from [User] where Useable = 1 and userType in (2,3) and User_ID in (" + IDS + ") ").Tables[0];
                break;
            case 2:
                Searchindex = "Product";
                dt = Com.Netmarch.SQLHelperDAL.SQLHelper.GetDataSet(" select ID ,ProductName,ProductIntroduction,AddUser from Products where IsAudit = 1 and AddUser in (select User_ID from [User] where Useable = 1 and userType in (2,3) ) and ID in (" + IDS + ") ").Tables[0];
                break;
            case 3:
                Searchindex = "SupplyAndDemand";
                dt = Com.Netmarch.SQLHelperDAL.SQLHelper.GetDataSet(" select ID ,Title,Content,AddUser from SupplyAndDemands where IsAudit = 1 and ID in (" + IDS + ") ").Tables[0];
                break;
            case 4:
                string[] str = IDS.Split('_');
                if (str[0].ToLower() == "articles")
                {
                    strArticleType = "Articles";
                    Searchindex = "Article";
                    dt = Com.Netmarch.SQLHelperDAL.SQLHelper.GetDataSet(" select Id ,Title,Summary,AddUser from Articles where State = 1 and ID in (" + str[1] + ") ").Tables[0];
                }
                else
                {
                    strArticleType = "EntArticles";
                    Searchindex = "EnArticle";
                    dt = Com.Netmarch.SQLHelperDAL.SQLHelper.GetDataSet(" select Id ,Title,Summary,AddUser from EntArticles where IsAudit = 1 and AddUser in (select User_ID from [User] where userType in (2,3) and Useable=1 ) and ID in (" + str[1] + ") ").Tables[0];
                    dt.Columns.Add("num");
                }
                break;
            default:
                Searchindex = "";
                break;
        }
        if (dt != null && dt.Rows.Count > 0 && Searchindex != "")
        {
            FileInfo nmm_FileInfo = new FileInfo(HttpContext.Current.Server.MapPath("/SearchIndex/" + Searchindex));
            if (Directory.Exists(nmm_FileInfo.FullName))
            {
                string[] Files = Directory.GetFileSystemEntries(nmm_FileInfo.FullName);
                if (Files != null)
                {
                    try
                    {
                        if (TypeValue == 1)
                        {
                            if (Files.Length > 0)
                            {
                                IndexWriter indexwrite = new IndexWriter(nmm_FileInfo.FullName, new StandardAnalyzer(), false);//索引文件存储的路径
                                foreach (DataRow dr in dt.Rows)
                                {
                                    Document doc = new Document();
                                    doc.Add(new Field("ID", dr["User_ID"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
                                    doc.Add(new Field("AddUserID", dr["User_ID"].ToString(), Field.Store.YES, Field.Index.NO));
                                    doc.Add(new Field("Title", dr["User_Name"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
                                    doc.Add(new Field("Type", "1", Field.Store.YES, Field.Index.NO));
                                    doc.Add(new Field("Content", dr["GSMemo"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
                                    doc.Add(new Field("PicPath", dr["Photo"].ToString(), Field.Store.YES, Field.Index.NO));
                                    doc.Add(new Field("URL", string.Format(ConfigurationManager.AppSettings["EnterpriseRootDirUrl"], dr["User_ID"].ToString()), Field.Store.YES, Field.Index.NO));
                                    indexwrite.AddDocument(doc);

                                }
                                indexwrite.Flush();
                                indexwrite.Close();
                            }
                            else
                            {
                                IndexWriter indexwrite = new IndexWriter(nmm_FileInfo.FullName, new StandardAnalyzer(), true);//索引文件存储的路径
                                foreach (DataRow dr in dt.Rows)
                                {
                                    Document doc = new Document();
                                    doc.Add(new Field("ID", dr["User_ID"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
                                    doc.Add(new Field("AddUserID", dr["User_ID"].ToString(), Field.Store.YES, Field.Index.NO));
                                    doc.Add(new Field("Title", dr["User_Name"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
                                    doc.Add(new Field("Type", "1", Field.Store.YES, Field.Index.NO));
                                    doc.Add(new Field("Content", dr["GSMemo"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
                                    doc.Add(new Field("PicPath", dr["Photo"].ToString(), Field.Store.YES, Field.Index.NO));
                                    doc.Add(new Field("URL", string.Format(ConfigurationManager.AppSettings["EnterpriseRootDirUrl"], dr["User_ID"].ToString()), Field.Store.YES, Field.Index.NO));
                                    indexwrite.AddDocument(doc);

                                }
                                indexwrite.Flush();
                                indexwrite.Close();
                            }
                        }
                        else
                        {
                            if (TypeValue == 2)
                            {
                                if (Files.Length > 0)
                                {
                                    IndexWriter indexwrite = new IndexWriter(nmm_FileInfo.FullName, new StandardAnalyzer(), false);//索引文件存储的路径
                                    DataSet dsProtalAttach = new DataSet();
                                    foreach (DataRow dr in dt.Rows)
                                    {
                                        dsProtalAttach = (new ProtalAttach()).GetDataSetProtalAttach(" and AttachType = '3' and ParentID = '" + dr["ID"].ToString() + "' ");
                                        Document doc = new Document();
                                        doc.Add(new Field("ID", dr["ID"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
                                        doc.Add(new Field("AddUserID", dr["AddUser"].ToString(), Field.Store.YES, Field.Index.NO));
                                        doc.Add(new Field("Title", dr["ProductName"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
                                        doc.Add(new Field("Type", "2", Field.Store.YES, Field.Index.NO));
                                        doc.Add(new Field("Content", dr["ProductIntroduction"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
                                        doc.Add(new Field("PicPath", (dsProtalAttach.Tables[0].Rows.Count > 0 ? dsProtalAttach.Tables[0].Rows[0]["SmallAttachUrl"].ToString() : ""), Field.Store.YES, Field.Index.NO));
                                        doc.Add(new Field("URL", string.Format(ConfigurationManager.AppSettings["ProductRootDirUrl"], dr["AddUser"].ToString()) + "&id=" + dr["ID"].ToString() + "", Field.Store.YES, Field.Index.NO));
                                        indexwrite.AddDocument(doc);

                                    }
                                    indexwrite.Flush();
                                    indexwrite.Close();
                                }
                                else
                                {
                                    IndexWriter indexwrite = new IndexWriter(nmm_FileInfo.FullName, new StandardAnalyzer(), true);//索引文件存储的路径
                                    DataSet dsProtalAttach = new DataSet();
                                    foreach (DataRow dr in dt.Rows)
                                    {
                                        dsProtalAttach = (new ProtalAttach()).GetDataSetProtalAttach(" and AttachType = '3' and ParentID = '" + dr["ID"].ToString() + "' ");
                                        Document doc = new Document();
                                        doc.Add(new Field("ID", dr["ID"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
                                        doc.Add(new Field("AddUserID", dr["AddUser"].ToString(), Field.Store.YES, Field.Index.NO));
                                        doc.Add(new Field("Title", dr["ProductName"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
                                        doc.Add(new Field("Type", "2", Field.Store.YES, Field.Index.NO));
                                        doc.Add(new Field("Content", dr["ProductIntroduction"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
                                        doc.Add(new Field("PicPath", (dsProtalAttach.Tables[0].Rows.Count > 0 ? dsProtalAttach.Tables[0].Rows[0]["SmallAttachUrl"].ToString() : ""), Field.Store.YES, Field.Index.NO));
                                        doc.Add(new Field("URL", string.Format(ConfigurationManager.AppSettings["ProductRootDirUrl"], dr["AddUser"].ToString()) + "&id=" + dr["ID"].ToString() + "", Field.Store.YES, Field.Index.NO));
                                        indexwrite.AddDocument(doc);

                                    }
                                    indexwrite.Flush();
                                    indexwrite.Close();
                                }
                            }
                            else
                            {
                                if (TypeValue == 3)
                                {
                                    if (Files.Length > 0)
                                    {
                                        IndexWriter indexwrite = new IndexWriter(nmm_FileInfo.FullName, new StandardAnalyzer(), false);//索引文件存储的路径
                                        foreach (DataRow dr in dt.Rows)
                                        {
                                            Document doc = new Document();
                                            doc.Add(new Field("ID", dr["ID"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
                                            doc.Add(new Field("AddUserID", dr["AddUser"].ToString(), Field.Store.YES, Field.Index.NO));
                                            doc.Add(new Field("Title", dr["Title"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
                                            doc.Add(new Field("Type", "3", Field.Store.YES, Field.Index.NO));
                                            doc.Add(new Field("Content", dr["Content"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
                                            doc.Add(new Field("PicPath", "", Field.Store.YES, Field.Index.NO));
                                            doc.Add(new Field("URL", string.Format(ConfigurationManager.AppSettings["SupplyAndDemandRootDirUrl"], dr["AddUser"].ToString()) + "&id=" + dr["ID"].ToString() + "", Field.Store.YES, Field.Index.NO));
                                            indexwrite.AddDocument(doc);

                                        }
                                        indexwrite.Flush();
                                        indexwrite.Close();
                                    }
                                    else
                                    {
                                        IndexWriter indexwrite = new IndexWriter(nmm_FileInfo.FullName, new StandardAnalyzer(), true);//索引文件存储的路径
                                        foreach (DataRow dr in dt.Rows)
                                        {
                                            Document doc = new Document();
                                            doc.Add(new Field("ID", dr["ID"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
                                            doc.Add(new Field("AddUserID", dr["AddUser"].ToString(), Field.Store.YES, Field.Index.NO));
                                            doc.Add(new Field("Title", dr["Title"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
                                            doc.Add(new Field("Type", "3", Field.Store.YES, Field.Index.NO));
                                            doc.Add(new Field("Content", dr["Content"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
                                            doc.Add(new Field("PicPath", "", Field.Store.YES, Field.Index.NO));
                                            doc.Add(new Field("URL", string.Format(ConfigurationManager.AppSettings["SupplyAndDemandRootDirUrl"], dr["AddUser"].ToString()) + "&id=" + dr["ID"].ToString() + "", Field.Store.YES, Field.Index.NO));
                                            indexwrite.AddDocument(doc);

                                        }
                                        indexwrite.Flush();
                                        indexwrite.Close();
                                    }
                                }
                                else
                                {
                                    if (TypeValue == 4)
                                    {
                                        if (Files.Length > 0)
                                        {
                                            //if (dt.Columns.Count > 4)
                                            if (strArticleType == "Articles")
                                            {
                                                IndexWriter indexwrite = new IndexWriter(nmm_FileInfo.FullName, new StandardAnalyzer(), false);//索引文件存储的路径
                                                foreach (DataRow dr in dt.Rows)
                                                {
                                                    Document doc = new Document();
                                                    doc.Add(new Field("ID", "Articles_" + dr["Id"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
                                                    doc.Add(new Field("AddUserID", dr["AddUser"].ToString(), Field.Store.YES, Field.Index.NO));
                                                    doc.Add(new Field("Title", dr["Title"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
                                                    doc.Add(new Field("Type", "4", Field.Store.YES, Field.Index.NO));
                                                    doc.Add(new Field("Content", dr["Summary"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
                                                    doc.Add(new Field("PicPath", "", Field.Store.YES, Field.Index.NO));
                                                    doc.Add(new Field("URL", string.Format(ConfigurationManager.AppSettings["ArticleRootDirUrl"], dr["Id"].ToString()), Field.Store.YES, Field.Index.NO));
                                                    indexwrite.AddDocument(doc);
                                                }
                                                indexwrite.Flush();
                                                indexwrite.Close();
                                            }
                                            else
                                            {
                                                IndexWriter indexwrite = new IndexWriter(nmm_FileInfo.FullName, new StandardAnalyzer(), false);//索引文件存储的路径
                                                foreach (DataRow dr in dt.Rows)
                                                {
                                                    Document doc = new Document();
                                                    doc.Add(new Field("ID", "EntArticles_" + dr["Id"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
                                                    doc.Add(new Field("AddUserID", dr["AddUser"].ToString(), Field.Store.YES, Field.Index.NO));
                                                    doc.Add(new Field("Title", dr["Title"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
                                                    doc.Add(new Field("Type", "4", Field.Store.YES, Field.Index.NO));
                                                    doc.Add(new Field("Content", dr["Summary"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
                                                    doc.Add(new Field("PicPath", "", Field.Store.YES, Field.Index.NO));
                                                    //doc.Add(new Field("URL", ("/qyhome/Artilce_Detail.aspx?id=" + dr["Id"].ToString() + "&cid=" + dr["AddUser"].ToString()), Field.Store.YES, Field.Index.NO));
                                                    doc.Add(new Field("URL", string.Format(ConfigurationManager.AppSettings["EnArticleRootDirUrl"], dr["Id"].ToString(), dr["AddUser"].ToString()), Field.Store.YES, Field.Index.NO));
                                                    indexwrite.AddDocument(doc);
                                                }
                                                indexwrite.Flush();
                                                indexwrite.Close();
                                            }
                                        }
                                        else
                                        {
                                            if (dt.Columns.Count > 4)
                                            {
                                                IndexWriter indexwrite = new IndexWriter(nmm_FileInfo.FullName, new StandardAnalyzer(), true);//索引文件存储的路径
                                                foreach (DataRow dr in dt.Rows)
                                                {
                                                    Document doc = new Document();
                                                    doc.Add(new Field("ID", "Articles_" + dr["Id"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
                                                    doc.Add(new Field("AddUserID", dr["AddUser"].ToString(), Field.Store.YES, Field.Index.NO));
                                                    doc.Add(new Field("Title", dr["Title"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
                                                    doc.Add(new Field("Type", "4", Field.Store.YES, Field.Index.NO));
                                                    doc.Add(new Field("Content", dr["Summary"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
                                                    doc.Add(new Field("PicPath", "", Field.Store.YES, Field.Index.NO));
                                                    doc.Add(new Field("URL", string.Format(ConfigurationManager.AppSettings["ArticleRootDirUrl"], dr["Id"].ToString()), Field.Store.YES, Field.Index.NO));
                                                    indexwrite.AddDocument(doc);
                                                }
                                                indexwrite.Flush();
                                                indexwrite.Close();
                                            }
                                            else
                                            {
                                                IndexWriter indexwrite = new IndexWriter(nmm_FileInfo.FullName, new StandardAnalyzer(), true);//索引文件存储的路径
                                                foreach (DataRow dr in dt.Rows)
                                                {
                                                    Document doc = new Document();
                                                    doc.Add(new Field("ID", "EntArticles_" + dr["Id"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
                                                    doc.Add(new Field("AddUserID", dr["AddUser"].ToString(), Field.Store.YES, Field.Index.NO));
                                                    doc.Add(new Field("Title", dr["Title"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
                                                    doc.Add(new Field("Type", "4", Field.Store.YES, Field.Index.NO));
                                                    doc.Add(new Field("Content", dr["Summary"].ToString(), Field.Store.YES, Field.Index.TOKENIZED));
                                                    doc.Add(new Field("PicPath", "", Field.Store.YES, Field.Index.NO));
                                                    //doc.Add(new Field("URL", ("/qyhome/Artilce_Detail.aspx?id=" + dr["Id"].ToString() + "&cid=" + dr["AddUser"].ToString()), Field.Store.YES, Field.Index.NO));
                                                    doc.Add(new Field("URL", string.Format(ConfigurationManager.AppSettings["EnArticleRootDirUrl"], dr["Id"].ToString(), dr["AddUser"].ToString()), Field.Store.YES, Field.Index.NO));
                                                    indexwrite.AddDocument(doc);
                                                }
                                                indexwrite.Flush();
                                                indexwrite.Close();
                                            }
                                        }
                                    }
                                    else
                                    {
                                        returnValue = 2;
                                    }
                                }
                            }
                        }
                        returnValue = 1;
                    }
                    catch
                    {
                        returnValue = 3;
                    }
                }
            }
        }
        else
        {
            returnValue = 2;
        }

        return returnValue;
    }
道之所在,虽千万人吾往矣
原文地址:https://www.cnblogs.com/kongdf/p/2566487.html