受欢迎的博客标签

elasticsearch 通过HTTP RESTful API 操作数据(windows iaspnetcore)

Published

 

Install Elasticsearch 7.x on windows

Newer versions of Elasticsearch are bundled with its own OpenJDK.

Installation Steps

Next, follow these steps to install Elasticsearch 7.12.0.

Step 1.Download and install the .zip package

1.下载Elasticsearch 7.x for windows

https://www.elastic.co/downloads/elasticsearch

2、解压后copy  到F:\elasticsearch\elasticsearch-7.12.0

Step 2.Running Elasticsearch Server from the command line

cd F:\elasticsearch\elasticsearch-7.12.0\bin
elasticsearch.bat

Step 3.Checking that Elasticsearch is running(测试是否运行正常)

http://localhost:9200/

output

http://localhost:9200/
{
  "name" : "DESKTOP-5NBVMT8",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "4Rwvaca2Q6ezm63KcifFzw",
  "version" : {
    "number" : "7.12.0",
    "build_flavor" : "default",
    "build_type" : "zip",
    "build_hash" : "78722783c38caa25a70982b5b042074cde5d3b3a",
    "build_date" : "2021-03-18T06:17:15.410153305Z",
    "build_snapshot" : false,
    "lucene_version" : "8.8.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

4、下载IK分词插件

https://github.com/medcl/elasticsearch-analysis-ik/releases 下载已经编译的版本,elasticsearch-analysis-ik-7.12.0.zip,不要下载源码Source code (zip)

https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.12.0/elasticsearch-analysis-ik-7.12.0.zip

5、

cd F:\elasticsearch\elasticsearch-7.12.0\plugins\
mkdir ik
copy F:\googleDownload\elasticsearch-analysis-ik-7.12.0\*.*  F:\elasticsearch\elasticsearch-7.12.0\plugins\ik

解压elasticsearch-analysis-ik-7.12.0.zip,copy 到F:\elasticsearch\elasticsearch-7.12.0\plugins\ik,不带目录,一个分词一个子目录 

F:\>cd F:\elasticsearch\elasticsearch-7.12.0\plugins\ik

F:\elasticsearch\elasticsearch-7.12.0\plugins\ik>dir
 驱动器 F 中的卷是 新加卷
 卷的序列号是 8686-9048

 F:\elasticsearch\elasticsearch-7.12.0\plugins\ik 的目录

2021/04/17  02:16    <DIR>          .
2021/04/17  02:16    <DIR>          ..
2021/04/17  02:16           263,965 commons-codec-1.9.jar
2021/04/17  02:16            61,829 commons-logging-1.2.jar
2021/04/17  02:16    <DIR>          config
2021/04/17  02:16            54,625 elasticsearch-analysis-ik-7.12.0.jar
2021/04/17  02:16           736,658 httpclient-4.5.2.jar
2021/04/17  02:16           326,724 httpcore-4.4.4.jar
2021/04/17  02:16             1,807 plugin-descriptor.properties
2021/04/17  02:16               125 plugin-security.policy
               7 个文件      1,445,733 字节
               3 个目录 529,961,910,272 可用字节

 

Restart Elasticsearch Server(重新启动)

cd F:\elasticsearch\elasticsearch-7.12.0\bin
elasticsearch.bat

 

Checking that Elasticsearch plugin [analysis-ik] is loaded(显示插件已加载)

http://localhost:9200/

Before

[o.e.p.PluginsService     ] [DESKTOP-5NBVMT8] no plugins loaded

Now

[o.e.p.PluginsService     ]  loaded plugin [analysis-ik]

1.检查es版本信息

output

2.查看集群是否健康

http://IP:9200/_cat/health?v

output

epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1580168554 23:42:34  elasticsearch yellow          1         1      2   2    0    0        2             0                  -                 50.0%

3.查看节点列表

http://IP:9200/_cat/nodes?v

output

ip        heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
127.0.0.1           15          97   1    0.02    0.02     0.00 dilm      *      iZuf6hn15mpmrtypv9ltogZ
localhost:9200/_stats

4.列出所有索引及存储大小

http://IP:9200/_cat/indices?v

output

health status index               uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   index1 dCxwa7m7Q2iHRkCjWT3cFA   1   1       1012            0      9.4mb          9.4mb
yellow open   index               6awW2eDiTx2klpvhGVN0og   1   1          0            0       283b           283b

 

5.列出分片设置

http://IP:9200/索引名称/_search_shards 

output

{"nodes":{"d1FehlgHS-uj1BBQ09eIvQ":{"name":"iZuf6hn15mpmrtypv9ltogZ","ephemeral_id":"rFLJl1dlSiaNt6wczEE1-w","transport_address":"127.0.0.1:9300","attributes":{"ml.machine_memory":"4136448000","xpack.installed":"true","ml.max_open_jobs":"20"}}},"indices":{"aspnetindex":{}},"shards":[[{"state":"STARTED","primary":true,"node":"d1FehlgHS-uj1BBQ09eIvQ","relocating_node":null,"shard":0,"index":"aspnetindex","allocation_id":{"id":"3x3IYBfZQTyhK3CN4Uh3ng"}}]]}

6.查看索引的mapping

http://IP:9200/索引名称/_mapping

output

{"aspnetindex":{"mappings":{"properties":{"Body":{"type":"text","boost":1.1,"store":true,"analyzer":"ik_max_word","search_analyzer":"ik_smart"},"ChineseWordCount":{"type":"float","doc_values":false,"ignore_malformed":true,"coerce":true},"CreatedOn":{"type":"date","store":true},"Slug":{"type":"text","store":true,"index_options":"docs"},"Tags":{"type":"text","store":true,"index_options":"docs"},"Title":{"type":"text","boost":1.5,"store":true,"analyzer":"ik_max_word"},"id":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"metaDescription":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"metaKeywords":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"metaTitle":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}}}}}

 查看所有索引情况,可以输出索引名称,映射及分词器

https://es.iaspnetcore.com/_all
https://es.iaspnetcore.com/_all?pretty=true
https://es.iaspnetcore.com/wwwiaspnetcoreindex/_mapping

 

查询

https://es.iaspnetcore.com/wwwiaspnetcoreindex/_search?q=Title:Core
https://es.iaspnetcore.com/wwwiaspnetcoreindex/_search?q=Title:Core&pretty=true

output

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 396,
      "relation" : "eq"
    },
    "max_score" : 2.0222034,
    "hits" : [
      {
        "_index" : "wwwiaspnetcoreindex",
        "_type" : "_doc",
        "_id" : "5b919b6248c4e411f885f671",
        "_score" : 2.0222034,
        "_source" : {
          "id" : "5b919b6248c4e411f885f671",
          "Slug" : "http://www.iaspnetcore.com/Blog/BlogPost/5b919b6248c4e411f885f671/awesome-net-core",
          "Title" : " Awesome .NET Core",
          "Body" : "1.Awesome .NET Core\r\nA collection of awesome .NET Core frameworks, libraries, tools, resources and software.\r\nhttps://github.com/thangchung/awesome-dotnet-core\r\n&nbsp;\r\n&nbsp;..",
          "CreatedOn" : "2018-09-06T21:25:54.4780000Z",
          "Tags" : [
            ".NET Core",
            "ASP.NET Core"
          ],
          "ChineseWordCount" : 0
        }
      },
      {
        "_index" : "wwwiaspnetcoreindex",
        "_type" : "_doc",
        "_id" : "584f2d4084cd454424dd1b8c",
        "_score" : 2.0171964,
        "_source" : {
          "id" : "584f2d4084cd454424dd1b8c",
          "Slug" : "http://www.iaspnetcore.com/Blog/BlogPost/584f2d4084cd454424dd1b8c/net-core-first-office-open-source-cross-platform-component-npoi-core",
          "Title" : ".NET Core 首例 Office 开源跨平台组件(NPOI Core)",
          "Body" : "http://www.cnblogs.com/savorboard/p/netcore-npoi.html#!comments\r\n前言\r\n最近项目中,需要使用到 Excel 导出,找了一圈发现没有适用于 .NET Core的,不依赖Office和操作系统限制的 Office 组件,于是萌生了把 NPOI 适配并移植到 .NET Core 的想法。\r\nNPOI 的介绍不多说了,不了解的可以看一下&nbsp;NPOI百度百科&nbsp;的介绍,在此感谢瞿总和他的团队的贡献。\r\nNPOI 的移植之路并非想象的那么容易,因为其依赖了 System.Drawing 和 System.Window.Forms 两个组件,还有一个第三方的 SharpZipLib 库,在 GitHub 克隆了最新的代码并且转换为 NetStandrad 1.6 编译之后,出现了数不清的错误,应该有上千个吧,在经过一天的努力之后(包括删除,修改,重写),错误数量已经减少到了100多个,50多个,20多个,编译通过。\r\n在移植的过程中可以真切感受到当初NPOI的作者在写这些代码时候的辛苦努力,因为NPOI最初是基于 .Net Framework 1.1 框架写的,那个时候没有泛型,没有var,没有很多的现成的类库,全都是靠最基础的一些数据结构来实现,虽然里面的很多种写法在目前看来可以很大程序的精简,但是在当时的条件下 真的是不容易。\r\n在通过编译之后,心里想着应该问题不大了,于是测试了一下,不幸的是,各种问题,又经过半天的调整之后,打算放弃了。 于是又去 github 上面搜索看看有没有其他什么解决方案之类的,无意间搜索到了一个 NPOI.Core 的一个项目,是一个老外移植的 NPOI 到Core平台,原来已经有人做了Core的移植了,克隆下来之后发现编译不过,又进去看了一下代码,这个库目前依赖于Windows平台,而我们项目是运行在CentOS的,其并不能在Linux上运行,看来还是空欢喜一场。\r\n怎么办? 于是,又一次重构开始了,有了前一次的重构经验之后,这一次可谓是轻车熟路了,NPOI Core 库 里面使用了很多.NET Core netstandrad 标准不支持的 Hashtable 和 ArrayList 等数据结构,这些已经被新的泛型 Directory 和 List 替代了,还有依赖的 SharpZipLib 等压缩组件也都替换成了 NetStandrad 的实现,当然还有其他很多杂七杂八的就不细说了,最后,终于 netstandrad 1.6 下编译通过。\r\n通过之后,本地 visual studio 下 新建了一个项目,简单测试了导出 Excel 的功能,没问题,也没有报错,心里很开心...。 这个时候我在想,最关键的就是能不能在Linux上正常运行了,其实这个时候我心里想我已经把依赖于.NET Framework 的各种类都换成了net standrad了,应该问题不大了。\r\n然后在一顿 dotnet publish 之后,把部署包传到了 Linux 下进行测试,果然,运行通过,并没有抛出任何异常,而且Excel也生成了,把Excel传输到windows上使用office打开,完美...\r\n然后紧接着就是继续各种测试了,在测试到 Word 的导出功能时候,出问题了。因为NPOI 天生对Word的功能支持的并不是很完善,心想是不是.NET Framewok下导出也不行啊,于是又新建的一个.NET Framework的项目,使用NPOI Team提供的 dll 测试,发现可以导出。然后就开始跟代码,各种测试,重构,最终Word的导出功能也没有问题了。紧接着又发到了Linux上测试,没有问题,心里的石头落地了。\r\n在通过测试之后,本想着把这次调整的代码以 PR 的形式推送给原作者的时候,发现好像原作者已经不维护这个项目了,无奈,只能自己发布NuGet了。\r\n于是我就把它重新发布到 NuGet 了,如果使用的过程中有问题,大家可以去我的github下面提交 issue。\r\nGitHub&nbsp;:&nbsp;https://github.com/yuleyule66/Npoi.Core\r\nNuGet&nbsp;:\r\nGetting Started\r\n导出 Excel\r\n本示例代码包含:\r\n多个 Sheet\r\n合并单元格\r\n自动调整列宽\r\n填充背景色\r\nvar newFile = @\"newbook.core.xlsx\";\r\nusing (var fs = new FileStream(newFile, FileMode.Create, FileAccess.Write)) {\r\n    IWorkbook workbook = new XSSFWorkbook();\r\n    ISheet sheet1 = workbook.CreateSheet(\"Sheet1\");\r\n    sheet1.AddMergedRegion(new CellRangeAddress(0, 0, 0, 10));\r\n    var rowIndex = 0;\r\n    IRow row = sheet1.CreateRow(rowIndex);\r\n    row.Height = 30 * 80;\r\n    row.CreateCell(0).SetCellValue(\"这是单元格内容,可以设置很长,看能不能自动调整列宽\");\r\n    sheet1.AutoSizeColumn(0);\r\n    rowIndex++;\r\n    var sheet2 = workbook.CreateSheet(\"Sheet2\");\r\n    var style1 = workbook.CreateCellStyle();\r\n    style1.FillForegroundColor = HSSFColor.Blue.Index2;\r\n    style1.FillPattern = FillPattern.SolidForeground;\r\n    var style2 = workbook.CreateCellStyle();\r\n    style2.FillForegroundColor = HSSFColor.Yellow.Index2;\r\n    style2.FillPattern = FillPattern.SolidForeground;\r\n    var cell2 = sheet2.CreateRow(0).CreateCell(0);\r\n    cell2.CellStyle = style1;\r\n    cell2.SetCellValue(0);\r\n    cell2 = sheet2.CreateRow(1).CreateCell(0);\r\n    cell2.CellStyle = style2;\r\n    cell2.SetCellValue(1);\r\n    cell2 = sheet2.CreateRow(2).CreateCell(0);\r\n    cell2.CellStyle = style1;\r\n    cell2.SetCellValue(2);\r\n    cell2 = sheet2.CreateRow(3).CreateCell(0);\r\n    cell2.CellStyle = style2;\r\n    cell2.SetCellValue(3);\r\n    cell2 = sheet2.CreateRow(4).CreateCell(0);\r\n    cell2.CellStyle = style1;\r\n    cell2.SetCellValue(4);\r\n    workbook.Write(fs);\r\n}\r\n导出 Word\r\n本示例代码包含:\r\n设置段落对其方式\r\n设置段落字体\r\n设置段落缩进\r\n这是字体大小\r\nvar newFile2 = @\"newbook.core.docx\";\r\nusing (var fs = new FileStream(newFile2, FileMode.Create, FileAccess.Write)) {\r\n    XWPFDocument doc = new XWPFDocument();\r\n    var p0 = doc.CreateParagraph();\r\n    p0.Alignment = ParagraphAlignment.CENTER;\r\n    XWPFRun r0 = p0.CreateRun();\r\n    r0.FontFamily = \"microsoft yahei\";\r\n    r0.FontSize = 18;\r\n    r0.IsBold = true;\r\n    r0.SetText(\"这里是标题\");\r\n    var p1 = doc.CreateParagraph();\r\n    p1.Alignment = ParagraphAlignment.LEFT;\r\n    p1.IndentationFirstLine = 500;\r\n    XWPFRun r1 = p1.CreateRun();\r\n    r1.FontFamily = \"仿宋\";\r\n    r1.FontSize = 12;\r\n    r1.IsBold = true;\r\n    r1.SetText(\"这里是正文,这里是正文这里是正文这里是正文这里是正文这里是正文这里是正文这里是正文这里是正文这里是正文这里是正文这里是正文\");\r\n    doc.Write(fs);\r\n}\r\n总结\r\n据不完全统计,这应该是 .NET Core 首例 Office 跨平台 组件了,大家如果在使用的过程中有什么问题,可以在 Github 上提交Issue(尽量使用英文)。当然更希望你能够提交 PR 来帮助一起完善 NPOI。\r\n如果你觉得本篇博客对您有帮助的话,感谢您的【推荐】。\r\n如果你对 .NET Core 感兴趣可以关注我,我会定期在博客分享关于 .NET Core 的学习心得。\r\n本文地址:http://www.cnblogs.com/savorboard/p/netcore-npoi.html作者博客:Savorboard\r\n&nbsp;..",
          "CreatedOn" : "2016-12-12T23:05:36.7720000Z",
          "Tags" : [
            "Word"
          ],
          "ChineseWordCount" : 1239
        }
      },

2、查询方式

在es中查询大致分为两类:普通查询、复杂查询

普通查询只需要使用:Get Request即可

复杂查询:

SearchSourceBuilder(用于指定搜索条件时的附加条件:排序、分页、查询范围、展示\屏蔽字段、各种常见聚合查询等)

  QueryBuilders(用于指定搜索模式:全量搜索、附加单条件搜索、附加多条件搜索、模糊搜索等)

  SearchRequest(用于指定被操作的索引、加载搜索条件等操作)

分别对应search API的两种表单提交方式

1.http get:一种是“简易版”的查询字符串(query string)将所有参数通过查询字符串定义,

2.http post:另一种版本使用JSON完整的表示请求体(request body),这种富搜索语言叫做结构化查询语句(DSL)。

2.1 简单查询 一种是像传递URL参数一样去传递查询语句,被称为简单查询

如:

GET /library/books/_search //查询index为library,type为books的全部内容

GET /library/books/_search?q=price:10 //查询index为library,type为books中price等于10的

实列:

http://localhost:9200/stocksof10index/F10FileContentModel/_search?q=stockCode:SH600067

1.选择索引和类别 在所有索引的所有类型中搜索:

/_search

在索引gb的所有类型中搜索:

/gb/_search

在索引gb和us的所有类型中搜索:/gb,us/_search

在以g或u开头的索引的所有类型中搜索:/g*,u*/_search

在索引gb的类型user中搜索:/gb/user/_search 在索引gb和us的类型为user和tweet中搜索:/gb,us/user,tweet/_search

在所有索引的user和tweet中搜索:/_all/user,tweet/_search

2.分页 如果你想每页显示5个结果,页码从1到3,那请求如下:

GET /_search?size=5

GET /_search?size=5&from=5

GET /_search?size=5&from=10

3.搜索

(1)这个语句查询所有类型为tweet并在tweet字段中包含elasticsearch字符的文档

GET /_all/tweet/_search?q=tweet:elasticsearch

(2)查找name字段中包含"john"和tweet字段包含"mary"的结果 +name:john +tweet:mary

(3)搜索的语句:_all field name字段包含"mary"或"john" date晚于2014-09-10 _all字段包含"aggregations"或"geo"  +name:(mary john) +date:>2014-09-10 +(aggregations geo)

2.2 DSL查询  

  https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html.

 

1. Install Elasticsearch 5.4.1 on windows

1.1 Install Elasticsearch 5.4.1 on windows

1.下载elasticsearch5.4.1

https://www.elastic.co/downloads/elasticsearch

2、解压后copy  到:d:\web\elasticsearch-5.4.1 3、建立一个D:\Web\elasticsearch-5.4.1\bin\a.bat

d:cd D:\Web\elasticsearch-5.4.1\binelasticsearch.bat 执行a.bat检查是否正常运行

4、下载IK分词插件

https://github.com/medcl/elasticsearch-analysis-ik/releases 下载一编译的版本,elasticsearch-analysis-ik-1.10.5.zip,不要下载源码Source code (zip)

5、解压后copy 到D:\Web\elasticsearch-5.4.1\plugins\ 带目录,一个分词一个子目录 运行:\Web\elasticsearch-5.4.1\bin\a.bat 会显示系统已经加载的模块,其中有IK 6.elasticsearch-6.2.1 安装分词插件IK 在elasticsearch-6.2.1用4,5的安装方式,启动程序出错,采用下面的安装方式:

.\bin\elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.2.1/elasticsearch-analysis-ik-6.2.1.zip

1.检查es版本信息

http://IP:9200

output

{
  "name" : "iZuf6hn15mpmrtypv9ltogZ",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "9MRok0MvR62qtdbi8X3PBQ",
  "version" : {
    "number" : "7.4.2",
    "build_flavor" : "default",
    "build_type" : "deb",
    "build_hash" : "2f90bbf7b93631e52bafb59b3b049cb44ec25e96",
    "build_date" : "2019-10-28T20:40:44.881551Z",
    "build_snapshot" : false,
    "lucene_version" : "8.2.0",
    "minimum_wire_compatibility_version" : "6.8.0",
    "minimum_index_compatibility_version" : "6.0.0-beta1"
  },
  "tagline" : "You Know, for Search"
}

2.查看集群是否健康

http://IP:9200/_cat/health?v

output

epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1580168554 23:42:34  elasticsearch yellow          1         1      2   2    0    0        2             0                  -                 50.0%

3.查看节点列表

http://IP:9200/_cat/nodes?v

output

ip        heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
127.0.0.1           15          97   1    0.02    0.02     0.00 dilm      *      iZuf6hn15mpmrtypv9ltogZ
localhost:9200/_stats

4.列出所有索引及存储大小

http://IP:9200/_cat/indices?v

output

health status index               uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   index1 dCxwa7m7Q2iHRkCjWT3cFA   1   1       1012            0      9.4mb          9.4mb
yellow open   index               6awW2eDiTx2klpvhGVN0og   1   1          0            0       283b           283b

 

5.列出分片设置

http://IP:9200/索引名称/_search_shards 

output

{"nodes":{"d1FehlgHS-uj1BBQ09eIvQ":{"name":"iZuf6hn15mpmrtypv9ltogZ","ephemeral_id":"rFLJl1dlSiaNt6wczEE1-w","transport_address":"127.0.0.1:9300","attributes":{"ml.machine_memory":"4136448000","xpack.installed":"true","ml.max_open_jobs":"20"}}},"indices":{"aspnetindex":{}},"shards":[[{"state":"STARTED","primary":true,"node":"d1FehlgHS-uj1BBQ09eIvQ","relocating_node":null,"shard":0,"index":"aspnetindex","allocation_id":{"id":"3x3IYBfZQTyhK3CN4Uh3ng"}}]]}

6.查看索引的mapping

http://IP:9200/索引名称/_mapping

output

{"aspnetindex":{"mappings":{"properties":{"Body":{"type":"text","boost":1.1,"store":true,"analyzer":"ik_max_word","search_analyzer":"ik_smart"},"ChineseWordCount":{"type":"float","doc_values":false,"ignore_malformed":true,"coerce":true},"CreatedOn":{"type":"date","store":true},"Slug":{"type":"text","store":true,"index_options":"docs"},"Tags":{"type":"text","store":true,"index_options":"docs"},"Title":{"type":"text","boost":1.5,"store":true,"analyzer":"ik_max_word"},"id":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"metaDescription":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"metaKeywords":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}},"metaTitle":{"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}}}}}

 查看所有索引情况,可以输出索引名称,映射及分词器

https://es.iaspnetcore.com/_all
https://es.iaspnetcore.com/_all?pretty=true
https://es.iaspnetcore.com/wwwiaspnetcoreindex/_mapping

 

查询

https://es.iaspnetcore.com/wwwiaspnetcoreindex/_search?q=Title:Core
https://es.iaspnetcore.com/wwwiaspnetcoreindex/_search?q=Title:Core&pretty=true

output

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 396,
      "relation" : "eq"
    },
    "max_score" : 2.0222034,
    "hits" : [
      {
        "_index" : "wwwiaspnetcoreindex",
        "_type" : "_doc",
        "_id" : "5b919b6248c4e411f885f671",
        "_score" : 2.0222034,
        "_source" : {
          "id" : "5b919b6248c4e411f885f671",
          "Slug" : "http://www.iaspnetcore.com/Blog/BlogPost/5b919b6248c4e411f885f671/awesome-net-core",
          "Title" : " Awesome .NET Core",
          "Body" : "1.Awesome .NET Core\r\nA collection of awesome .NET Core frameworks, libraries, tools, resources and software.\r\nhttps://github.com/thangchung/awesome-dotnet-core\r\n&nbsp;\r\n&nbsp;..",
          "CreatedOn" : "2018-09-06T21:25:54.4780000Z",
          "Tags" : [
            ".NET Core",
            "ASP.NET Core"
          ],
          "ChineseWordCount" : 0
        }
      },
      {
        "_index" : "wwwiaspnetcoreindex",
        "_type" : "_doc",
        "_id" : "584f2d4084cd454424dd1b8c",
        "_score" : 2.0171964,
        "_source" : {
          "id" : "584f2d4084cd454424dd1b8c",
          "Slug" : "http://www.iaspnetcore.com/Blog/BlogPost/584f2d4084cd454424dd1b8c/net-core-first-office-open-source-cross-platform-component-npoi-core",
          "Title" : ".NET Core 首例 Office 开源跨平台组件(NPOI Core)",
          "Body" : "http://www.cnblogs.com/savorboard/p/netcore-npoi.html#!comments\r\n前言\r\n最近项目中,需要使用到 Excel 导出,找了一圈发现没有适用于 .NET Core的,不依赖Office和操作系统限制的 Office 组件,于是萌生了把 NPOI 适配并移植到 .NET Core 的想法。\r\nNPOI 的介绍不多说了,不了解的可以看一下&nbsp;NPOI百度百科&nbsp;的介绍,在此感谢瞿总和他的团队的贡献。\r\nNPOI 的移植之路并非想象的那么容易,因为其依赖了 System.Drawing 和 System.Window.Forms 两个组件,还有一个第三方的 SharpZipLib 库,在 GitHub 克隆了最新的代码并且转换为 NetStandrad 1.6 编译之后,出现了数不清的错误,应该有上千个吧,在经过一天的努力之后(包括删除,修改,重写),错误数量已经减少到了100多个,50多个,20多个,编译通过。\r\n在移植的过程中可以真切感受到当初NPOI的作者在写这些代码时候的辛苦努力,因为NPOI最初是基于 .Net Framework 1.1 框架写的,那个时候没有泛型,没有var,没有很多的现成的类库,全都是靠最基础的一些数据结构来实现,虽然里面的很多种写法在目前看来可以很大程序的精简,但是在当时的条件下 真的是不容易。\r\n在通过编译之后,心里想着应该问题不大了,于是测试了一下,不幸的是,各种问题,又经过半天的调整之后,打算放弃了。 于是又去 github 上面搜索看看有没有其他什么解决方案之类的,无意间搜索到了一个 NPOI.Core 的一个项目,是一个老外移植的 NPOI 到Core平台,原来已经有人做了Core的移植了,克隆下来之后发现编译不过,又进去看了一下代码,这个库目前依赖于Windows平台,而我们项目是运行在CentOS的,其并不能在Linux上运行,看来还是空欢喜一场。\r\n怎么办? 于是,又一次重构开始了,有了前一次的重构经验之后,这一次可谓是轻车熟路了,NPOI Core 库 里面使用了很多.NET Core netstandrad 标准不支持的 Hashtable 和 ArrayList 等数据结构,这些已经被新的泛型 Directory 和 List 替代了,还有依赖的 SharpZipLib 等压缩组件也都替换成了 NetStandrad 的实现,当然还有其他很多杂七杂八的就不细说了,最后,终于 netstandrad 1.6 下编译通过。\r\n通过之后,本地 visual studio 下 新建了一个项目,简单测试了导出 Excel 的功能,没问题,也没有报错,心里很开心...。 这个时候我在想,最关键的就是能不能在Linux上正常运行了,其实这个时候我心里想我已经把依赖于.NET Framework 的各种类都换成了net standrad了,应该问题不大了。\r\n然后在一顿 dotnet publish 之后,把部署包传到了 Linux 下进行测试,果然,运行通过,并没有抛出任何异常,而且Excel也生成了,把Excel传输到windows上使用office打开,完美...\r\n然后紧接着就是继续各种测试了,在测试到 Word 的导出功能时候,出问题了。因为NPOI 天生对Word的功能支持的并不是很完善,心想是不是.NET Framewok下导出也不行啊,于是又新建的一个.NET Framework的项目,使用NPOI Team提供的 dll 测试,发现可以导出。然后就开始跟代码,各种测试,重构,最终Word的导出功能也没有问题了。紧接着又发到了Linux上测试,没有问题,心里的石头落地了。\r\n在通过测试之后,本想着把这次调整的代码以 PR 的形式推送给原作者的时候,发现好像原作者已经不维护这个项目了,无奈,只能自己发布NuGet了。\r\n于是我就把它重新发布到 NuGet 了,如果使用的过程中有问题,大家可以去我的github下面提交 issue。\r\nGitHub&nbsp;:&nbsp;https://github.com/yuleyule66/Npoi.Core\r\nNuGet&nbsp;:\r\nGetting Started\r\n导出 Excel\r\n本示例代码包含:\r\n多个 Sheet\r\n合并单元格\r\n自动调整列宽\r\n填充背景色\r\nvar newFile = @\"newbook.core.xlsx\";\r\nusing (var fs = new FileStream(newFile, FileMode.Create, FileAccess.Write)) {\r\n    IWorkbook workbook = new XSSFWorkbook();\r\n    ISheet sheet1 = workbook.CreateSheet(\"Sheet1\");\r\n    sheet1.AddMergedRegion(new CellRangeAddress(0, 0, 0, 10));\r\n    var rowIndex = 0;\r\n    IRow row = sheet1.CreateRow(rowIndex);\r\n    row.Height = 30 * 80;\r\n    row.CreateCell(0).SetCellValue(\"这是单元格内容,可以设置很长,看能不能自动调整列宽\");\r\n    sheet1.AutoSizeColumn(0);\r\n    rowIndex++;\r\n    var sheet2 = workbook.CreateSheet(\"Sheet2\");\r\n    var style1 = workbook.CreateCellStyle();\r\n    style1.FillForegroundColor = HSSFColor.Blue.Index2;\r\n    style1.FillPattern = FillPattern.SolidForeground;\r\n    var style2 = workbook.CreateCellStyle();\r\n    style2.FillForegroundColor = HSSFColor.Yellow.Index2;\r\n    style2.FillPattern = FillPattern.SolidForeground;\r\n    var cell2 = sheet2.CreateRow(0).CreateCell(0);\r\n    cell2.CellStyle = style1;\r\n    cell2.SetCellValue(0);\r\n    cell2 = sheet2.CreateRow(1).CreateCell(0);\r\n    cell2.CellStyle = style2;\r\n    cell2.SetCellValue(1);\r\n    cell2 = sheet2.CreateRow(2).CreateCell(0);\r\n    cell2.CellStyle = style1;\r\n    cell2.SetCellValue(2);\r\n    cell2 = sheet2.CreateRow(3).CreateCell(0);\r\n    cell2.CellStyle = style2;\r\n    cell2.SetCellValue(3);\r\n    cell2 = sheet2.CreateRow(4).CreateCell(0);\r\n    cell2.CellStyle = style1;\r\n    cell2.SetCellValue(4);\r\n    workbook.Write(fs);\r\n}\r\n导出 Word\r\n本示例代码包含:\r\n设置段落对其方式\r\n设置段落字体\r\n设置段落缩进\r\n这是字体大小\r\nvar newFile2 = @\"newbook.core.docx\";\r\nusing (var fs = new FileStream(newFile2, FileMode.Create, FileAccess.Write)) {\r\n    XWPFDocument doc = new XWPFDocument();\r\n    var p0 = doc.CreateParagraph();\r\n    p0.Alignment = ParagraphAlignment.CENTER;\r\n    XWPFRun r0 = p0.CreateRun();\r\n    r0.FontFamily = \"microsoft yahei\";\r\n    r0.FontSize = 18;\r\n    r0.IsBold = true;\r\n    r0.SetText(\"这里是标题\");\r\n    var p1 = doc.CreateParagraph();\r\n    p1.Alignment = ParagraphAlignment.LEFT;\r\n    p1.IndentationFirstLine = 500;\r\n    XWPFRun r1 = p1.CreateRun();\r\n    r1.FontFamily = \"仿宋\";\r\n    r1.FontSize = 12;\r\n    r1.IsBold = true;\r\n    r1.SetText(\"这里是正文,这里是正文这里是正文这里是正文这里是正文这里是正文这里是正文这里是正文这里是正文这里是正文这里是正文这里是正文\");\r\n    doc.Write(fs);\r\n}\r\n总结\r\n据不完全统计,这应该是 .NET Core 首例 Office 跨平台 组件了,大家如果在使用的过程中有什么问题,可以在 Github 上提交Issue(尽量使用英文)。当然更希望你能够提交 PR 来帮助一起完善 NPOI。\r\n如果你觉得本篇博客对您有帮助的话,感谢您的【推荐】。\r\n如果你对 .NET Core 感兴趣可以关注我,我会定期在博客分享关于 .NET Core 的学习心得。\r\n本文地址:http://www.cnblogs.com/savorboard/p/netcore-npoi.html作者博客:Savorboard\r\n&nbsp;..",
          "CreatedOn" : "2016-12-12T23:05:36.7720000Z",
          "Tags" : [
            "Word"
          ],
          "ChineseWordCount" : 1239
        }
      },

1.2 查询方式 search API有两种表单

一种是“简易版”的查询字符串(query string)将所有参数通过查询字符串定义,另一种版本使用JSON完整的表示请求体(request body),这种富搜索语言叫做结构化查询语句(DSL)。

(一)简单查询 一种是像传递URL参数一样去传递查询语句,被称为简单查询

如:

GET /library/books/_search //查询index为library,type为books的全部内容

GET /library/books/_search?q=price:10 //查询index为library,type为books中price等于10的

实列:

http://localhost:9200/stocksof10index/F10FileContentModel/_search?q=stockCode:SH600067

1.选择索引和类别 在所有索引的所有类型中搜索:

/_search

在索引gb的所有类型中搜索:

/gb/_search

在索引gb和us的所有类型中搜索:/gb,us/_search

在以g或u开头的索引的所有类型中搜索:/g*,u*/_search

在索引gb的类型user中搜索:/gb/user/_search 在索引gb和us的类型为user和tweet中搜索:/gb,us/user,tweet/_search

在所有索引的user和tweet中搜索:/_all/user,tweet/_search

2.分页 如果你想每页显示5个结果,页码从1到3,那请求如下:

GET /_search?size=5

GET /_search?size=5&from=5

GET /_search?size=5&from=10

3.搜索

(1)这个语句查询所有类型为tweet并在tweet字段中包含elasticsearch字符的文档

GET /_all/tweet/_search?q=tweet:elasticsearch

(2)查找name字段中包含"john"和tweet字段包含"mary"的结果 +name:john +tweet:mary

(3)搜索的语句:_all field name字段包含"mary"或"john" date晚于2014-09-10 _all字段包含"aggregations"或"geo"  +name:(mary john) +date:>2014-09-10 +(aggregations geo)

(二)DSL查询  

  https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html.

 

Useful links

https://www.elastic.co/guide/en/elasticsearch/reference/current/zip-windows.html#install-windows