ManticoreSearch

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

目录

一、索引结构

二、windows安装manticoresearch

2.1 下载manticoresearch:

2.2 运行安装程序

2.3 配置manticoresearch

2.4 启动manticoresearch

2.5 启动验证

三、springcloud集成manticoresearch

3.1 添加maven依赖

四、基本操作

4.1 创建索引

4.2 添加数据 

4.3 检索数据 

4.4 统计数据


一、索引结构

manticoresearch使用倒排索引结构来支持高效的全文搜索

倒排索引是一种将文档中的每个单词映射到包含该单词的文档的数据结构

二、windows安装manticoresearch

2.1 下载manticoresearch:

Manticore Search

https://github.com/manticoresoftware/manticoresearch

2.2 运行安装程序

2.3 配置manticoresearch

编辑manticore.conf,修改监听端口、索引路径等

2.4 启动manticoresearch

执行bin目录下的searchd.exe文件

2.5 启动验证

访问http://localhost:9308/

三、springcloud集成manticoresearch

3.1 添加maven依赖


    io.manticoresearch
    manticoresearch-java
    1.0.0

3.2 创建manticoresearch客户端bean 

@Configuration
public class ManticoreSearchConfig {
    @Bean
    public ManticoreSearchClient manticoreSearchClient() {
        // 创建并返回 ManticoreSearch 客户端实例
        return new ManticoreSearchClient("localhost", 9308);
    }
}

3.3 使用manticoresearch客户端 

@Service
public class SearchService {

    @Autowired
    private ManticoreSearchClient manticoreSearchClient;

    @Autowired
    public SearchService(ManticoreSearchClient manticoreSearchClient) {
        this.manticoreSearchClient = manticoreSearchClient;
    }

    public void searchDocuments(String indexName, String query) {
        SearchRequest searchRequest = new SearchRequest(indexName);
        // 设置搜索请求参数
        // ...

        // 发送搜索请求
        SearchResponse searchResponse = manticoreSearchClient.search(searchRequest);
        // 处理搜索结果
        // ...
    }
}

四、基本操作

4.1 创建索引

// 设置索引配置
IndexSettingsRequest settingsRequest = new IndexSettingsRequest("my_index");
settingsRequest.setSettings(new IndexSettings().setRtMemLimit("256M"));
// 发送设置请求
IndexSettingsResponse settingsResponse = client.indexSettings(settingsRequest);

4.2 添加数据 

// 创建批量请求
BulkRequest bulkRequest = new BulkRequest("my_index");
// 添加数据项
bulkRequest.add(new BulkDataItem().setId("1").setFields(Collections.singletonMap("title", "Document 1")));
bulkRequest.add(new BulkDataItem().setId("2").setFields(Collections.singletonMap("title", "Document 2")));
// 执行批量请求
BulkResponse bulkResponse = client.bulk(bulkRequest);

4.3 检索数据 

// 创建搜索请求
SearchRequest searchRequest = new SearchRequest("my_index");
// 创建查询条件
BoolQueryBuilder boolQuery = new BoolQueryBuilder();
boolQuery.must(new MatchQueryBuilder("title", "Document"));
searchRequest.setQuery(boolQuery);
// 设置排序
SortQueryBuilder sortQuery = new SortQueryBuilder();
sortQuery.addSort(new SortParam("title", SortQuery.SortOrder.ASC));
searchRequest.setSort(sortQuery);
// 发送搜索请求
SearchResponse searchResponse = client.search(searchRequest);

4.4 统计数据

// 创建搜索请求
SearchRequest searchRequest = new SearchRequest("my_index");
// 创建查询条件
QueryBuilder queryBuilder = new TermQueryBuilder("title", "Document");
QueryParam queryParam = new QueryParam(queryBuilder);
searchRequest.setQuery(queryParam);
// 发送搜索请求
SearchResponse searchResponse = client.search(searchRequest);
long totalCount = searchResponse.getTotal();

你可能感兴趣的:(elasticsearch,spring,cloud,java)