2013-07-31 110 views
0

嗨我試圖搜索使用運算符IN,但總是返回一個空列表,什麼是錯的?波紋管我的單元測試。我想要的是通過描述或相似詞來搜索產品。 (未來我假裝實現模糊)。neo4j和java收集搜索

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); 
} 

回答

1

我希望我有你的用例的權利。但是我認爲你的Cypher查詢有點混亂。 您的疑問:

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

讓對n.similarities IN ['tv']看看。屬性similarities是一個數組。 ['tv']是一個字符串數組。因此,表達式`n.similarities IN ['tv']總是計算爲false,因爲['tv']不包含任何數組,更不用說similarities數組。

最後,我只是想你的意思是別的東西,即。

'tv' IN n.similarities 

這是正確的嗎?您想要檢查similarities陣列中是否包含字符串tv。當我改變了查詢方式,即生成的查詢將

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

然後,改編測試類爲我工作。

+0

這就是它的人...我剛剛開始與neo4j昨天...謝謝 – giullianomorroni