

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

# Amazon Neptune 中的全文檢索搜尋查詢執行
<a name="full-text-search-query-execution"></a>

在包含全文搜尋的查詢中，Neptune 會先嘗試放置全文檢索搜尋呼叫，再放置查詢的其他部分。這可減少 OpenSearch 的呼叫次數，並在大多數情況下，大幅提升效能。不過，這絕不是硬性規則。例如，有些情況，`PatternNode` 或 `UnionNode` 可能先於全文檢索搜尋呼叫。

請考慮對資料庫進行下列 Gremlin 查詢，此資料庫包含 100,000 個 `Person` 執行個體：

```
g.withSideEffect('Neptune#fts.endpoint', 'your-es-endpoint-URL')
 .hasLabel('Person')
 .has('name', 'Neptune#fts marcello~');
```

如果此查詢按照步驟出現的順序執行，則 100,000 個解決方案將流入 OpenSearch，導致數百個 OpenSearch 呼叫。事實上，Neptune 會先呼叫 OpenSearch，然後結合產生的結果與 Neptune 結果。在大多數情況下，這樣做比依原始順序執行查詢快得多。

您可以使用 [noReordering 查詢提示](gremlin-query-hints-noReordering.md)來防止重新排序查詢步驟執行：

```
g.withSideEffect('Neptune#fts.endpoint', 'your-es-endpoint-URL')
 .withSideEffect('Neptune#noReordering', true)
 .hasLabel('Person')
 .has('name', 'Neptune#fts marcello~');
```

在這第二種情況下，會先執行 `.hasLabel` 步驟，再執行 `.has('name', 'Neptune#fts marcello~')` 步驗。

對於另一個範例，考慮對相同類型的資料進行 SPARQL 查詢：

```
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX neptune-fts: <http://aws.amazon.com/neptune/vocab/v01/services/fts#>
SELECT ?person WHERE {
  ?person rdf:type foaf:Person .
  SERVICE neptune-fts:search {
    neptune-fts:config neptune-fts:endpoint 'http://your-es-endpoint.com' .
    neptune-fts:config neptune-fts:field foaf:name .
    neptune-fts:config neptune-fts:query 'mike' .
    neptune-fts:config neptune-fts:return ?person .
  }
}
```

在這裡，Neptune 會再次先執行查詢的 `SERVICE` 部分，然後結合產生的結果與 `Person` 資料。您可以使用 [joinOrder 查詢提示](sparql-query-hints-joinOrder.md)來隱藏此行為：

```
PREFIX foaf: <http://xmlns.com/foaf/0.1/>
PREFIX neptune-fts: <http://aws.amazon.com/neptune/vocab/v01/services/fts#>
PREFIX hint: <http://aws.amazon.com/neptune/vocab/v01/QueryHints#>
SELECT ?person WHERE {
  hint:Query hint:joinOrder "Ordered" .
  ?person rdf:type foaf:Person .
  SERVICE neptune-fts:search {
    neptune-fts:config neptune-fts:endpoint 'http://your-es-endpoint.com' .
    neptune-fts:config neptune-fts:field foaf:name .
    neptune-fts:config neptune-fts:query 'mike' .
    neptune-fts:config neptune-fts:return ?person .
  }
}
```

再次，在第二個查詢中，這些部分會按照它們出現在查詢的順序執行。

**注意**  
 透過索引查詢 opensearch 別名，而不是直接查詢 opensearch 索引，可能會產生不正確的結果。您應該直接查詢 opensearch 索引，而不是別名。