In the previous post, we used a hybrid vector index to search park descriptions using semantic search and keyword search separately. The semantic search returned descriptions related to the meaning of a question, while the keyword search returned descriptions matching an Oracle Text expression.
For the following examples, we are going to use the same PARKS_INFO table and PARK_DESCRIPTION_HVI index. This time, we will combine the two searches and examine how their scores affect the order of the returned results.
Consider someone looking for a park where they can learn about prehistoric mammals. They might ask the following question:
Where can I learn about prehistoric mammals and ancient animal remains?
The question provides the text for semantic search. Along with this question, we can supply a keyword expression to search for descriptions containing fossil, fossils, or mammoths.
fossil OR fossils OR mammothsThe following statement supplies both the semantic question and the keyword expression to DBMS_HYBRID_VECTOR.SEARCH. We will use DOCUMENT mode to return park-level results and MAX to represent each document using its strongest retrieved chunk score.
demo@ADB26ai> select json_serialize(
2 dbms_hybrid_vector.search(
3 json('{
4 "hybrid_index_name": "PARK_DESCRIPTION_HVI",
5 "search_fusion": "UNION",
6 "search_scorer": "RSF",
7 "vector": {
8 "search_text": "Where can I learn about prehistoric mammals and ancient animal remains?",
9 "search_mode": "DOCUMENT",
10 "aggregator": "MAX",
11 "score_weight": 1
12 },
13 "text": {
14 "contains": "fossil OR fossils OR mammoths",
15 "score_weight": 1
16 },
17 "return": {
18 "topN": 5,
19 "values": [
20 "rowid",
21 "score",
22 "vector_score",
23 "text_score"
24 ]
25 }
26 }
27 ')
28 )
29 returning clob pretty
30 ) as search_results;
SEARCH_RESULTS
--------------------------------------------------------------------------------
[
{
"rowid": "AAQVAeABUAAPJSyABC",
"score": 41.18,
"vector_score": 60.35,
"text_score": 22
},
{
"rowid": "AAQVAeABUAAPJS5AAy",
"score": 40.02,
"vector_score": 71.04,
"text_score": 9
},
{
"rowid": "AAQVAeABUAAPHl8AAF",
"score": 39.19,
"vector_score": 70.37,
"text_score": 8
},
{
"rowid": "AAQVAeABUAAPJS5AAI",
"score": 38.02,
"vector_score": 67.04,
"text_score": 9
},
{
"rowid": "AAQVAeABUAAPJSpAAe",
"score": 37.68,
"vector_score": 67.35,
"text_score": 8
}
]Notice that the request contains both a vector section and a text section. The vector section supplies the question for semantic search, and text.contains supplies the Oracle Text expression.
The response contains three scores. VECTOR_SCORE represents semantic relevance, TEXT_SCORE represents keyword relevance, and SCORE is the final combined score used to rank the results.
The SEARCH_FUSION parameter is set to UNION. This allows candidates retrieved by either search to participate in the combined result set. SEARCH_SCORER is set to RSF, or Relative Score Fusion, which combines the two scores using the specified weights. The SEARCH documentation describes these parameters in more detail.
To understand the final ranking, let us look at the first two rows from the JSON response.
ROWID VECTOR_SCORE TEXT_SCORE FINAL_SCORE
-------------------- ------------ ---------- -----------
AAQVAeABUAAPJSyABC 60.35 22 41.18
AAQVAeABUAAPJS5AAy 71.04 9 40.02The table above presents the captured JSON values in a compact format.
As you can see, the first result has a vector score of 60.35, while the second has a higher vector score of 71.04. However, the first result appears above the second in the final ranking.
The difference comes from the keyword scores. The first description has a text score of 22, compared with 9 for the second description. The first result discusses Columbian mammoths and an ancient nursery herd in Texas. The second is the Hagerman description, which discusses Pliocene habitats and extinct animals.
Both score weights are explicitly set to 1 in this request. The displayed final scores therefore follow an equal-weight combination of the semantic and keyword scores.
Final score =
(vector_score × vector_weight + text_score × text_weight)
-------------------------------------------------------
vector_weight + text_weightFor the first result, the calculation is:
(60.35 × 1 + 22 × 1) / (1 + 1)
= 41.175
Displayed score: 41.18For the second result:
(71.04 × 1 + 9 × 1) / (1 + 1)
= 40.02The stronger keyword score moves the mammoth description above the document with the stronger semantic score. This illustrates how the two relevance signals contribute to the final ordering. Oracle describes the RSF calculation in Understand Hybrid Search.
This example shows the effect of combining the scores for this particular question. To determine whether the combined ranking is more useful for an application, we would need to evaluate a representative set of questions and expected results.
Using INTERSECT to combine the results
The previous query used UNION to allow candidates from either search. We can change SEARCH_FUSION to INTERSECT when we want to retain candidates that appear in both retrieved result sets.
The following query makes that change. The semantic question, keyword expression, score weights, and requested number of results remain the same.
demo@ADB26ai> select json_serialize(
2 dbms_hybrid_vector.search(
3 json('{
4 "hybrid_index_name": "PARK_DESCRIPTION_HVI",
5 "search_fusion": "INTERSECT",
6 "search_scorer": "RSF",
7 "vector": {
8 "search_text": "Where can I learn about prehistoric mammals and ancient animal remains?",
9 "search_mode": "DOCUMENT",
10 "aggregator": "MAX",
11 "score_weight": 1
12 },
13 "text": {
14 "contains": "fossil OR fossils OR mammoths",
15 "score_weight": 1
16 },
17 "return": {
18 "topN": 5,
19 "values": [
20 "rowid",
21 "score",
22 "vector_score",
23 "text_score"
24 ]
25 }
26 }
27 ')
28 )
29 returning clob pretty
30 ) as search_results;
SEARCH_RESULTS
--------------------------------------------------------------------------------
[
{
"rowid": "AAQVAeABUAAPJSyABC",
"score": 41.18,
"vector_score": 60.35,
"text_score": 22
},
{
"rowid": "AAQVAeABUAAPJS5AAy",
"score": 40.02,
"vector_score": 71.04,
"text_score": 9
},
{
"rowid": "AAQVAeABUAAPHl8AAF",
"score": 39.19,
"vector_score": 70.37,
"text_score": 8
},
{
"rowid": "AAQVAeABUAAPJS5AAI",
"score": 38.02,
"vector_score": 67.04,
"text_score": 9
},
{
"rowid": "AAQVAeABUAAPJSpAAe",
"score": 37.68,
"vector_score": 67.35,
"text_score": 8
}
]The query returns the same top five documents, in the same order and with the same scores, as the preceding UNION query.
This means the five leading results in this run also survive INTERSECT. It does not mean that UNION and INTERSECT are equivalent, or that the complete candidate sets are identical. A different question or a keyword expression with less overlap could produce different results.
The distinction is in which retrieved candidates qualify. UNION retains candidates from either search, while INTERSECT retains candidates found in both. This applies to the retrieved candidate sets; it is not an unlimited semantic comparison against every document in the table.
Returning the matching passages
The previous queries returned document-level results. An application may also need the relevant passage itself, for example when displaying the text that supports a search result.
For the following query, we change SEARCH_MODE to CHUNK and request CHUNK_ID and CHUNK_TEXT along with the scores. We continue using the same question, keyword expression, INTERSECT operator, and RSF scoring method.
demo@ADB26ai> select json_serialize(
2 dbms_hybrid_vector.search(
3 json('{
4 "hybrid_index_name": "PARK_DESCRIPTION_HVI",
5 "search_fusion": "INTERSECT",
6 "search_scorer": "RSF",
7 "vector": {
8 "search_text": "Where can I learn about prehistoric mammals and ancient animal remains?",
9 "search_mode": "CHUNK",
10 "score_weight": 1
11 },
12 "text": {
13 "contains": "fossil OR fossils OR mammoths",
14 "score_weight": 1
15 },
16 "return": {
17 "topN": 5,
18 "values": [
19 "rowid",
20 "chunk_id",
21 "chunk_text",
22 "score",
23 "vector_score",
24 "text_score"
25 ]
26 }
27 }
28 ')
29 )
30 returning clob pretty
31 ) as search_results;The first result is shown below.
SEARCH_RESULTS
--------------------------------------------------------------------------------
[
{
"rowid": "AAQVAeABUAAPJS5AAy",
"chunk_id": "1",
"chunk_text": "During the Pliocene, this place looked quite different. Lush wetlands,
forests, and grasslands provided excellent habitat for a variety of animals.
From fascinating, now-extinct creatures like the saber-toothed cat, mastodon,
and ground sloth, to more familiar animals like horses, beavers, and birds,
the scientific study of Pliocene fossils is the key to Hagerman.
",
"score": 40.02,
"vector_score": 71.04,
"text_score": 9
}
]Only the first result is shown. The array is shortened for presentation, and the passage is line-wrapped for readability.
The response now includes the matching passage as well as its parent ROWID and chunk identifier. The Hagerman passage discusses extinct animals, including the saber-toothed cat, mastodon, and ground sloth, which helps us understand its relationship to the question.
The scores for all five returned chunks are shown below.
ROWID CHUNK_ID VECTOR_SCORE TEXT_SCORE FINAL_SCORE
-------------------- -------- ------------ ---------- -----------
AAQVAeABUAAPJS5AAy 1 71.04 9 40.02
AAQVAeABUAAPHl8AAF 1 70.37 8 39.19
AAQVAeABUAAPJS5AAI 1 67.04 9 38.02
AAQVAeABUAAPJSpAAe 1 67.35 8 37.68
AAQVAeABUAAPJSgABI 1 64.74 9 36.87This is a compact presentation of the captured JSON results, with the passage text omitted.
Notice that Hagerman is now the first result. The mammoth description that ranked first in document mode does not appear in this top-five chunk result set. The captured output shows a difference between the two requests, but it does not establish the exact cause. Examining a larger candidate set would be a useful next step.
In hybrid chunk mode, the keyword score comes from the parent document. A passage returned by the query should therefore not automatically be assumed to contain every keyword that matched its parent. See the DBMS_HYBRID_VECTOR documentation for the search-mode and fusion details.
These examples use the same hybrid vector index to combine meaning-based retrieval with keyword relevance. SEARCH_FUSION controls which retrieved candidates remain, SEARCH_SCORER controls how they are ranked, and SEARCH_MODE controls whether the results represent documents or individual passages.
For the next experiment, we can keep the question and keyword expression unchanged and adjust the score weights. Changing one setting at a time will make it easier to see how increasing keyword or semantic influence affects the final results.
No comments:
Post a Comment