乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      在.Net環(huán)境下使用elasticsearch實(shí)現(xiàn)大數(shù)據(jù)量的搜索

       印度阿三17 2019-02-14

      最近因?yàn)轫?xiàng)目需要使用搜索引擎,因此嘗試使用.Net去操作elasticsearch,把使用過(guò)程記錄如下:

      1.安裝elasticsearch

      2.安裝elasticsearch插件

      3.使用NEST操作elasticsearch

       

      elasticsearch下文使用簡(jiǎn)稱(chēng)ES,ES已經(jīng)更新到了6.*,經(jīng)常使用的應(yīng)該是2.*和5.*,其中5.*當(dāng)然對(duì)2.*更新了許多功能,但是在初學(xué)者最直觀的改變是關(guān)聯(lián)插件的版本,2.*關(guān)聯(lián)插件的版本號(hào)基本上是亂的,需要去插件對(duì)應(yīng)的網(wǎng)站上查詢(xún),但是5.*對(duì)應(yīng)的插件版本基本上和ES本身的版本號(hào)一致,這就避免了為找相應(yīng)插件而做的很多無(wú)謂的工作。

       

      1.安裝elasticsearch

      安裝之前請(qǐng)先安裝Jdk,我使用的是1.8版本,安裝完后配置環(huán)境變量,安裝過(guò)程略過(guò)。

      我這里使用的是5.6.0版本。下載路徑如下 https://www./downloads/past-releases/elasticsearch-5-6-0

      下載完成后,將下載文件直接解壓到安裝目錄下即可。

      安裝完成后,就是啟動(dòng)ES在bin目錄下可以運(yùn)行elasticsearch.bat在命令行界面啟動(dòng)(命令行不能關(guān)閉),也可以運(yùn)行elasticsearch-service.bat啟動(dòng)ES的服務(wù)進(jìn)程,這樣就不用每次開(kāi)機(jī)啟動(dòng)了。

      配置文件在config/elasticsearch.yml中,每次修改完配置需要服務(wù)重啟,切記切記。

       

      啟動(dòng)成功后,打開(kāi)瀏覽器,輸入默認(rèn)的url和端口  

      http://localhost:9200/   

      如果啟動(dòng)成功,則會(huì)出現(xiàn)如下頁(yè)面,說(shuō)明安裝成功了

      2.安裝elasticsearch插件

      ES有許多插件用來(lái)幫助我們使用,其中我覺(jué)得最主要的有兩個(gè)插件

       1)esheader    可以使用在web頁(yè)面上訪(fǎng)問(wèn)esheader的集群數(shù)據(jù)

       2)  analysis-ik   中文的分詞插件,默認(rèn)的分詞功能在搜索時(shí)只能將中文分成一個(gè)一個(gè)的單字

       

      esheader的安裝

      esheader在2.*版本上是可以直接在命令行中進(jìn)行安裝的,但是5.*版本上不能這樣做了,網(wǎng)上采用的辦法很麻煩,我找到一種比較簡(jiǎn)單的辦法。

      其實(shí)說(shuō)到底esheader就是在web頁(yè)面上向es服務(wù)器發(fā)送http請(qǐng)求,并獲取結(jié)果進(jìn)行展示,其實(shí)esheader實(shí)質(zhì)上就是一個(gè)java web網(wǎng)站,因此只需要將它部署到tomcat服務(wù)器上,運(yùn)行即可。

      ip和端口號(hào),需要和安裝ES配置的相一致,而且需要將ES的配置文件修改為允許跨域訪(fǎng)問(wèn)即在config/elasticsearch.yml最后追加如下配置,然后重啟服務(wù)

      http.cors.enabled: true
      http.cors.allow-origin: "*"

      然后訪(fǎng)問(wèn)tomcat上對(duì)應(yīng)的網(wǎng)頁(yè)即可,如下圖。說(shuō)明esheader安裝成功了?。?/p>

      analysis-ik 的安裝

      ik可以直接在命令行下面安裝(前提是知道ik文件的url)

      在ES的bin目錄下打開(kāi)控制臺(tái)運(yùn)行 ,即可

      .\elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.6.0/elasticsearch-analysis-ik-5.6.0.zip

       安裝完成后ES的plugins目錄中會(huì)添加對(duì)應(yīng)插件的目錄

       

      3.使用NEST訪(fǎng)問(wèn)ES

      新建一個(gè).net控制臺(tái)項(xiàng)目,打開(kāi)NuGet管理器,搜索NEST,然后安裝,如圖。我安裝的是5.6.0版本,不知道其余版本是否OK,感興趣的朋友可以試試

       

      安裝完成后,就可以編寫(xiě)代碼了

              private static void AddData()
              {
                  //1.通過(guò)es服務(wù)器 localhost:9200 來(lái)定義es client
                  var node = new Uri("http://localhost:9200");
                  var indexName = "products";
                  var settings = new ConnectionSettings(node).DefaultIndex(indexName);
                  var elastic = new ElasticClient(settings);
      
                  //es服務(wù)器健康檢查
                  var res = elastic.ClusterHealth();
                  Console.WriteLine(res.Status);
      
                  //2.創(chuàng)建索引esbot
                  //if (!elastic.IndexExists(indexName).Exists)
                  //{
                  //    var createIndexResponse = elastic.CreateIndex(indexName);
                  //    var mappingBlogPost = elastic.Map<Account>(s => s.AutoMap());
                  //}
                  //var mappingBlogPost = elastic.Map<Account>(s => s.AutoMap());
                  //List<Account> accounts = new List<Account>() { new Account { Id = 2, Name = "aaa", Password = "bbb" }, new Account { Id = 3, Name = "ccc", Password = "ddd" } };
                  List<Product> duodians = new TestEntities().duodian.ToList().Select(a => a.ToProduct()).ToList();
      
      
                  elastic.IndexMany(duodians, indexName);
      
              }
      
              private static void SimpleSelect()
              {
                  //1.通過(guò)es服務(wù)器 localhost:9200 來(lái)定義es client
                  var node = new Uri("http://localhost:9200");
                  var indexName = "products";
                  var settings = new ConnectionSettings(node).DefaultIndex(indexName);
                  var elastic = new ElasticClient(settings);
      
                  //5. 簡(jiǎn)單搜索
                  //var searchResult = elastic.Search<Product>(sr => sr.Query(q => q.MatchAll()).From(0).Size(100));
                  //var searchResult = elastic.Search<Product>(sr => sr.Query(q => q.QueryString(qs=>qs.Query("蘇泊爾"))));
                  var searchResult = elastic.Search<Product>(sr => sr.Query(q => q.Match(qm => qm.Field(f => f.title).Query("給你省小號(hào)搟面杖"))));
      
                  return;
              }
      View Code

       

          [ElasticsearchType(Name = "Product", IdProperty = "GuidId")]
          public class Product
          {
              [Keyword(Name = "GuidId", Index = true)]
              public string GuidId { get; set; } = Guid.NewGuid().ToString();
      
              [Number(NumberType.Long, Name = "Id")]
              public int id { get; set; }
      
              [Text(Name = "Title", Index = true, Analyzer = "ik_max_word")]
              public string title { get; set; }
      
              [Keyword(Name = "Img", Index = false)]
              public string img { get; set; }
      
              [Keyword(Name = "Lunfanimg", Index = false)]
              public string lunfanimg { get; set; }
      
              [Keyword(Name = "Spec", Index = false)]
              public string spec { get; set; }
      
              [Keyword(Name = "Xcimg", Index = false)]
              public string xcimg { get; set; }
      
              [Keyword(Name = "Skuid", Index = false)]
              public string skuid { get; set; }
      
              [Keyword(Name = "Pimg", Index = false)]
              public string pimg { get; set; }
      
              [Keyword(Name = "Plunfanimg", Index = false)]
              public string plunfanimg { get; set; }
      
              [Keyword(Name = "Pxcimg", Index = false)]
              public string pxcimg { get; set; }
      
              [Keyword(Name = "Categoryid", Index = false)]
              public string categoryid { get; set; }
      
              [Keyword(Name = "Price", Index = false)]
              public string price { get; set; }
      
              [Keyword(Name = "Brandname", Index = false)]
              public string brandname { get; set; }
      
              [Keyword(Name = "Categoryname", Index = false)]
              public string categoryname { get; set; }
      
              [Keyword(Name = "Created", Index = false)]
              public System.DateTime created { get; set; }
          }
      
          public static class duodianExtension
          {
              public static Product ToProduct(this duodian duodian)
              {
                  return new Product
                  {
                      brandname = duodian.brandname,
                      categoryid = duodian.categoryid,
                      id = duodian.id,
                      img = duodian.img,
                      lunfanimg = duodian.lunfanimg,
                      pimg = duodian.pimg,
                      plunfanimg = duodian.plunfanimg,
                      price = duodian.price,
                      pxcimg = duodian.pxcimg,
                      skuid = duodian.skuid,
                      spec = duodian.spec,
                      title = duodian.title,
                      xcimg = duodian.xcimg,
                      created = duodian.created,
                      categoryname = duodian.categoryname
                  };
              }
          }
      View Code

        本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶(hù)發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買(mǎi)等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
        轉(zhuǎn)藏 分享 獻(xiàn)花(0

        0條評(píng)論

        發(fā)表

        請(qǐng)遵守用戶(hù) 評(píng)論公約

        類(lèi)似文章 更多