Nebula Graph 3.6+全Tag索引配置咨询:Docker/Spring Boot启动实现
Nebula Graph v3.6 全Tag属性搜索索引实现方案
一、定义全Tag属性搜索的索引
根据你的搜索需求,分两种场景选择索引类型:
1. 精准匹配/范围查询:组合索引
如果需要对Tag的所有属性做精准匹配、数值范围过滤这类查询,直接创建包含该Tag全部属性的组合索引即可。语法如下:
CREATE INDEX IF NOT EXISTS idx_tag_all_properties ON <tag_name>(<prop1>, <prop2>, ..., <propN>);
示例(Tag名为user,属性包含id/name/age):
CREATE INDEX IF NOT EXISTS idx_user_all ON user(id, name, age);
注意:Nebula组合索引支持前缀匹配,只要查询条件包含索引的前缀属性,就能命中索引。
2. 模糊/全文搜索:全文索引
如果需要对Tag的多个属性做关键词模糊搜索,需使用Nebula的全文索引(前提是已部署并配置Elasticsearch作为后端)。创建语法:
CREATE FULLTEXT INDEX IF NOT EXISTS ft_idx_tag_all ON <tag_name>(prop1, prop2, ..., propN) WITH ( elasticsearch = { index = "nebula_ft_tag", hosts = "http://es-host:9200" } );
若已有存量数据,需手动触发索引重建:
REBUILD FULLTEXT INDEX ft_idx_tag_all;
二、Spring Boot启动时自动检查并创建索引
直接通过Nebula Java客户端在Spring Boot启动阶段执行索引检查与创建逻辑:
1. 添加依赖
在pom.xml中引入Nebula客户端依赖:
<dependency> <groupId>com.vesoft</groupId> <artifactId>nebula-java-client</artifactId> <version>3.6.0</version> </dependency>
2. 启动初始化逻辑
编写ApplicationRunner实现类,在应用启动时自动执行索引检查:
@Component public class NebulaIndexInitializer implements ApplicationRunner { @Value("${nebula.host}") private String nebulaHost; @Value("${nebula.port}") private int nebulaPort; @Value("${nebula.username}") private String username; @Value("${nebula.password}") private String password; @Value("${nebula.space}") private String spaceName; // 可配置在application.yml中,定义需创建索引的Tag及其属性 private final Map<String, List<String>> tagProps = Map.of( "user", List.of("id", "name", "age"), "product", List.of("pid", "pname", "price") ); @Override public void run(ApplicationArguments args) throws Exception { NebulaPoolConfig poolConfig = new NebulaPoolConfig(); poolConfig.setMaxConnSize(10); NebulaPool pool = new NebulaPool(); pool.init(List.of(new HostAddress(nebulaHost, nebulaPort)), poolConfig); try (Session session = pool.getSession(username, password, false)) { session.execute("USE " + spaceName); for (Map.Entry<String, List<String>> entry : tagProps.entrySet()) { String tagName = entry.getKey(); List<String> props = entry.getValue(); String indexName = "idx_" + tagName + "_all"; // 检查索引是否存在 ResultSet checkResult = session.execute(String.format("SHOW INDEXES LIKE '%s'", indexName)); if (checkResult.getRows().isEmpty()) { String propsStr = String.join(", ", props); String createSql = String.format("CREATE INDEX IF NOT EXISTS %s ON %s(%s)", indexName, tagName, propsStr); session.execute(createSql); System.out.println("索引 " + indexName + " 创建成功"); } else { System.out.println("索引 " + indexName + " 已存在,无需创建"); } } } finally { pool.close(); } } }
若使用全文索引,只需将上述创建SQL替换为对应全文索引语法,并补充Elasticsearch配置参数即可。
三、通过Docker实现索引自动创建
无需修改Spring Boot代码,可通过Docker脚本实现索引初始化:
1. 编写初始化脚本
创建init-indexes.sh脚本:
#!/bin/bash # 延迟等待Nebula服务启动完成 sleep 30 # 连接Nebula执行索引创建 nebula-console -addr ${NEBULA_HOST} -port ${NEBULA_PORT} -u ${NEBULA_USER} -p ${NEBULA_PASSWORD} -e " USE ${NEBULA_SPACE}; CREATE INDEX IF NOT EXISTS idx_user_all ON user(id, name, age); CREATE INDEX IF NOT EXISTS idx_product_all ON product(pid, pname, price); "
2. Docker Compose配置
添加一个初始化容器,在Nebula启动后执行脚本:
version: '3.8' services: nebula-graphd: image: vesoft/nebula-graphd:v3.6.0 # 其他Nebula配置(端口、挂载等) nebula-index-init: image: vesoft/nebula-console:v3.6.0 depends_on: - nebula-graphd environment: - NEBULA_HOST=nebula-graphd - NEBULA_PORT=9669 - NEBULA_USER=root - NEBULA_PASSWORD=nebula - NEBULA_SPACE=test_space volumes: - ./init-indexes.sh:/init-indexes.sh command: ["sh", "/init-indexes.sh"]
内容的提问来源于stack exchange,提问作者saber tabatabaee yazdi
相关产品推荐
相关产品推荐

