Files
doc-exports/docs/css/umn/css_01_0123.html
zhengxiu 2125539080 css umn 25.1.0 version
Reviewed-by: Pruthi, Vineet <vineet.pruthi@t-systems.com>
Co-authored-by: zhengxiu <zhengxiu@huawei.com>
Co-committed-by: zhengxiu <zhengxiu@huawei.com>
2025-07-04 09:10:17 +00:00

305 lines
25 KiB
HTML

<a name="css_01_0123"></a><a name="css_01_0123"></a>
<h1 class="topictitle1">Using Vector Indexes for Data Search in an Elasticsearch Cluster</h1>
<div id="body0000001245354483"><div class="p" id="css_01_0123__p3494164217576">Vector indexes support data searches in different ways.<ul id="css_01_0123__ul1415819211585"><li id="css_01_0123__li11581826586"><a href="#css_01_0123__en-us_topic_0000001268154489_section344710865418">Standard Query</a></li><li id="css_01_0123__li1915815215814"><a href="#css_01_0123__en-us_topic_0000001268154489_section102341611718">Composite Query</a></li><li id="css_01_0123__li91581921580"><a href="#css_01_0123__en-us_topic_0000001268154489_section4946246546">ScriptScore Query</a></li><li id="css_01_0123__li11158920581"><a href="#css_01_0123__en-us_topic_0000001268154489_section107953451661">Re-Score Query</a></li><li id="css_01_0123__li121593215820"><a href="#css_01_0123__section102071126171611">Painless Syntax Extension</a></li></ul>
</div>
<div class="section" id="css_01_0123__en-us_topic_0000001268154489_section344710865418"><a name="css_01_0123__en-us_topic_0000001268154489_section344710865418"></a><a name="en-us_topic_0000001268154489_section344710865418"></a><h4 class="sectiontitle">Standard Query</h4><p id="css_01_0123__en-us_topic_0000001268154489_p10699133014548">Standard vector query syntax is provided for vector fields with vector indexes. The following command will return <em id="css_01_0123__en-us_topic_0000001268154489_i6193191024117">n</em> (specified by <strong id="css_01_0123__en-us_topic_0000001268154489_b524152410413">size</strong>/<strong id="css_01_0123__en-us_topic_0000001268154489_b7169626174118">topk</strong>) data records that are most close to the query vector.</p>
<pre class="screen" id="css_01_0123__en-us_topic_0000001268154489_screen1466920229569">POST my_index/_search
{
"size":2,
"_source": false,
"query": {
"vector": {
"my_vector": {
"vector": [1, 1],
"topk":2
}
}
}
}</pre>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="css_01_0123__en-us_topic_0000001268154489_table112016411577" frame="border" border="1" rules="all"><caption><b>Table 1 </b>Parameters for standard query</caption><thead align="left"><tr id="css_01_0123__en-us_topic_0000001268154489_row120119417572"><th align="left" class="cellrowborder" valign="top" width="24.12%" id="mcps1.3.2.4.2.3.1.1"><p id="css_01_0123__en-us_topic_0000001268154489_p152017415713">Parameter</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="75.88000000000001%" id="mcps1.3.2.4.2.3.1.2"><p id="css_01_0123__en-us_topic_0000001268154489_p02013445711">Description</p>
</th>
</tr>
</thead>
<tbody><tr id="css_01_0123__en-us_topic_0000001268154489_row1420184205713"><td class="cellrowborder" valign="top" width="24.12%" headers="mcps1.3.2.4.2.3.1.1 "><p id="css_01_0123__en-us_topic_0000001268154489_p102012475715">vector (the first one)</p>
</td>
<td class="cellrowborder" valign="top" width="75.88000000000001%" headers="mcps1.3.2.4.2.3.1.2 "><p id="css_01_0123__en-us_topic_0000001268154489_p6201742577">Indicates that the query type is <strong id="css_01_0123__en-us_topic_0000001268154489_b257724474217">VectorQuery</strong>.</p>
</td>
</tr>
<tr id="css_01_0123__en-us_topic_0000001268154489_row1120194155720"><td class="cellrowborder" valign="top" width="24.12%" headers="mcps1.3.2.4.2.3.1.1 "><p id="css_01_0123__en-us_topic_0000001268154489_p42011645578">my_vector</p>
</td>
<td class="cellrowborder" valign="top" width="75.88000000000001%" headers="mcps1.3.2.4.2.3.1.2 "><p id="css_01_0123__en-us_topic_0000001268154489_p920110435711">Indicates the name of the vector field you want to query.</p>
</td>
</tr>
<tr id="css_01_0123__en-us_topic_0000001268154489_row39015465716"><td class="cellrowborder" valign="top" width="24.12%" headers="mcps1.3.2.4.2.3.1.1 "><p id="css_01_0123__en-us_topic_0000001268154489_p17901854135712">vector (the second one)</p>
</td>
<td class="cellrowborder" valign="top" width="75.88000000000001%" headers="mcps1.3.2.4.2.3.1.2 "><p id="css_01_0123__en-us_topic_0000001268154489_p290145445715">Indicates the vector value you want to query, which can be an array or a Base64 string</p>
</td>
</tr>
<tr id="css_01_0123__en-us_topic_0000001268154489_row0480125417579"><td class="cellrowborder" valign="top" width="24.12%" headers="mcps1.3.2.4.2.3.1.1 "><p id="css_01_0123__en-us_topic_0000001268154489_p24811654135718">topk</p>
</td>
<td class="cellrowborder" valign="top" width="75.88000000000001%" headers="mcps1.3.2.4.2.3.1.2 "><p id="css_01_0123__en-us_topic_0000001268154489_p11481554155712">Same as the value of <strong id="css_01_0123__en-us_topic_0000001268154489_b560421614493">size</strong> generally.</p>
</td>
</tr>
<tr id="css_01_0123__en-us_topic_0000001268154489_row1271143335915"><td class="cellrowborder" valign="top" width="24.12%" headers="mcps1.3.2.4.2.3.1.1 "><p id="css_01_0123__en-us_topic_0000001268154489_p971133385920">Other optional parameters</p>
</td>
<td class="cellrowborder" valign="top" width="75.88000000000001%" headers="mcps1.3.2.4.2.3.1.2 "><p id="css_01_0123__en-us_topic_0000001268154489_p27143355918">Indicates optional query parameters. You can adjust the vector index parameters to achieve higher query performance or precision. For more information, see <a href="#css_01_0123__en-us_topic_0000001268154489_table172381842595">Table 2</a>.</p>
</td>
</tr>
</tbody>
</table>
</div>
<div class="tablenoborder"><a name="css_01_0123__en-us_topic_0000001268154489_table172381842595"></a><a name="en-us_topic_0000001268154489_table172381842595"></a><table cellpadding="4" cellspacing="0" summary="" id="css_01_0123__en-us_topic_0000001268154489_table172381842595" frame="border" border="1" rules="all"><caption><b>Table 2 </b>Optional query parameters</caption><thead align="left"><tr id="css_01_0123__en-us_topic_0000001268154489_row223811465915"><th align="left" class="cellrowborder" valign="top" width="15.15%" id="mcps1.3.2.5.2.4.1.1"><p id="css_01_0123__en-us_topic_0000001268154489_p17238114195911">Type</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="24.46%" id="mcps1.3.2.5.2.4.1.2"><p id="css_01_0123__en-us_topic_0000001268154489_p19238194195920">Parameter</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="60.39%" id="mcps1.3.2.5.2.4.1.3"><p id="css_01_0123__en-us_topic_0000001268154489_p182381943596">Description</p>
</th>
</tr>
</thead>
<tbody><tr id="css_01_0123__en-us_topic_0000001268154489_row623816413595"><td class="cellrowborder" rowspan="2" valign="top" width="15.15%" headers="mcps1.3.2.5.2.4.1.1 "><p id="css_01_0123__en-us_topic_0000001268154489_p523844115912">Graph index configuration parameters</p>
</td>
<td class="cellrowborder" valign="top" width="24.46%" headers="mcps1.3.2.5.2.4.1.2 "><p id="css_01_0123__en-us_topic_0000001268154489_p1723812465912">ef</p>
</td>
<td class="cellrowborder" valign="top" width="60.39%" headers="mcps1.3.2.5.2.4.1.3 "><p id="css_01_0123__en-us_topic_0000001268154489_p174132034174514">Queue size of the neighboring node during the query. A larger value indicates a higher query precision and slower query speed. The default value is <strong id="css_01_0123__en-us_topic_0000001268154489_b7905614124620">200</strong>.</p>
<p id="css_01_0123__en-us_topic_0000001268154489_p12238249592">Value range: (0, 100000]</p>
</td>
</tr>
<tr id="css_01_0123__en-us_topic_0000001268154489_row9238341594"><td class="cellrowborder" valign="top" headers="mcps1.3.2.5.2.4.1.1 "><p id="css_01_0123__en-us_topic_0000001268154489_p102381543592">max_scan_num</p>
</td>
<td class="cellrowborder" valign="top" headers="mcps1.3.2.5.2.4.1.2 "><p id="css_01_0123__en-us_topic_0000001268154489_p1502203510457">Maximum number of scanned nodes. A larger value indicates a higher query precision and slower query speed. The default value is <strong id="css_01_0123__en-us_topic_0000001268154489_b11210116477">10000</strong>.</p>
<p id="css_01_0123__en-us_topic_0000001268154489_p15239184195911">Value range: (0, 1000000]</p>
</td>
</tr>
<tr id="css_01_0123__en-us_topic_0000001268154489_row2023913425914"><td class="cellrowborder" valign="top" width="15.15%" headers="mcps1.3.2.5.2.4.1.1 "><p id="css_01_0123__en-us_topic_0000001268154489_p15239124155914">IVF index configuration parameters</p>
</td>
<td class="cellrowborder" valign="top" width="24.46%" headers="mcps1.3.2.5.2.4.1.2 "><p id="css_01_0123__en-us_topic_0000001268154489_p12239164185913">nprobe</p>
</td>
<td class="cellrowborder" valign="top" width="60.39%" headers="mcps1.3.2.5.2.4.1.3 "><p id="css_01_0123__en-us_topic_0000001268154489_p11820136184520">Number of center points. A larger value indicates a higher query precision and slower query speed. The default value is <strong id="css_01_0123__en-us_topic_0000001268154489_b265764411479">100</strong>.</p>
<p id="css_01_0123__en-us_topic_0000001268154489_p142399412593">Value range: (0, 100000]</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section" id="css_01_0123__en-us_topic_0000001268154489_section102341611718"><a name="css_01_0123__en-us_topic_0000001268154489_section102341611718"></a><a name="en-us_topic_0000001268154489_section102341611718"></a><h4 class="sectiontitle">Composite Query</h4><p id="css_01_0123__en-us_topic_0000001268154489_p364401311819">Vector search can be used with other Elasticsearch subqueries to implement a composite query, such as pre-filtering query and Boolean query.</p>
<div class="note" id="css_01_0123__note84281450266"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="css_01_0123__p242820500615">Only Elasticsearch 7.10.2 clusters support pre-filtering queries.</p>
</div></div>
<p id="css_01_0123__p53711361930">In the following example, the top 10 records whose <span class="parmname" id="css_01_0123__parmname203064516415"><b>my_label</b></span> value is <span class="parmvalue" id="css_01_0123__parmvalue162651181355"><b>red</b></span> are queried.</p>
<ul id="css_01_0123__ul238620710614"><li id="css_01_0123__li1583916257117">Pre-filtering query: First, filters are used to retrieve matching results. Then, vector search is performed on these results to find the most relevant vectors based on similarity.<div class="p" id="css_01_0123__p5328926115"><a name="css_01_0123__li1583916257117"></a><a name="li1583916257117"></a>The following is an example of the code:<pre class="screen" id="css_01_0123__screen2076214231683">POST my_index/_search
{
"size": 10,
"query": {
"vector": {
"my_vector": {
"vector": [1, 2],
"topk": 10,
"filter": {
"term": { "my_label": "red" }
}
}
}
}
}</pre>
<div class="note" id="css_01_0123__note13735642194318"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="css_01_0123__p07351425437">If the <span class="parmname" id="css_01_0123__parmname11859142122615"><b>filter</b></span> is too restrictive, leading to a small intermediate result set, you can set the index parameter <span class="parmname" id="css_01_0123__parmname201396545275"><b>index.vector.exact_search_threshold</b></span> to switch from pre-filtering query to brute-force query, thus improving the recall of the query. For details about the parameter, see <a href="css_01_0121.html#css_01_0121__en-us_topic_0000001309709789_section137344225249">Creating a Vector Index</a>.</p>
</div></div>
</div>
</li><li id="css_01_0123__li01282063320">Boolean query: A Boolean query is in fact a post-filtering query method. Filtering and vector similarity-search are performed separately. Then, the results of the two are combined. The logic of combination is determined by clauses like must, should, and filter.<div class="p" id="css_01_0123__p152088710317"><a name="css_01_0123__li01282063320"></a><a name="li01282063320"></a>The following is an example of the code:<pre class="screen" id="css_01_0123__en-us_topic_0000001268154489_screen6703123515212">POST my_index/_search
{
"size": 10,
"query": {
"bool": {
"must": {
"vector": {
"my_vector": {
"vector": [1, 2],
"topk": 10
}
}
},
"filter": {
"term": { "my_label": "red" }
}
}
}
}</pre>
</div>
</li></ul>
</div>
<div class="section" id="css_01_0123__en-us_topic_0000001268154489_section4946246546"><a name="css_01_0123__en-us_topic_0000001268154489_section4946246546"></a><a name="en-us_topic_0000001268154489_section4946246546"></a><h4 class="sectiontitle">ScriptScore Query</h4><p id="css_01_0123__en-us_topic_0000001268154489_p191338162050">You can use <strong id="css_01_0123__en-us_topic_0000001268154489_b10212131312154">script_score</strong> to perform Nearest Neighbor Search (NSS) on vectors. The query syntax is provided below.</p>
<p id="css_01_0123__en-us_topic_0000001268154489_p1512359042">The pre-filtering condition can be any query. <strong id="css_01_0123__en-us_topic_0000001268154489_b11388750131013">script_score</strong> traverses only the pre-filtered results, calculates the vector similarity, and sorts and returns the results. This query method does not use vector indexes. Its performance depends on the size of the intermediate result set after the pre-filtering. If the pre-filtering condition is set to <strong id="css_01_0123__en-us_topic_0000001268154489_b11385181114112">match_all</strong>, brute-force search is performed on all data.</p>
<pre class="screen" id="css_01_0123__en-us_topic_0000001268154489_screen3512185915411">POST my_index/_search
{
"size":2,
"query": {
"script_score": {
"query": {
"match_all": {}
},
"script": {
"source": "vector_score",
"lang": "vector",
"params": {
"field": "my_vector",
"vector": [1.0, 2.0],
"metric": "euclidean"
}
}
}
}
}</pre>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="css_01_0123__en-us_topic_0000001268154489_table95121659546" frame="border" border="1" rules="all"><caption><b>Table 3 </b>script_score parameters</caption><thead align="left"><tr id="css_01_0123__en-us_topic_0000001268154489_row1451214594419"><th align="left" class="cellrowborder" valign="top" width="30.3%" id="mcps1.3.4.5.2.3.1.1"><p id="css_01_0123__en-us_topic_0000001268154489_p551216591840">Parameter</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="69.69999999999999%" id="mcps1.3.4.5.2.3.1.2"><p id="css_01_0123__en-us_topic_0000001268154489_p105128591413">Description</p>
</th>
</tr>
</thead>
<tbody><tr id="css_01_0123__en-us_topic_0000001268154489_row135121591243"><td class="cellrowborder" valign="top" width="30.3%" headers="mcps1.3.4.5.2.3.1.1 "><p id="css_01_0123__en-us_topic_0000001268154489_p651218591747">source</p>
</td>
<td class="cellrowborder" valign="top" width="69.69999999999999%" headers="mcps1.3.4.5.2.3.1.2 "><p id="css_01_0123__en-us_topic_0000001268154489_p1351225910417">Script description. Its value is <strong id="css_01_0123__en-us_topic_0000001268154489_b247465910117">vector_score</strong> if the vector similarity is used for scoring.</p>
</td>
</tr>
<tr id="css_01_0123__en-us_topic_0000001268154489_row175123599411"><td class="cellrowborder" valign="top" width="30.3%" headers="mcps1.3.4.5.2.3.1.1 "><p id="css_01_0123__en-us_topic_0000001268154489_p75121759143">lang</p>
</td>
<td class="cellrowborder" valign="top" width="69.69999999999999%" headers="mcps1.3.4.5.2.3.1.2 "><p id="css_01_0123__en-us_topic_0000001268154489_p1651235910417">Script syntax description. Its value is <strong id="css_01_0123__en-us_topic_0000001268154489_b187361212623">vector</strong>.</p>
</td>
</tr>
<tr id="css_01_0123__en-us_topic_0000001268154489_row1651219599413"><td class="cellrowborder" valign="top" width="30.3%" headers="mcps1.3.4.5.2.3.1.1 "><p id="css_01_0123__en-us_topic_0000001268154489_p1451211592047">field</p>
</td>
<td class="cellrowborder" valign="top" width="69.69999999999999%" headers="mcps1.3.4.5.2.3.1.2 "><p id="css_01_0123__en-us_topic_0000001268154489_p1351218596418">Vector field name</p>
</td>
</tr>
<tr id="css_01_0123__en-us_topic_0000001268154489_row1551285917417"><td class="cellrowborder" valign="top" width="30.3%" headers="mcps1.3.4.5.2.3.1.1 "><p id="css_01_0123__en-us_topic_0000001268154489_p165129591342">vector</p>
</td>
<td class="cellrowborder" valign="top" width="69.69999999999999%" headers="mcps1.3.4.5.2.3.1.2 "><p id="css_01_0123__en-us_topic_0000001268154489_p16512195915416">Vector data to be queried</p>
</td>
</tr>
<tr id="css_01_0123__en-us_topic_0000001268154489_row751218591947"><td class="cellrowborder" valign="top" width="30.3%" headers="mcps1.3.4.5.2.3.1.1 "><p id="css_01_0123__en-us_topic_0000001268154489_p18512659244">metric</p>
</td>
<td class="cellrowborder" valign="top" width="69.69999999999999%" headers="mcps1.3.4.5.2.3.1.2 "><p id="css_01_0123__en-us_topic_0000001268154489_p75126591544">Measurement method, which can be <strong id="css_01_0123__en-us_topic_0000001268154489_b8193126124912">euclidean</strong>, <strong id="css_01_0123__en-us_topic_0000001268154489_b184521131114911">inner_product</strong>, <strong id="css_01_0123__en-us_topic_0000001268154489_b392818339492">cosine</strong>, and <strong id="css_01_0123__en-us_topic_0000001268154489_b6805153644910">hamming</strong>.</p>
<p id="css_01_0123__en-us_topic_0000001268154489_p35122592420">Default value: <strong id="css_01_0123__en-us_topic_0000001268154489_b47131738114915">euclidean</strong></p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section" id="css_01_0123__en-us_topic_0000001268154489_section107953451661"><a name="css_01_0123__en-us_topic_0000001268154489_section107953451661"></a><a name="en-us_topic_0000001268154489_section107953451661"></a><h4 class="sectiontitle">Re-Score Query</h4><p id="css_01_0123__en-us_topic_0000001268154489_p147213301172">If the <strong id="css_01_0123__en-us_topic_0000001268154489_b19947215207">GRAPH_PQ</strong> or <strong id="css_01_0123__en-us_topic_0000001268154489_b16658223132017">IVF_GRAPH_PQ</strong> index is used, the query results are sorted based on the asymmetric distance calculated by PQ. CSS supports re-scoring and ranking of query results to improve the recall rate.</p>
<p id="css_01_0123__en-us_topic_0000001268154489_p1567411217323">Assuming that <strong id="css_01_0123__en-us_topic_0000001268154489_b8942142419315">my_index</strong> is a PQ index, an example of re-scoring the query results is as follows:</p>
<pre class="screen" id="css_01_0123__en-us_topic_0000001268154489_screen1639814155510">GET my_index/_search
{
"size": 10,
"query": {
"vector": {
"my_vector": {
"vector": [1.0, 2.0],
"topk": 100
}
}
},
"rescore": {
"window_size": 100,
"vector_rescore": {
"field": "my_vector",
"vector": [1.0, 2.0],
"metric": "euclidean"
}
}
}</pre>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="css_01_0123__en-us_topic_0000001268154489_table165271512103218" frame="border" border="1" rules="all"><caption><b>Table 4 </b>Rescore parameter description</caption><thead align="left"><tr id="css_01_0123__en-us_topic_0000001268154489_row96748123320"><th align="left" class="cellrowborder" valign="top" width="30.3%" id="mcps1.3.5.5.2.3.1.1"><p id="css_01_0123__en-us_topic_0000001268154489_p467481273214">Parameter</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="69.69999999999999%" id="mcps1.3.5.5.2.3.1.2"><p id="css_01_0123__en-us_topic_0000001268154489_p7674111213212">Description</p>
</th>
</tr>
</thead>
<tbody><tr id="css_01_0123__en-us_topic_0000001268154489_row4674161215326"><td class="cellrowborder" valign="top" width="30.3%" headers="mcps1.3.5.5.2.3.1.1 "><p id="css_01_0123__en-us_topic_0000001268154489_p136741112123210">window_size</p>
</td>
<td class="cellrowborder" valign="top" width="69.69999999999999%" headers="mcps1.3.5.5.2.3.1.2 "><p id="css_01_0123__en-us_topic_0000001268154489_p116742129325">Vector search returns <em id="css_01_0123__en-us_topic_0000001268154489_i1196913103618">topk</em> search results and ranks the first <em id="css_01_0123__en-us_topic_0000001268154489_i3963141519619">window_size</em> results.</p>
</td>
</tr>
<tr id="css_01_0123__en-us_topic_0000001268154489_row19674612133213"><td class="cellrowborder" valign="top" width="30.3%" headers="mcps1.3.5.5.2.3.1.1 "><p id="css_01_0123__en-us_topic_0000001268154489_p16674171213212">field</p>
</td>
<td class="cellrowborder" valign="top" width="69.69999999999999%" headers="mcps1.3.5.5.2.3.1.2 "><p id="css_01_0123__en-us_topic_0000001268154489_p667431253217">Vector field name</p>
</td>
</tr>
<tr id="css_01_0123__en-us_topic_0000001268154489_row6674151212329"><td class="cellrowborder" valign="top" width="30.3%" headers="mcps1.3.5.5.2.3.1.1 "><p id="css_01_0123__en-us_topic_0000001268154489_p14674171273210">vector</p>
</td>
<td class="cellrowborder" valign="top" width="69.69999999999999%" headers="mcps1.3.5.5.2.3.1.2 "><p id="css_01_0123__en-us_topic_0000001268154489_p176748125322">Vector data to be queried</p>
</td>
</tr>
<tr id="css_01_0123__en-us_topic_0000001268154489_row7674121216327"><td class="cellrowborder" valign="top" width="30.3%" headers="mcps1.3.5.5.2.3.1.1 "><p id="css_01_0123__en-us_topic_0000001268154489_p167419129328">metric</p>
</td>
<td class="cellrowborder" valign="top" width="69.69999999999999%" headers="mcps1.3.5.5.2.3.1.2 "><p id="css_01_0123__en-us_topic_0000001268154489_p1767451253213">Measurement method, which can be <strong id="css_01_0123__en-us_topic_0000001268154489_b1555915506358">euclidean</strong>, <strong id="css_01_0123__en-us_topic_0000001268154489_b156085018355">inner_product</strong>, <strong id="css_01_0123__en-us_topic_0000001268154489_b1556018501357">cosine</strong>, and <strong id="css_01_0123__en-us_topic_0000001268154489_b175601850103512">hamming</strong>.</p>
<p id="css_01_0123__en-us_topic_0000001268154489_p5674131217326">Default value: <strong id="css_01_0123__en-us_topic_0000001268154489_b761714314614">euclidean</strong></p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
<div class="section" id="css_01_0123__section102071126171611"><a name="css_01_0123__section102071126171611"></a><a name="section102071126171611"></a><h4 class="sectiontitle">Painless Syntax Extension</h4><p id="css_01_0123__p177861335101615">CSS extension supports multiple vector distance calculation functions, which can be directly used in customized painless scripts to build flexible re-score formulas.</p>
<p id="css_01_0123__p18965620112713">The following is an example:</p>
<pre class="screen" id="css_01_0123__screen11315195710305">POST my_index/_search
{
"size": 10,
"query": {
"script_score": {
"query": {
"match_all": {}
},
"script": {
"source": "1 / (1 + euclidean(params.vector, doc[params.field]))",
"params": {
"field": "my_vector",
"vector": [1, 2]
}
}
}
}
}</pre>
<div class="tablenoborder"><table cellpadding="4" cellspacing="0" summary="" id="css_01_0123__table17610185023516" frame="border" border="1" rules="all"><caption><b>Table 5 </b>Supported distance vector calculation functions</caption><thead align="left"><tr id="css_01_0123__row461012507354"><th align="left" class="cellrowborder" valign="top" width="34.32%" id="mcps1.3.6.5.2.3.1.1"><p id="css_01_0123__p196101850203512">Function Signature</p>
</th>
<th align="left" class="cellrowborder" valign="top" width="65.68%" id="mcps1.3.6.5.2.3.1.2"><p id="css_01_0123__p1061095020358">Description</p>
</th>
</tr>
</thead>
<tbody><tr id="css_01_0123__row1261045014357"><td class="cellrowborder" valign="top" width="34.32%" headers="mcps1.3.6.5.2.3.1.1 "><p id="css_01_0123__p13610450103520">euclidean(Float[], DocValues)</p>
</td>
<td class="cellrowborder" valign="top" width="65.68%" headers="mcps1.3.6.5.2.3.1.2 "><p id="css_01_0123__p2610125033511">Euclidean distance function</p>
</td>
</tr>
<tr id="css_01_0123__row961085053513"><td class="cellrowborder" valign="top" width="34.32%" headers="mcps1.3.6.5.2.3.1.1 "><p id="css_01_0123__p1610205014351">cosine(Float[], DocValues)</p>
</td>
<td class="cellrowborder" valign="top" width="65.68%" headers="mcps1.3.6.5.2.3.1.2 "><p id="css_01_0123__p6610750143515">Cosine similarity function</p>
</td>
</tr>
<tr id="css_01_0123__row1961015501351"><td class="cellrowborder" valign="top" width="34.32%" headers="mcps1.3.6.5.2.3.1.1 "><p id="css_01_0123__p46101950163517">innerproduct(Float[], DocValues)</p>
</td>
<td class="cellrowborder" valign="top" width="65.68%" headers="mcps1.3.6.5.2.3.1.2 "><p id="css_01_0123__p461015012354">Inner product function</p>
</td>
</tr>
<tr id="css_01_0123__row98852044174013"><td class="cellrowborder" valign="top" width="34.32%" headers="mcps1.3.6.5.2.3.1.1 "><p id="css_01_0123__p13885174412404">hamming(String, DocValues)</p>
</td>
<td class="cellrowborder" valign="top" width="65.68%" headers="mcps1.3.6.5.2.3.1.2 "><p id="css_01_0123__p18852044114019">Hamming distance function Only vectors whose <strong id="css_01_0123__b144391029103117">dim_type</strong> is <strong id="css_01_0123__b9490931163116">binary</strong> are supported. The input query vector must be a Base64-encoded character string.</p>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="css_01_0117.html">Configuring Vector Search for Elasticsearch Clusters</a></div>
</div>
</div>