英文:
How to find node by query via REST in Go
问题
我测试了根据neo4j手册(http://docs.neo4j.org/chunked/snapshot/rest-api-indexes.html<22.10.10. Find node by query>)中的查询来查找节点,但是遇到了一些问题。我创建了两个节点和一个索引。然后我将两个节点添加到索引中。当我使用lucene查询来查询节点时,我找不到任何结果。
这是我的测试代码:
func TestFindNodeByQuery(t *testing.T) {
log.Println("开始测试FindNodeByQuery")
session, err := Dial(settingFile)
if err != nil {
t.Error(err)
}
log.Println("创建索引,类型为lucene")
indexName := "testIndex"
indexType := "fulltext"
indexProvider := "lucene"
err = session.CreateNodeIndexWithConf(indexName, indexType, indexProvider)
if err != nil {
t.Error(err)
}
log.Println("创建两个节点")
data := map[string]string{
"name": "test01",
"key01": "value01",
}
node1, err := session.CreateNode(data)
if err != nil {
t.Error(err)
}
data["name"] = "test02"
node2, err := session.CreateNode(data)
if err != nil {
t.Error(err)
}
indexKey := "some key"
indexValue := "some value"
_, err = session.AddNodeToIndex(indexKey, indexValue, indexName, node1.ID)
if err != nil {
t.Error(err)
}
_, err = session.AddNodeToIndex(indexKey, indexValue, indexName, node2.ID)
if err != nil {
t.Error(err)
}
luceneQuery := "indexKey:some*"
results, err := session.FindNodeByQuery(indexName, luceneQuery)
// results, err := session.FindNodeByMatch(indexName, indexKey, indexValue)
log.Println(len(results))
for _, result := range results {
log.Println(result)
}
log.Println("清理数据...")
err = session.DeleteNode(node1.ID)
if err != nil {
t.Error(err)
}
err = session.DeleteNode(node2.ID)
if err != nil {
t.Error(err)
}
err = session.DeleteNodeIndex(indexName)
if err != nil {
t.Error(err)
}
log.Println("数据清理完成")
log.Println("FindNodeByQuery测试结束!")
}
英文:
I test find node by query refer to neo4j manual(http://docs.neo4j.org/chunked/snapshot/rest-api-indexes.html<22.10.10. Find node by query>, but I have some troubles, I created two nodes, one index. Then I add two nodes into index. I can't find anything when I query node by lucene query.
This is my test code:
func TestFindNodeByQuery(t *testing.T) {
log.Println("Start testing FindNodeByQuery")
session, err := Dial(settingFile)
if err != nil {
t.Error(err)
}
log.Println("Create Index,type lucene")
indexName := "testIndex"
indexType := "fulltext"
indexProvider := "lucene"
err = session.CreateNodeIndexWithConf(indexName, indexType, indexProvider)
if err != nil {
t.Error(err)
}
log.Println("create two nodes")
data := map[string]string{
"name": "test01",
"key01": "value01",
}
node1, err := session.CreateNode(data)
if err != nil {
t.Error(err)
}
data["name"] = "test02"
node2, err := session.CreateNode(data)
if err != nil {
t.Error(err)
}
indexKey := "some key"
indexValue := "some value"
_, err = session.AddNodeToIndex(indexKey, indexValue, indexName, node1.ID)
if err != nil {
t.Error(err)
}
_, err = session.AddNodeToIndex(indexKey, indexValue, indexName, node2.ID)
if err != nil {
t.Error(err)
}
luceneQuery := "indexKey:some*"
results, err := session.FindNodeByQuery(indexName, luceneQuery)
// results, err := session.FindNodeByMatch(indexName, indexKey, indexValue)
log.Println(len(results))
for _, result := range results {
log.Println(result)
}
log.Println("Clean data...")
err = session.DeleteNode(node1.ID)
if err != nil {
t.Error(err)
}
err = session.DeleteNode(node2.ID)
if err != nil {
t.Error(err)
}
err = session.DeleteNodeIndex(indexName)
if err != nil {
t.Error(err)
}
log.Println("data cleaned")
log.Println("FindNodeByQuery test finished!")
}
答案1
得分: 0
尝试使用没有空格的索引键。这样可以让你的生活更轻松。
然后你实际上必须在Lucene查询中使用正确的索引键来存储数据:
luceneQuery := "some_key:some*"
如果之前你使用了
indexKey := "some_key"
而不是你所做的:luceneQuery := "indexKey:some*"
英文:
Try to use an index key without spaces. That makes your live easier.
Then you actually have to use the correct index key that you stored data under in your lucene query:
luceneQuery := "some_key:some*"
if you used
indexKey := "some_key"
before. Not as you did: luceneQuery := "indexKey:some*"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论