Setting Up Postman for Elasticsearch Testing
Postman serves as a powerful GUI tool for testing RESTful APIs. After downloading the installer from the official website, execute the application file to complete the setup. Upon first launch, create an account or sign in to access the full functionality of the client.
REST API Methods in Elasticsearch
Elasticsearch supports standard HTTP methods for different operations:
- POST: Create new documents or indices. If no ID is specified, Elasticsearch auto-generates one.
- PUT: Update existing documents or indices.
- GET: Retrieve documents or indices.
- DELETE: Remove documents or indices.
Index names must be lowercase. When creating a document without specifying an ID, the system assigns a unique identifier automatically.
Querying Data with Highlighting
To search for documents, send a POST request to the search endpoint:
http://hostname:9200/index_name/type_name/_search
The following request body searches for documents where the title field contains "my" and highlights the matched term:
{
"query": {
"match": {
"title": {
"query": "my"
}
}
},
"highlight": {
"fields": {
"title": {}
}
}
}The response includes matching documents with highlighted terms wrapped in <em> tags:
{
"took": 3,
"timed_out": false,
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "indextest",
"_id": "i_WWCGIBMRJTfK5J3FTg",
"_score": 0.2876821,
"_source": {
"title": "this is my first elasticsearch test",
"content": "Elasticsearch is a highly scalable search engine."
},
"highlight": {
"title": ["this is <em>my</em> first elasticsearch test"]
}
}
]
}
}Integrating Elasticsearch with Java
Maven Dependencies
Add the required dependencies to your pom.xml:
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.0.0</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>transport</artifactId>
<version>6.0.0</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.44</version>
</dependency>
</dependencies>Retrieving Documents
Establish a connection to the Elasticsearch cluster and fetch a document by ID:
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import java.net.InetAddress;
public class EsClientTest {
private TransportClient client;
public void fetchDocument() throws Exception {
Settings esSettings = Settings.builder()
.put("cluster.name", "my-esLearn")
.build();
client = new PreBuiltTransportClient(esSettings)
.addTransportAddress(
new TransportAddress(InetAddress.getByName("127.0.0.1"), 9300)
);
GetResponse documentResponse = client
.prepareGet("blog", "article", "1")
.execute()
.actionGet();
System.out.println(documentResponse.getSource());
client.close();
}
}Bulk Indexing with Custom Analyzer
For bulk operations with a specific analyzer such as IK, use the BulkRequestBuilder:
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.doc.DocWriteResponse;
import org.elasticsearch.action.bulk.BulkItemResponse;
import org.elasticsearch.common.xcontent.XContentFactory;
import java.util.Date;
public void bulkIndexWithAnalyzer() throws Exception {
BulkRequestBuilder bulkBuilder = client.prepareBulk();
bulkBuilder.add(client.prepareIndex("twitter", "tweet", "1")
.setSource(XContentFactory.jsonBuilder()
.startObject()
.field("analyzer", "ik_max_word")
.field("user", "john_doe")
.field("postDate", new Date())
.field("message", "testing Elasticsearch integration")
.endObject()
)
);
bulkBuilder.add(client.prepareIndex("twitter", "tweet", "2")
.setSource(XContentFactory.jsonBuilder()
.startObject()
.field("user", "jane_doe")
.field("postDate", new Date())
.field("message", "second document entry")
.endObject()
)
);
BulkResponse bulkResponse = bulkBuilder.get();
if (bulkResponse.hasFailures()) {
System.err.println("Bulk operation failed: " + bulkResponse.buildFailureMessage());
}
for (BulkItemResponse item : bulkResponse.getItems()) {
DocWriteResponse response = item.getResponse();
System.out.println("Indexed into: " + response.getIndex());
}
}