elasticsearchHow can I use Elasticsearch to access Habr content?
Elasticsearch can be used to access Habr content in several ways.
- Using the Elasticsearch API, you can create an index of Habr content and query it for specific results. For example, the following code will create an index called "habr" and query it for posts containing the word "Elasticsearch":
PUT /habr
{
"mappings": {
"post": {
"properties": {
"content": {
"type": "text"
}
}
}
}
}
POST /habr/_search
{
"query": {
"match": {
"content": "Elasticsearch"
}
}
}
Output example
{
"took": 11,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.2876821,
"hits": [
{
"_index": "habr",
"_type": "post",
"_id": "1",
"_score": 0.2876821,
"_source": {
"content": "This is a post about Elasticsearch"
}
},
{
"_index": "habr",
"_type": "post",
"_id": "2",
"_score": 0.2876821,
"_source": {
"content": "This is another post about Elasticsearch"
}
}
]
}
}
- You can also use the Elasticsearch Java API to access Habr content. The following code will create an index called "habr" and query it for posts containing the word "Elasticsearch":
// Create the index
CreateIndexRequest request = new CreateIndexRequest("habr");
CreateIndexResponse response = client.indices().create(request);
// Query the index
SearchRequest searchRequest = new SearchRequest("habr");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
searchSourceBuilder.query(QueryBuilders.matchQuery("content", "Elasticsearch"));
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest);
// Print the results
SearchHit[] searchHits = searchResponse.getHits().getHits();
for (SearchHit hit : searchHits) {
System.out.println(hit.getSourceAsString());
}
Output example
{"content": "This is a post about Elasticsearch"}
{"content": "This is another post about Elasticsearch"}
Helpful links
More of Elasticsearch
- How can I use Elasticsearch with Zammad?
- How can I store and query zoned datetime values in Elasticsearch?
- How do I use ElasticSearch to zip files?
- How can I use Elasticsearch and ZFS together?
- How can I use Elasticsearch to diagnose "yellow" issues?
- How do I set up an Elasticsearch Yum repository?
- How can I use YouTube to learn about Elasticsearch?
- How can I use Kibana to visualize data stored in Elasticsearch?
- How do I configure elasticsearch to use an XMS memory allocator?
- How do I configure elasticsearch xpack.security.transport.ssl?
See more codes...