JanusGraph — 查詢謂詞和數據類型(janusgraph Search predicates and data types)
- 2019 年 10 月 4 日
- 筆記
文章列出了JanusGraph在 全局圖搜索和局部遍歷 中支持的所有謂詞。
26.1 比較謂詞
下列比較謂詞,枚舉了用於索引查詢並在上面的示例中使用:
- eq (equal)
- neq (not equal)
- gt (greater than)
- gte (greater than or equal)
- lt (less than)
- lte (less than or equal)
String、numeric、Date和即時的數據類型可以支持所有謂詞。
boolean和uuid僅支持neq和eq
26.2 文本謂詞
Text枚舉指定用於查詢匹配文本或字符串值的搜索操作符。兩種類型謂詞區別:
- 文本搜索謂詞在文本字符串被標記化後與文本字符串中的單個單詞匹配。這些謂詞不區分大小寫。
- textContains:如果(至少)文本字符串中的一個單詞與查詢字符串匹配,則為true
- textContainsPrefix:如果(至少)文本字符串中的一個單詞以查詢字符串開頭,則為true
- textContainsRegex:如果(至少)文本字符串中的一個單詞與給定的正則表達式匹配,則為true
- textContainsFuzzy:如果(至少)文本字符串中的一個單詞與查詢字符串相似(基於Levenshtein編輯距離),則為true
- 字符串搜索謂詞與整個字符串值匹配
- textPrefix:如果字符串值以給定的查詢字符串開頭
- textRegex:如果字符串值與給定的正則表達式完全匹配
- textFuzzy:如果字符串值類似於給定的查詢字符串(基於Levenshtein編輯距離)
有關全文和字符串搜索的更多信息,請參見第24.1節「全文搜索」。
26.3 地理謂詞
下面列舉了地理謂詞:
- geoIntersect 如果兩個幾何對象具有至少一個共同點(相反geoDisjoint),則這是正確的。
- geoWithin 如果一個幾何對象包含另一個幾何對象,則成立
- geoDisjoint 如果兩個幾何對象沒有共同的點(相反geoIntersect),則這是正確的。
- geoContains 如果一個幾何對象被另一個包含,則該方法成立。
有關地理搜索的詳細信息,請參見第24.2節「地理映射」。 ### 23.4 查詢示例
26.4 查詢示例
以下查詢示例演示了教程上的一些謂詞:
// 1)獲取name屬性為「hercules」的節點 g.V().has("name", "hercules") // 2) Find all vertices with an age greater than 50 g.V().has("age", gt(50)) // or find all vertices between 1000 (inclusive) and 5000 (exclusive) years of age and order by increasing age g.V().has("age", inside(1000, 5000)).order().by("age", incr) // which returns the same result set as the following query but in reverse order g.V().has("age", inside(1000, 5000)).order().by("age", decr) // 3)獲取所有給定經緯度50km內的所有place g.E().has("place", geoWithin(Geoshape.circle(37.97, 23.72, 50))) // 4) Find all edges where reason contains the word "loves" // 4) 查找邊reason屬性中包含「loves」詞的所有邊 g.E().has("reason", textContains("loves")) // or all edges which contain two words (need to chunk into individual words) g.E().has("reason", textContains("loves")).has("reason", textContains("breezes")) // or all edges which contain words that start with "lov" g.E().has("reason", textContainsPrefix("lov")) // or all edges which contain words that match the regular expression "br[ez]*s" in their entirety g.E().has("reason", textContainsRegex("br[ez]*s")) // or all edges which contain words similar to "love" g.E().has("reason", textContainsFuzzy("love")) // 5) Find all vertices older than a thousand years and named "saturn" // 5)查詢所有年齡大於1000年的並且姓名為「saturn」的節點 g.V().has("age", gt(1000)).has("name", "saturn")
26.5 支持數據類型
雖然JanusGraph的複合索引(composite indexes)支持 可以存儲在JanusGraph中的 任何數據類型, 但混合索引(mixed indexes )僅限於以下數據類型。
- Byte
- Short
- Integer
- Long
- Float
- Double
- String
- Geoshape
- Date
- Instant
- UUID
將來將支持其他數據類型。
26.6 地理位置數據類型
Geoshape數據類型支持 :點,圓,框,線,多邊形,多點,多線和多邊形。
索引後端目前支持索引:點,圓,框,線,多邊形,多點, 多線,多邊形和幾何集合。
僅通過混合索引支持地理空間索引查找。
要構建Geoshape,請使用以下方法:
//lat, lng Geoshape.point(37.97, 23.72) //lat, lng, radius in km Geoshape.circle(37.97, 23.72, 50) //SW lat, SW lng, NE lat, NE lng Geoshape.box(37.97, 23.72, 38.97, 24.72) //WKT Geoshape.fromWkt("POLYGON ((35.4 48.9, 35.6 48.9, 35.6 49.1, 35.4 49.1, 35.4 48.9))") //MultiPoint Geoshape.geoshape(Geoshape.getShapeFactory().multiPoint().pointXY(60.0, 60.0).pointXY(120.0, 60.0) .build()) //MultiLine Geoshape.geoshape(Geoshape.getShapeFactory().multiLineString() .add(Geoshape.getShapeFactory().lineString().pointXY(59.0, 60.0).pointXY(61.0, 60.0)) .add(Geoshape.getShapeFactory().lineString().pointXY(119.0, 60.0).pointXY(121.0, 60.0)).build()) //MultiPolygon Geoshape.geoshape(Geoshape.getShapeFactory().multiPolygon() .add(Geoshape.getShapeFactory().polygon().pointXY(59.0, 59.0).pointXY(61.0, 59.0) .pointXY(61.0, 61.0).pointXY(59.0, 61.0).pointXY(59.0, 59.0)) .add(Geoshape.getShapeFactory().polygon().pointXY(119.0, 59.0).pointXY(121.0, 59.0) .pointXY(121.0, 61.0).pointXY(119.0, 61.0).pointXY(119.0, 59.0)).build()) //GeometryCollection Geoshape.geoshape(Geoshape.getGeometryCollectionBuilder() .add(Geoshape.getShapeFactory().pointXY(60.0, 60.0)) .add(Geoshape.getShapeFactory().lineString().pointXY(119.0, 60.0).pointXY(121.0, 60.0).build()) .add(Geoshape.getShapeFactory().polygon().pointXY(119.0, 59.0).pointXY(121.0, 59.0) .pointXY(121.0, 61.0).pointXY(119.0, 61.0).pointXY(119.0, 59.0)).build())
此外,通過GraphSON導入圖形時,幾何體可能由GeoJSON表示:
//string "37.97, 23.72" //list [37.97, 23.72] //GeoJSON feature { "type": "Feature", "geometry": { "type": "Point", "coordinates": [125.6, 10.1] }, "properties": { "name": "Dinagat Islands" } } //GeoJSON geometry { "type": "Point", "coordinates": [125.6, 10.1] }
GeoJSON可以指定為Point,Circle,LineString或Polygon。多邊形必須關閉。請注意, 與JanusGraph API不同,GeoJSON將坐標指定為lng lat。
26.7 集合
如果您使用的是Elasticsearch,則可以對SET和LIST類型的屬性進行索引。例如:
mgmt = graph.openManagement() // 設置names屬性為SET類型 nameProperty = mgmt.makePropertyKey("names").dataType(String.class).cardinality(Cardinality.SET).make() mgmt.buildIndex("search", Vertex.class).addKey(nameProperty, Mapping.STRING.asParameter()).buildMixedIndex("search") mgmt.commit() // 插入一個節點 person = graph.addVertex() person.property("names", "Robert") person.property("names", "Bob") graph.tx().commit() // 現在查詢它 g.V().has("names", "Bob").count().next() //1 g.V().has("names", "Robert").count().next() //1
如果轉載此博文,請附上本文鏈接https://blog.csdn.net/csdn___lyy,謝謝合作~
