使用JAVA语言,写一个elastic案例
时间: 2024-01-25 19:02:41 浏览: 115
好的,我可以为您提供一个简单的Java程序,用于在Elasticsearch中创建索引并执行一些基本操作:
首先,您需要确保已经安装了Elasticsearch并启动了它。然后,您需要添加以下依赖项到您的Maven项目中:
```
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.10.1</version>
</dependency>
```
接下来,您可以使用以下代码来创建一个索引并添加一些文档:
```
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.completion.CompletionSuggestionBuilder;
import org.elasticsearch.search.suggest.term.TermSuggestionBuilder;
import org.elasticsearch.search.suggest.term.TermSuggestion;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
public class ElasticExample {
private RestHighLevelClient client;
public ElasticExample() {
// 初始化客户端
// 填写Elasticsearch的地址和端口号
client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http")
)
);
}
public void createIndex() throws IOException {
// 创建一个索引
String indexName = "books";
CreateIndexRequest request = new CreateIndexRequest(indexName);
client.indices().create(request, RequestOptions.DEFAULT);
// 创建一些文档
IndexRequest request1 = new IndexRequest(indexName);
Map<String, Object> book1 = new HashMap<>();
book1.put("title", "Java入门");
book1.put("author", "张三");
book1.put("price", 45.0);
request1.id("1");
request1.source(book1);
client.index(request1, RequestOptions.DEFAULT);
IndexRequest request2 = new IndexRequest(indexName);
Map<String, Object> book2 = new HashMap<>();
book2.put("title", "Python编程");
book2.put("author", "李四");
book2.put("price", 60.0);
request2.id("2");
request2.source(book2);
client.index(request2, RequestOptions.DEFAULT);
}
public void search() throws IOException {
// 查询
String indexName = "books";
SearchRequest request = new SearchRequest(indexName);
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query(QueryBuilders.matchQuery("title", "Java"));
sourceBuilder.from(0);
sourceBuilder.size(10);
sourceBuilder.sort("price", SortOrder.ASC);
HighlightBuilder highlightBuilder = new HighlightBuilder();
highlightBuilder.field("title");
highlightBuilder.preTags("<em>");
highlightBuilder.postTags("</em>");
sourceBuilder.highlighter(highlightBuilder);
SuggestBuilder suggestBuilder = new SuggestBuilder();
CompletionSuggestionBuilder completionSuggestionBuilder = new CompletionSuggestionBuilder("suggest");
completionSuggestionBuilder.text("J");
suggestBuilder.addSuggestion("suggest", completionSuggestionBuilder);
TermSuggestionBuilder termSuggestionBuilder = new TermSuggestionBuilder("suggest");
termSuggestionBuilder.text("Java");
suggestBuilder.addSuggestion("suggest", termSuggestionBuilder);
request.source(sourceBuilder);
request.suggest(suggestBuilder);
SearchResponse response = client.search(request, RequestOptions.DEFAULT);
SearchHits hits = response.getHits();
for (SearchHit hit : hits) {
String id = hit.getId();
String title = hit.getSourceAsMap().get("title").toString();
String author = hit.getSourceAsMap().get("author").toString();
double price = Double.parseDouble(hit.getSourceAsMap().get("price").toString());
Map<String, HighlightField> highlightFields = hit.getHighlightFields();
HighlightField titleField = highlightFields.get("title");
String highlightTitle = titleField.fragments()[0].toString();
System.out.println("id: " + id + ", title: " + highlightTitle + ", author: " + author + ", price: " + price);
}
Suggest suggest = response.getSuggest();
CompletionSuggestion completionSuggestion = suggest.getSuggestion("suggest");
for (CompletionSuggestion.Entry entry : completionSuggestion.getEntries()) {
for (CompletionSuggestion.Entry.Option option : entry.getOptions()) {
System.out.println("Suggestion: " + option.getText().toString());
}
}
TermSuggestion termSuggestion = suggest.getSuggestion("suggest");
for (TermSuggestion.Entry entry : termSuggestion.getEntries()) {
for (TermSuggestion.Entry.Option option : entry) {
System.out.println("Suggestion: " + option.getText().toString());
}
}
}
public static void main(String[] args) throws IOException {
ElasticExample example = new ElasticExample();
example.createIndex();
example.search();
}
}
```
这个程序创建了一个名为“books”的索引,并向其中添加了两个文档。然后,它执行了一个查询,返回了所有标题包含“Java”的文档,并对标题进行了高亮显示。此外,它还展示了如何使用自动完成功能和建议功能来提供搜索建议。
请注意,这只是一个简单的示例程序,您需要根据自己的需求进行修改和扩展。
阅读全文