asciidoc文件阅读_可搜索的文件? 是的你可以。 选择AsciiDoc的另一个原因
asciidoc文件閱讀
Elasticsearch是一個基于Apache Lucene的靈活,功能強(qiáng)大的開源,分布式實(shí)時云搜索和分析引擎,可提供全文搜索功能。 它是面向文檔且無架構(gòu)的。
Asciidoctor是一個純Ruby處理器,用于將AsciiDoc源文件和字符串轉(zhuǎn)換為HTML 5 , DocBook 4.5和其他格式。 除了Asciidoctor Ruby部分之外,還有一個Asciidoctor-java-integration項(xiàng)目,該項(xiàng)目使我們可以從Java調(diào)用Asciidoctor函數(shù),而無需注意正在執(zhí)行Ruby代碼。
在這篇文章中,我們將看到如何在AsciiDoc文檔上使用Elasticsearch ,以使其可通過其標(biāo)題信息或內(nèi)容進(jìn)行搜索。
讓我們添加所需的依賴項(xiàng):
<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文件轉(zhuǎn)換為json文檔。
現(xiàn)在我們可以啟動一個Elasticsearch實(shí)例,在本例中它將是一個嵌入式實(shí)例。
node = nodeBuilder().local(true).node();下一步是解析AsciiDoc文檔標(biāo)題,讀取其內(nèi)容并將其轉(zhuǎn)換為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文件轉(zhuǎn)換成JSON文件,我們將使用由Elasticsearch 的Java API提供了以編程方式創(chuàng)建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));}}基本上,我們通過調(diào)用startObject方法來啟動新對象, field方法來添加新字段以及startArray來啟動數(shù)組來構(gòu)建json文檔。 然后,將使用此生成器以json格式呈現(xiàn)等效對象。 請注意,我們使用的是從Asciidoctor類返回頭從AsciiDoc文件屬性沒有閱讀和渲染整個文檔readDocumentHeader方法。 最后,內(nèi)容字段設(shè)置為所有文檔內(nèi)容。
現(xiàn)在我們準(zhǔn)備開始為文檔建立索引。 注意populateData方法接收一個Client對象作為參數(shù)。 該對象來自Elasticsearch Java API ,表示與Elasticsearch數(shù)據(jù)庫的連接。
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()); }重要的是要注意,算法的第一部分是通過使用以前的轉(zhuǎn)換器類和Lambdaj項(xiàng)目的convert方法將所有我們的AsciiDoc文件(在本例中為兩個)轉(zhuǎn)換為XContentBuilder實(shí)例。
如果您愿意,可以在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 。
下一部分是在一個索引內(nèi)插入文檔。 這是通過使用prepareIndex方法完成的,該方法需要一個索引名稱( docs ),一個索引類型( asciidoctor )和要插入的文檔的ID 。 然后我們調(diào)用setSource方法,該方法將XContentBuilder對象轉(zhuǎn)換為json ,最后通過調(diào)用execute()。actionGet() ,將數(shù)據(jù)發(fā)送到數(shù)據(jù)庫。
僅由于我們使用的是Elasticsearch的嵌入式實(shí)例(在生產(chǎn)中不需要此部分),才需要最后一步,該實(shí)例通過調(diào)用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");在這種情況下,當(dāng)然不會返回任何結(jié)果。 請注意,在這種情況下,我們使用字段查詢而不是術(shù)語查詢,并且使用+和–符號來排除和包括單詞。
您也可以找到所有包含title上 釋放的單詞的文檔。
import static org.elasticsearch.index.query.QueryBuilders.matchQuery;//....QueryBuilder matchQuery = matchQuery("title", "released");最后,讓我們找到所有有關(guān)0.1.2發(fā)行版的文檔,在這種情況下,只有一個文檔在談?wù)撍?#xff0c;另一個文檔在談?wù)?.1.3。
QueryBuilder matchQuery = matchQuery("content", "0.1.2");現(xiàn)在,我們只需要將查詢發(fā)送到Elasticsearch數(shù)據(jù)庫即可,這是通過使用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內(nèi)容,但是您可以使用asciidoctor.render(String content,Options options)方法將內(nèi)容呈現(xiàn)為所需格式。
因此,在本文中,我們看到了如何使用Elasticsearch為文檔建立索引,如何使用Asciidoctor-java-integration項(xiàng)目從AsciiDoc文件中獲取一些重要信息,以及最后如何對插入的文檔執(zhí)行一些查詢。 當(dāng)然, Elasticsearch中還有更多種查詢,但是本文的目的并不是要探索Elasticsearch的所有可能性。
此外,請注意使用AsciiDoc格式編寫文檔的重要性。 您無需花費(fèi)太多精力就可以為文檔建立搜索引擎。 另一方面,設(shè)想使用任何專有的二進(jìn)制格式(例如Microsoft Word)來實(shí)現(xiàn)相同的所有代碼。 因此,我們已經(jīng)顯示了使用AsciiDoc而不是其他格式的另一個原因。
翻譯自: https://www.javacodegeeks.com/2013/06/searchable-documents-yes-you-can-another-reason-to-choose-asciidoc.html
asciidoc文件閱讀
總結(jié)
以上是生活随笔為你收集整理的asciidoc文件阅读_可搜索的文件? 是的你可以。 选择AsciiDoc的另一个原因的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 舰队安卓老游戏(舰队安卓)
- 下一篇: linux端口进程查看(linux端口进