gpt4 book ai didi

java - neo4j 和 java 集合搜索

转载 作者:行者123 更新时间:2023-12-01 14:17:34 24 4
gpt4 key购买 nike

您好,我正在尝试使用运算符 IN 进行搜索,但总是返回空列表,这是怎么回事?下面是我的单元测试。我想要的是通过描述或与描述相似的词来搜索产品。 (以后我假装实现模糊)。

private GraphDatabaseService graph;
private Index<Node> indexProduct;
private ExecutionEngine engine;

@Before
public void prepareTestDatabase() {
TestGraphDatabaseFactory testGraphDatabaseFactory = new TestGraphDatabaseFactory();
this.graph = testGraphDatabaseFactory.newEmbeddedDatabase("/tmp/neo4j/tests");
this.indexProduct = this.graph.index().forNodes("node_product");
this.engine = new ExecutionEngine(this.graph, StringLogger.SYSTEM);
this.loadDataForTest();
}

private void loadDataForTest() {
Transaction tx = this.graph.beginTx();

List<String> similarities = new ArrayList<>();
similarities.add("televisor");
similarities.add("tv");
String description = "televisão";
Product product = new Product(description, similarities);

Node node = this.graph.createNode(DynamicLabel.label(product.description));
node.setProperty("description", product.description);
node.setProperty("similarities", product.similarities);

this.indexProduct.add(node, "PRODUCT", product.description);

tx.success();
tx.finish();
}

@Test
public void searchByDescription(){
String description = "televisão";

List<String> list = new ArrayList<>();
ExecutionResult result = this.engine.execute("start n=node:node_product('PRODUCT:*') WHERE HAS(n.similarities) AND n.similarities IN ['tv'] return n.description");
for (Map<String, Object> row : result) {
for (Entry<String, Object> column : row.entrySet()) {
Object value = column.getValue();
list.add(String.valueOf(value));
}
}

Assert.assertTrue(list.contains(description));
}

这是产品类别

class Product {
String description;
String[] similarities;

Product(String description, List<String> similaritiesList) {
super();
this.description = description;
this.similarities = new String[similaritiesList.size()];
for (int i=0; i<similaritiesList.size(); i++)
this.similarities[i] = similaritiesList.get(i);
}

最佳答案

我希望我的用例是正确的。但我认为您的 Cypher 查询有点困惑。您的查询:

"start n=node:node_product('PRODUCT:*') WHERE HAS(n.similarities) AND n.similarities IN ['tv'] return n.description"

让我们看看 ['tv'] 中的n.similarities。属性similarities是一个数组。 ['tv'] 是一个字符串数组。因此,表达式 `n.similarities IN ['tv'] 的计算结果始终为 false,因为 ['tv'] 不包含任何数组,更不用说similarities数组了。

最后我只是认为你的意思是别的。

 'tv' IN n.similarities

这可能是正确的吗?您想要检查字符串 tv 是否包含在 similarities 数组中。当我以这种方式更改查询时,即生成的查询将是

start n=node:node_product('PRODUCT:*') WHERE HAS(n.similarities) AND 'tv' IN n.similarities return n.description

然后,改编后的测试类对我有用。

关于java - neo4j 和 java 集合搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17981501/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com