当前位置:  首页>> 技术小册>> ElasticSearch零基础到实战

Elasticsearch提供了一个可扩展和高性能的解决方案,用于搜索和分析大型数据集。Elasticsearch使用正排索引和倒排索引来加速搜索和查询操作。

正排索引

正排索引是一个文档到字段的映射。每个文档都包含一个或多个字段,例如标题、内容、日期等等。正排索引包含了所有的文档和它们的字段值。在搜索过程中,查询语句会被匹配到正排索引中对应的字段值上,然后返回相关的文档。

下面是一个简单的示例,展示如何使用Elasticsearch的Java API来创建一个包含两个字段的索引,一个是字符串类型,一个是整数类型。

  1. import org.elasticsearch.client.RestHighLevelClient;
  2. import org.elasticsearch.client.indices.CreateIndexRequest;
  3. import org.elasticsearch.client.indices.CreateIndexResponse;
  4. import org.elasticsearch.common.xcontent.XContentBuilder;
  5. import org.elasticsearch.common.xcontent.XContentFactory;
  6. import org.elasticsearch.common.xcontent.XContentType;
  7. import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
  8. import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
  9. import org.elasticsearch.action.support.master.AcknowledgedResponse;
  10. import org.elasticsearch.client.RestHighLevelClient;
  11. import org.elasticsearch.common.settings.Settings;
  12. import org.elasticsearch.common.xcontent.XContentBuilder;
  13. import org.elasticsearch.common.xcontent.XContentFactory;
  14. import org.elasticsearch.common.xcontent.XContentType;
  15. import org.elasticsearch.index.mapper.MapperParsingException;
  16. import org.elasticsearch.rest.RestStatus;
  17. public class Main {
  18. public static void main(String[] args) throws Exception {
  19. // 创建一个客户端
  20. RestHighLevelClient client = new RestHighLevelClient(
  21. RestClient.builder(new HttpHost("localhost", 9200, "http")));
  22. // 创建一个包含两个字段的索引
  23. CreateIndexRequest request = new CreateIndexRequest("my_index");
  24. XContentBuilder builder = XContentFactory.jsonBuilder();
  25. builder.startObject();
  26. {
  27. builder.startObject("properties");
  28. {
  29. builder.startObject("title");
  30. {
  31. builder.field("type", "text");
  32. }
  33. builder.endObject();
  34. builder.startObject("count");
  35. {
  36. builder.field("type", "integer");
  37. }
  38. builder.endObject();
  39. }
  40. builder.endObject();
  41. }
  42. builder.endObject();
  43. request.mapping(builder);
  44. CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
  45. // 关闭客户端
  46. client.close();
  47. }
  48. }

在上面的示例中,我们创建了一个名为my_index的索引,并在其中定义了两个字段:title和count。title是一个文本字段,count是一个整数字段。创建索引的请求被发送到Elasticsearch服务器,如果成功创建索引,就会返回一个包含索引信息的响应。

倒排索引

倒排索引是一个单词到文档的映射。在倒排索引中,每个单词都被映射到一个或多个文档,这些文档包含了该单词。倒排索引非常适合用于全文搜索和倒排索引非常适合用于全文搜索和关键字查询。当用户输入一个查询时,查询语句中的每个单词都会被映射到倒排索引中的文档列表,然后Elasticsearch会将这些文档返回给用户。

下面是一个简单的示例,展示如何使用Elasticsearch的Java API来创建一个包含两个文档的索引,每个文档包含一个字符串字段。

  1. import org.elasticsearch.client.RestHighLevelClient;
  2. import org.elasticsearch.client.indices.CreateIndexRequest;
  3. import org.elasticsearch.client.indices.CreateIndexResponse;
  4. import org.elasticsearch.common.xcontent.XContentBuilder;
  5. import org.elasticsearch.common.xcontent.XContentFactory;
  6. import org.elasticsearch.common.xcontent.XContentType;
  7. import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
  8. import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
  9. import org.elasticsearch.action.support.master.AcknowledgedResponse;
  10. import org.elasticsearch.client.RestHighLevelClient;
  11. import org.elasticsearch.common.settings.Settings;
  12. import org.elasticsearch.common.xcontent.XContentBuilder;
  13. import org.elasticsearch.common.xcontent.XContentFactory;
  14. import org.elasticsearch.common.xcontent.XContentType;
  15. import org.elasticsearch.index.mapper.MapperParsingException;
  16. import org.elasticsearch.rest.RestStatus;
  17. public class Main {
  18. public static void main(String[] args) throws Exception {
  19. // 创建一个客户端
  20. RestHighLevelClient client = new RestHighLevelClient(
  21. RestClient.builder(new HttpHost("localhost", 9200, "http")));
  22. // 创建一个包含两个文档的索引
  23. IndexRequest request1 = new IndexRequest("my_index");
  24. request1.id("1");
  25. String json1 = "{\"text\": \"The quick brown fox jumps over the lazy dog.\"}";
  26. request1.source(json1, XContentType.JSON);
  27. IndexResponse response1 = client.index(request1, RequestOptions.DEFAULT);
  28. IndexRequest request2 = new IndexRequest("my_index");
  29. request2.id("2");
  30. String json2 = "{\"text\": \"The quick brown fox.\"}";
  31. request2.source(json2, XContentType.JSON);
  32. IndexResponse response2 = client.index(request2, RequestOptions.DEFAULT);
  33. // 关闭客户端
  34. client.close();
  35. }
  36. }

在上面的示例中,我们创建了一个名为my_index的索引,并向其中添加了两个文档。每个文档都包含一个名为text的字符串字段,表示文档内容。使用IndexRequest来创建文档,发送请求到Elasticsearch服务器,如果成功创建文档,就会返回一个包含文档信息的响应。

小结:

正排索引和倒排索引是Elasticsearch中重要的概念,用于提高搜索和查询的性能。在本文中,我们介绍了这两种索引的基本概念,并通过简单的Java代码示例展示了如何使用Elasticsearch的API来创建和管理这些索引。如果您想深入了解Elasticsearch,建议阅读官方文档和参考资料。


该分类下的相关小册推荐: