在 Gremlin 查詢中使用 Neptune 全文檢索搜尋 - Amazon Neptune

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

在 Gremlin 查詢中使用 Neptune 全文檢索搜尋

NeptuneSearchStep 可以對未轉換為 Neptune 步驟的 Gremlin 周遊部分,進行全文搜尋查詢。例如,考慮使用以下查詢。

g.withSideEffect("Neptune#fts.endpoint", "your-es-endpoint-URL") .V() .tail(100) .has("name", "Neptune#fts mark*") <== # Limit the search on name

此查詢會在 Neptune 中轉換為以下最佳化的周遊。

Neptune steps: [ NeptuneGraphQueryStep(Vertex) { JoinGroupNode { PatternNode[(?1, <~label>, ?2, <~>) . project distinct ?1 .], {estimatedCardinality=INFINITY} }, annotations={path=[Vertex(?1):GraphStep], maxVarId=4} }, NeptuneTraverserConverterStep ] + not converted into Neptune steps: [NeptuneTailGlobalStep(100), NeptuneTinkerpopTraverserConverterStep, NeptuneSearchStep { JoinGroupNode { SearchNode[(idVar=?3, query=mark*, field=name) . project ask .], {endpoint=your-OpenSearch-endpoint-URL} } JoinGroupNode { SearchNode[(idVar=?3, query=mark*, field=name) . project ask .], {endpoint=your-OpenSearch-endpoint-URL} } }]

下列範例是針對航線資料的 Gremlin 查詢:

Gremlin 基本不區分大小寫的 match 查詢

g.withSideEffect("Neptune#fts.endpoint", "your-OpenSearch-endpoint-URL") .withSideEffect('Neptune#fts.queryType', 'match') .V().has("city","Neptune#fts dallas") ==>v[186] ==>v[8]

Gremlin match 查詢

g.withSideEffect("Neptune#fts.endpoint", "your-OpenSearch-endpoint-URL") .withSideEffect('Neptune#fts.queryType', 'match') .V().has("city","Neptune#fts southampton") .local(values('code','city').fold()) .limit(5) ==>[SOU, Southampton]

Gremlin fuzzy 查詢

g.withSideEffect("Neptune#fts.endpoint", "your-OpenSearch-endpoint-URL") .V().has("city","Neptune#fts allas~").values('city').limit(5) ==>Dallas ==>Dallas ==>Walla Walla ==>Velas ==>Altai

Gremlin query_string 模糊查詢

g.withSideEffect("Neptune#fts.endpoint", "your-OpenSearch-endpoint-URL") .withSideEffect('Neptune#fts.queryType', 'query_string') .V().has("city","Neptune#fts allas~").values('city').limit(5) ==>Dallas ==>Dallas

Gremlin query_string 規則表達式查詢

g.withSideEffect("Neptune#fts.endpoint", "your-OpenSearch-endpoint-URL") .withSideEffect('Neptune#fts.queryType', 'query_string') .V().has("city","Neptune#fts /[dp]allas/").values('city').limit(5) ==>Dallas ==>Dallas

Gremlin 混合查詢

此查詢會在同一個查詢中使用 Neptune 內部索引和 OpenSearch 索引。

g.withSideEffect("Neptune#fts.endpoint", "your-OpenSearch-endpoint-URL") .V().has("region","GB-ENG") .has('city','Neptune#fts L*') .values('city') .dedup() .limit(10) ==>London ==>Leeds ==>Liverpool ==>Land's End

簡單的 Gremlin 全文檢索搜尋範例

g.withSideEffect("Neptune#fts.endpoint", "your-OpenSearch-endpoint-URL") .V().has('desc','Neptune#fts regional municipal') .local(values('code','desc').fold()) .limit(100) ==>[HYA, Barnstable Municipal Boardman Polando Field] ==>[SPS, Sheppard Air Force Base-Wichita Falls Municipal Airport] ==>[ABR, Aberdeen Regional Airport] ==>[SLK, Adirondack Regional Airport] ==>[BFD, Bradford Regional Airport] ==>[EAR, Kearney Regional Airport] ==>[ROT, Rotorua Regional Airport] ==>[YHD, Dryden Regional Airport] ==>[TEX, Telluride Regional Airport] ==>[WOL, Illawarra Regional Airport] ==>[TUP, Tupelo Regional Airport] ==>[COU, Columbia Regional Airport] ==>[MHK, Manhattan Regional Airport] ==>[BJI, Bemidji Regional Airport] ==>[HAS, Hail Regional Airport] ==>[ALO, Waterloo Regional Airport] ==>[SHV, Shreveport Regional Airport] ==>[ABI, Abilene Regional Airport] ==>[GIZ, Jizan Regional Airport] ==>[USA, Concord Regional Airport] ==>[JMS, Jamestown Regional Airport] ==>[COS, City of Colorado Springs Municipal Airport] ==>[PKB, Mid Ohio Valley Regional Airport]

使用 query_string 搭配「+」和「-」運算子的 Gremlin 查詢

雖然 query_string 查詢類型比預設的 simple_query_string 類型嚴格,但其確實能讓您執行較精確的查詢。下面的第一個查詢使用 query_string,第二個查詢使用預設的 simple_query_string

g.withSideEffect("Neptune#fts.endpoint", "your-OpenSearch-endpoint-URL") .withSideEffect('Neptune#fts.queryType', 'query_string') . V().has('desc','Neptune#fts +London -(Stansted|Gatwick)') .local(values('code','desc').fold()) .limit(10) ==>[LHR, London Heathrow] ==>[YXU, London Airport] ==>[LTN, London Luton Airport] ==>[SEN, London Southend Airport] ==>[LCY, London City Airport]

請注意在以下範例中,simple_query_string 如何不動聲色地忽略 '+' 和 '-' 運算子:

g.withSideEffect("Neptune#fts.endpoint", "your-OpenSearch-endpoint-URL") .V().has('desc','Neptune#fts +London -(Stansted|Gatwick)') .local(values('code','desc').fold()) .limit(10) ==>[LHR, London Heathrow] ==>[YXU, London Airport] ==>[LGW, London Gatwick] ==>[STN, London Stansted Airport] ==>[LTN, London Luton Airport] ==>[SEN, London Southend Airport] ==>[LCY, London City Airport] ==>[SKG, Thessaloniki Macedonia International Airport] ==>[ADB, Adnan Menderes International Airport] ==>[BTV, Burlington International Airport]
g.withSideEffect("Neptune#fts.endpoint", "your-OpenSearch-endpoint-URL") .withSideEffect('Neptune#fts.queryType', 'query_string') .V().has('desc','Neptune#fts +(regional|municipal) -(international|bradford)') .local(values('code','desc').fold()) .limit(10) ==>[CZH, Corozal Municipal Airport] ==>[MMU, Morristown Municipal Airport] ==>[YBR, Brandon Municipal Airport] ==>[RDD, Redding Municipal Airport] ==>[VIS, Visalia Municipal Airport] ==>[AIA, Alliance Municipal Airport] ==>[CDR, Chadron Municipal Airport] ==>[CVN, Clovis Municipal Airport] ==>[SDY, Sidney Richland Municipal Airport] ==>[SGU, St George Municipal Airport]

具有 ANDOR 運算子的 Gremlin query_string 查詢

g.withSideEffect("Neptune#fts.endpoint", "your-OpenSearch-endpoint-URL") .withSideEffect('Neptune#fts.queryType', 'query_string') .V().has('desc','Neptune#fts (St AND George) OR (St AND Augustin)') .local(values('code','desc').fold()) .limit(10) ==>[YIF, St Augustin Airport] ==>[STG, St George Airport] ==>[SGO, St George Airport] ==>[SGU, St George Municipal Airport]

Gremlin term 查詢

g.withSideEffect("Neptune#fts.endpoint", "your-OpenSearch-endpoint-URL") .withSideEffect('Neptune#fts.queryType', 'term') .V().has("SKU","Neptune#fts ABC123DEF9") .local(values('code','city').fold()) .limit(5) ==>[AUS, Austin]

Gremlin prefix 查詢

g.withSideEffect("Neptune#fts.endpoint", "your-OpenSearch-endpoint-URL") .withSideEffect('Neptune#fts.queryType', 'prefix') .V().has("icao","Neptune#fts ka") .local(values('code','icao','city').fold()) .limit(5) ==>[AZO, KAZO, Kalamazoo] ==>[APN, KAPN, Alpena] ==>[ACK, KACK, Nantucket] ==>[ALO, KALO, Waterloo] ==>[ABI, KABI, Abilene]

在 Neptune Gremlin 中使用 Lucene 語法

在 Neptune Gremlin 中,您也可以使用 Lucene 查詢語法編寫非常強大的查詢條件。請注意,僅對 OpenSearch 中的 query_string 查詢支援 Lucene 語法。

假設下列資料:

g.addV("person") .property(T.id, "p1") .property("name", "simone") .property("surname", "rondelli") g.addV("person") .property(T.id, "p2") .property("name", "simone") .property("surname", "sengupta") g.addV("developer") .property(T.id, "p3") .property("name", "simone") .property("surname", "rondelli")

使用 (queryTypequery_string 時所呼叫的) Lucene 語法,即可按名字和姓氏搜尋此資料,如下所示:

g.withSideEffect("Neptune#fts.endpoint", "es_endpoint") .withSideEffect("Neptune#fts.queryType", "query_string") .V() .has("*", "Neptune#fts predicates.name.value:simone AND predicates.surname.value:rondelli") ==> v[p1], v[p3]

請注意,在上述 has() 步驟中,此欄位由 "*" 取代)。實際上,置於該處的任何值都由您在查詢中存取的欄位所覆寫。您可以使用 predicates.name.value, 存取名字欄位,因為這是資料模型的結構方式。

您可以按名字、姓氏和標籤進行搜尋,如下所示:

g.withSideEffect("Neptune#fts.endpoint", getEsEndpoint()) .withSideEffect("Neptune#fts.queryType", "query_string") .V() .has("*", "Neptune#fts predicates.name.value:simone AND predicates.surname.value:rondelli AND entity_type:person") ==> v[p1]

標籤以 entity_type 加以存取,因為這同樣是資料模型的結構方式。

您也可以加入巢狀條件:

g.withSideEffect("Neptune#fts.endpoint", getEsEndpoint()) .withSideEffect("Neptune#fts.queryType", "query_string") .V() .has("*", "Neptune#fts (predicates.name.value:simone AND predicates.surname.value:rondelli AND entity_type:person) OR predicates.surname.value:sengupta") ==> v[p1], v[p2]

插入現代 TinkerPop 圖形

g.addV('person').property(T.id, '1').property('name', 'marko').property('age', 29) .addV('personr').property(T.id, '2').property('name', 'vadas').property('age', 27) .addV('software').property(T.id, '3').property('name', 'lop').property('lang', 'java') .addV('person').property(T.id, '4').property('name', 'josh').property('age', 32) .addV('software').property(T.id, '5').property('name', 'ripple').property('lang', 'java') .addV('person').property(T.id, '6').property('name', 'peter').property('age', 35) g.V('1').as('a').V('2').as('b').addE('knows').from('a').to('b').property('weight', 0.5f).property(T.id, '7') .V('1').as('a').V('3').as('b').addE('created').from('a').to('b').property('weight', 0.4f).property(T.id, '9') .V('4').as('a').V('3').as('b').addE('created').from('a').to('b').property('weight', 0.4f).property(T.id, '11') .V('4').as('a').V('5').as('b').addE('created').from('a').to('b').property('weight', 1.0f).property(T.id, '10') .V('6').as('a').V('3').as('b').addE('created').from('a').to('b').property('weight', 0.2f).property(T.id, '12') .V('1').as('a').V('4').as('b').addE('knows').from('a').to('b').property('weight', 1.0f).property(T.id, '8')

按字串欄位值排序範例

g.withSideEffect("Neptune#fts.endpoint", "your-OpenSearch-endpoint-URL") .withSideEffect('Neptune#fts.queryType', 'query_string') .withSideEffect('Neptune#fts.sortOrder', 'asc') .withSideEffect('Neptune#fts.sortBy', 'name') .V().has('name', 'Neptune#fts marko OR vadas OR ripple')

按非字串欄位值排序範例

g.withSideEffect("Neptune#fts.endpoint", "your-OpenSearch-endpoint-URL") .withSideEffect('Neptune#fts.queryType', 'query_string') .withSideEffect('Neptune#fts.sortOrder', 'asc') .withSideEffect('Neptune#fts.sortBy', 'age.value') .V().has('name', 'Neptune#fts marko OR vadas OR ripple')

按 ID 欄位值排序範例

g.withSideEffect("Neptune#fts.endpoint", "your-OpenSearch-endpoint-URL") .withSideEffect('Neptune#fts.queryType', 'query_string') .withSideEffect('Neptune#fts.sortOrder', 'asc') .withSideEffect('Neptune#fts.sortBy', 'Neptune#fts.entity_id') .V().has('name', 'Neptune#fts marko OR vadas OR ripple')

按標籤欄位值排序範例

g.withSideEffect("Neptune#fts.endpoint", "your-OpenSearch-endpoint-URL") .withSideEffect('Neptune#fts.queryType', 'query_string') .withSideEffect('Neptune#fts.sortOrder', 'asc') .withSideEffect('Neptune#fts.sortBy', 'Neptune#fts.entity_type') .V().has('name', 'Neptune#fts marko OR vadas OR ripple')

document_type 欄位值排序範例

g.withSideEffect("Neptune#fts.endpoint", "your-OpenSearch-endpoint-URL") .withSideEffect('Neptune#fts.queryType', 'query_string') .withSideEffect('Neptune#fts.sortOrder', 'asc') .withSideEffect('Neptune#fts.sortBy', 'Neptune#fts.document_type') .V().has('name', 'Neptune#fts marko OR vadas OR ripple')