日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

可搜索的文件? 是的你可以。 选择AsciiDoc的另一个原因

發布時間:2023/12/3 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 可搜索的文件? 是的你可以。 选择AsciiDoc的另一个原因 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Elasticsearch是一個基于Apache Lucene的靈活,功能強大的開源,分布式實時云搜索和分析引擎,可提供全文搜索功能。 它是面向文檔且無架構的。

Asciidoctor是一個純Ruby處理器,用于將AsciiDoc源文件和字符串轉換為HTML 5 , DocBook 4.5和其他格式。 除了Asciidoctor Ruby部分之外,還有一個Asciidoctor-java-integration項目,該項目使我們可以從Java調用Asciidoctor函數,而無需注意正在執行Ruby代碼。

在這篇文章中,我們將了解如何在AsciiDoc文檔上使用Elasticsearch ,以使其可通過其標題信息或內容進行搜索。

讓我們添加所需的依賴項:

<dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.11</version><scope>test</scope></dependency><dependency><groupId>com.googlecode.lambdaj</groupId><artifactId>lambdaj</artifactId><version>2.3.3</version></dependency><dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>0.90.1</version></dependency><dependency><groupId>org.asciidoctor</groupId><artifactId>asciidoctor-java-integration</artifactId><version>0.1.3</version></dependency></dependencies>

Lambdaj庫用于將AsciiDoc文件轉換為json文檔。

現在我們可以啟動一個Elasticsearch實例,在我們的例子中,它將是一個嵌入式實例。

node = nodeBuilder().local(true).node();

下一步是解析AsciiDoc文檔標題,讀取其內容并將其轉換為json文檔。

存儲在Elasticsearch中的json文檔的示例可以是:

{"title":"Asciidoctor Maven plugin 0.1.2 released!","authors":[{"author":"Jason Porter","email":"example@mail.com"}],"version":null,"content":"= Asciidoctor Maven plugin 0.1.2 released!.....","tags":["release","plugin"] }

而對于一個AsciiDoc文件轉換成JSON文件,我們將使用由Elasticsearch 的Java API提供了以編程方式創建JSON文件XContentBuilder類。

package com.lordofthejars.asciidoctor;import static org.elasticsearch.common.xcontent.XContentFactory.*; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.util.List;import org.asciidoctor.Asciidoctor; import org.asciidoctor.Author; import org.asciidoctor.DocumentHeader; import org.asciidoctor.internal.IOUtils; import org.elasticsearch.common.xcontent.XContentBuilder;import ch.lambdaj.function.convert.Converter;public class AsciidoctorFileJsonConverter implements Converter<File, XContentBuilder> {private Asciidoctor asciidoctor;public AsciidoctorFileJsonConverter() {this.asciidoctor = Asciidoctor.Factory.create();}public XContentBuilder convert(File asciidoctor) {DocumentHeader documentHeader = this.asciidoctor.readDocumentHeader(asciidoctor);XContentBuilder jsonContent = null;try {jsonContent = jsonBuilder().startObject().field("title", documentHeader.getDocumentTitle()).startArray("authors");Author mainAuthor = documentHeader.getAuthor();jsonContent.startObject().field("author", mainAuthor.getFullName()).field("email", mainAuthor.getEmail()).endObject();List<Author> authors = documentHeader.getAuthors();for (Author author : authors) {jsonContent.startObject().field("author", author.getFullName()).field("email", author.getEmail()).endObject();}jsonContent.endArray().field("version", documentHeader.getRevisionInfo().getNumber()).field("content", readContent(asciidoctor)).array("tags", parseTags((String)documentHeader.getAttributes().get("tags"))).endObject();} catch (IOException e) {throw new IllegalArgumentException(e);}return jsonContent;}private String[] parseTags(String tags) {tags = tags.substring(1, tags.length()-1);return tags.split(", ");}private String readContent(File content) throws FileNotFoundException {return IOUtils.readFull(new FileInputStream(content));}}

基本上,我們通過調用startObject方法來啟動新對象, field方法來添加新字段以及startArray來啟動數組來構建json文檔。 然后,將使用此生成器以json格式呈現等效對象。 請注意,我們使用的是從Asciidoctor類返回頭從AsciiDoc文件屬性沒有閱讀和渲染整個文檔readDocumentHeader方法。 最后,內容字段設置為所有文檔內容。

現在我們準備開始為文檔建立索引。 注意populateData方法接收一個Client對象作為參數。 該對象來自Elasticsearch Java API ,表示與Elasticsearch數據庫的連接。

import static ch.lambdaj.Lambda.convert; //....private void populateData(Client client) throws IOException {List<File> asciidoctorFiles = new ArrayList<File>() {{add(new File("target/test-classes/java_release.adoc"));add(new File("target/test-classes/maven_release.adoc"));}};List<XContentBuilder> jsonDocuments = convertAsciidoctorFilesToJson(asciidoctorFiles);for (int i=0; i < jsonDocuments.size(); i++) {client.prepareIndex("docs", "asciidoctor", Integer.toString(i)).setSource(jsonDocuments.get(i)).execute().actionGet();}client.admin().indices().refresh(new RefreshRequest("docs")).actionGet(); }private List<XContentBuilder> convertAsciidoctorFilesToJson(List<File> asciidoctorFiles) {return convert(asciidoctorFiles, new AsciidoctorFileJsonConverter()); }

重要的是要注意,算法的第一部分是通過使用先前的轉換器類和Lambdaj項目的convert方法將所有我們的AsciiDoc文件(在本例中為兩個)轉換為XContentBuilder實例。

如果您愿意,可以在https://github.com/asciidoctor/asciidoctor.github.com/blob/develop/news/asciidoctor-java-integration-0-1-3-中查看本示例中使用的兩個文檔。 release.adoc和https://github.com/asciidoctor/asciidoctor.github.com/blob/develop/news/asciidoctor-maven-plugin-0-1-2-released.adoc 。

下一部分是在一個索引中插入文檔。 這是通過使用prepareIndex方法完成的,該方法需要一個索引名稱( docs ),一個索引類型( asciidoctor )和要插入的文檔的ID 。 然后我們調用setSource方法,該方法將XContentBuilder對象轉換為json ,最后通過調用execute()。actionGet() ,將數據發送到數據庫。

僅由于我們使用的是Elasticsearch的嵌入式實例(在生產中不需要此部分),才需要最后一步,該實例通過調用refresh方法刷新索引。

之后,我們可以開始查詢Elasticsearch以從我們的AsciiDoc文檔中檢索信息。

讓我們從一個非常簡單的示例開始,該示例返回所有插入的文檔:

SearchResponse response = client.prepareSearch().execute().actionGet();

接下來,我們將搜索Alex Soto編寫的所有文檔,在本例中是其中一個。

import static org.elasticsearch.index.query.QueryBuilders.matchQuery; //.... QueryBuilder matchQuery = matchQuery("author", "Alex Soto");QueryBuilder matchQuery = matchQuery("author", "Alexander Soto");

請注意,我正在搜索字段作者字符串Alex Soto ,該字符串僅返回一個。 另一個文檔由Jason編寫。 但是有趣的是,如果您搜索Alexander Soto ,那么將返回相同的文檔。 Elasticsearch非常聰明,可以知道Alex和Alexander是非常相似的名字,因此它也返回了文檔。

更多查詢,如何查找由Alex而不是Soto撰寫的文檔。

import static org.elasticsearch.index.query.QueryBuilders.fieldQuery;//....QueryBuilder matchQuery = fieldQuery("author", "+Alex -Soto");

在這種情況下,當然不會返回任何結果。 請注意,在這種情況下,我們使用字段查詢而不是術語查詢,并且使用+和–符號來排除和包括單詞。

您也可以找到所有包含title上 釋放的單詞的文檔。

import static org.elasticsearch.index.query.QueryBuilders.matchQuery;//....QueryBuilder matchQuery = matchQuery("title", "released");

最后,讓我們找到所有談論0.1.2版本的文檔,在這種情況下,只有一個文檔談論它,另一個文檔談論0.1.3。

QueryBuilder matchQuery = matchQuery("content", "0.1.2");

現在我們只需要將查詢發送到Elasticsearch數據庫即可,這是通過使用prepareSearch方法完成的。

SearchResponse response = client.prepareSearch("docs").setTypes("asciidoctor").setQuery(matchQuery).execute().actionGet();SearchHits hits = response.getHits();for (SearchHit searchHit : hits) {System.out.println(searchHit.getSource().get("content")); }

請注意,在這種情況下,我們通過控制臺打印AsciiDoc內容,但是您可以使用asciidoctor.render(String content,Options options)方法將內容呈現為所需格式。

因此,在本文中,我們看到了如何使用Elasticsearch為文檔建立索引,如何使用Asciidoctor-java-integration項目從AsciiDoc文件中獲取一些重要信息,以及最后如何對插入的文檔執行一些查詢。 當然, Elasticsearch中還有更多種查詢,但是本文的目的并不是要探索Elasticsearch的所有可能性。

另外,請注意使用AsciiDoc格式編寫文檔有多么重要。 您無需花費太多精力就可以為文檔建立搜索引擎。 另一方面,請設想使用任何專有的二進制格式(例如Microsoft Word)來實現相同的所有代碼。 因此,我們已經說明了使用AsciiDoc而不是其他格式的另一個原因。

參考: 可搜索的文件? 是的你可以。 從我們的JCG合作伙伴 Alex Soto 選擇 “ One Jar To Rule All All”博客的另一個原因 。

翻譯自: https://www.javacodegeeks.com/2013/06/searchable-documents-yes-you-can-another-reason-to-choose-asciidoc.html

總結

以上是生活随笔為你收集整理的可搜索的文件? 是的你可以。 选择AsciiDoc的另一个原因的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。