如何通过Go中的REST查询找到节点

huangapple go评论92阅读模式
英文:

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(&quot;Start testing FindNodeByQuery&quot;)
	session, err := Dial(settingFile)
	if err != nil {
		t.Error(err)
	}
	log.Println(&quot;Create Index,type lucene&quot;)
	indexName := &quot;testIndex&quot;
	indexType := &quot;fulltext&quot;
	indexProvider := &quot;lucene&quot;
	err = session.CreateNodeIndexWithConf(indexName, indexType, indexProvider)
	if err != nil {
		t.Error(err)
	}
	log.Println(&quot;create two nodes&quot;)
	data := map[string]string{
		&quot;name&quot;:  &quot;test01&quot;,
		&quot;key01&quot;: &quot;value01&quot;,
	}
	node1, err := session.CreateNode(data)
	if err != nil {
		t.Error(err)
	}
	data[&quot;name&quot;] = &quot;test02&quot;
	node2, err := session.CreateNode(data)
	if err != nil {
		t.Error(err)
	}
	indexKey := &quot;some key&quot;
	indexValue := &quot;some value&quot;
	_, 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 := &quot;indexKey:some*&quot;
	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(&quot;Clean data...&quot;)
	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(&quot;data cleaned&quot;)
	log.Println(&quot;FindNodeByQuery test finished!&quot;)

}

答案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 := &quot;some_key:some*&quot;

if you used

indexKey := &quot;some_key&quot;

before. Not as you did: luceneQuery := &quot;indexKey:some*&quot;

huangapple
  • 本文由 发表于 2013年4月11日 07:19:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/15938189.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定