Neo4j 進階搜尋 概要
概念上. Neo4j主要的搜尋方式就是Filter和Prune Filter透過由起始點Path到這個點的內容來判別這個Node是否回傳. (如果是. 就傳回該關係) Prune 未來將被停用了. Prune則與上面相反. (如果是. 就不回傳該關係) 不過Neo4j API 裡面的這一段話 :
Deprecated. becuase of the introduction of Evaluator which combines PruneEvaluator and filtering (Predicate of Paths).
即將改以Evaluators取代Filter和Prune. 透過Evaluators我們可以透過傳入Traverser決定是否回傳該關係. 以下為四種Traverser : INCLUDE_AND_CONTINUE , INCLUDE_AND_PRUNE, INCLUDE_AND_CONTINUE, EXCLUDE_AND_CONTINUE 而Neo4j.rb目前仍是使用Filter和Prune. 而Evaluation相信將在不久被釋出.
另外,我們還可以對結果做不重覆的處理. Uniqueness的使用方法可以看我.
Lucene搜尋 由於Neo4j使用Lucene搜尋,所以搜尋的方式也能有許多變化.
Range Query :
(1) index.index(nfriend, "FriendshipId", 5 ) index.index(nfriend, "FriendshipId", 4) index.index(nfriend, "FriendshipId", 54) index.index(nfriend, "FriendshipId", 45) index.index(nfriend, "FriendshipId", 554) index.index(nfriend, "FriendshipId", 435) index.index(nfriend, "FriendshipId", 654) index.index(nfriend, "FriendshipId", 345) Neo4j index.getNodes("FriendshipId", "[5 to 4]") Neo4j.rb Object.find(:FriendshipID).between(5,4)
Result => 5, 54, 554, 654
(2) index.index(Cocktail, "material", "Wiskey") index.index(Dish, "material", "Onion") index.index(Dish, "spice", "Curry") index.index(Cocktail, "material", "Orange") index.index(Dish, "material", "Apple") index.index(Flavor, "material", "PineApple") Neo4j index.getNodes("material",[Wiskey TO Apple]) Neo4j.rb Object.find(:material,"Wiskey","Apple")
Result => Wiskey,Onion,Orange,Apple
Full Text Search
Neo4j
index.getNodes( "name", "John" )
Neo4j.rb
index :name, :type => :fulltext Object.find(:name=>"John", :type => :fulltext)
Sorting
Neo4j
index.getNodes( "name", "wachowski", new Sort( new SortField( "name",SortField.STRING ) ) );
Neo4j.rb
Object.find(:name=>"John").asc(:name).desc(:name)
關於深度搜尋
在Neo4j.rb中. 要限定深度很簡單. 只需要加上 depth即可. 範例如下 :
Member.find( Member.find(:live_in=>"Taipei").depth(:all)
喜歡的就把 :all直接改數字即可. :)
BranchSelector 則能讓我們做深度及廣度的搜尋, 主要分為下列四種 : preorderDepthFirst - 深度優先,先做 postorderDepthFirst - 深度優先,後做 preorderBreadthFirst - 廣度優先,後做 postorderBreadthFirst - 廣度優先,先做
值得注意的是,BreadthFirst會比較佔用記憶體.
根據Neo4j.rb作者所說(2011/02/18),Uniqueness及BranchSelector的功能目前都還在在努力中...相信很快就會有成果.. :) 而Neo4j.rb的這些搜尋範例Von則趕工中.. XD










