Files
doc-exports/docs/css/umn/css_01_0081.html
zhengxiu 93d856d5c5 css umn 25.6.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-11-25 11:34:43 +00:00

80 lines
8.5 KiB
HTML

<a name="EN-US_TOPIC_0000001992165589"></a><a name="EN-US_TOPIC_0000001992165589"></a>
<h1 class="topictitle1">Using DSL to Search for Data in OpenSearch</h1>
<div id="body0000001992165589"><p id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_p17394111161812">DSL is the specified query language for OpenSearch. It is the best language for interaction between OpenSearch clusters and clients. OpenSearch DSL is a JSON-based language. Other languages, such as SQL, are translated into OpenSearch DSL before they can be used for interacting with OpenSearch clusters.</p>
<p id="EN-US_TOPIC_0000001992165589__p9262163325312">This topic lists some of the most commonly used OpenSearch DSL query statements. For more, see <a href="https://opensearch.org/docs/latest/query-dsl/" target="_blank" rel="noopener noreferrer">Query DSL</a>.</p>
<div class="section" id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_section383919455212"><h4 class="sectiontitle">DSL Usage Example</h4><p id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_p1059184122211">Compile the request content in JSON format on the Dev Tools page of OpenSearch Dashboards and execute the search request.</p>
<p id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_p1663415301464">For example, run the following command to retrieve all documents in the <strong id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_b241613581863">test</strong> index:</p>
<div class="codecoloring" codetype="Json" id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_en-us_topic_0000001268594557_screen158809477411"><div class="highlight"><table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><span class="normal">1</span>
<span class="normal">2</span>
<span class="normal">3</span>
<span class="normal">4</span>
<span class="normal">5</span>
<span class="normal">6</span></pre></div></td><td class="code"><div><pre><span></span><span class="err">GET</span><span class="w"> </span><span class="err">/</span><span class="kc">test</span><span class="err">/_search</span>
<span class="p">{</span>
<span class="w"> </span><span class="nt">&quot;query&quot;</span><span class="p">:</span><span class="w"> </span><span class="p">{</span>
<span class="w"> </span><span class="nt">&quot;match_all&quot;</span><span class="p">:</span><span class="w"> </span><span class="p">{}</span>
<span class="w"> </span><span class="p">}</span>
<span class="p">}</span>
</pre></div></td></tr></table></div>
</div>
<p id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_p1678182553119">The search result is also in JSON format.</p>
</div>
<div class="section" id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_section4944125615312"><h4 class="sectiontitle">Common DSL Query Statements</h4><ul id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_ul186240196385"><li id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_li946616485469">Sets the query filters, which is equivalent to <strong id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_b12501514493">where</strong> in the SQL language.<p id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_p1152724914464">In the command below, there is no index filter in front of <span class="parmvalue" id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_parmvalue6305154474618"><b>_search</b></span>, so all indexes are queried. A bool query allows you to combine multiple search queries with boolean conditions. <strong id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_b140175018129">filter</strong> forcibly filters documents whose <span class="parmname" id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_parmname1430574474617"><b>status</b></span> field is <span class="parmvalue" id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_parmvalue830594412464"><b>published</b></span> and <span class="parmname" id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_parmname43050446465"><b>publish_date</b></span> is later than <span class="parmvalue" id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_parmvalue630510447466"><b>2015-01-01</b></span>. <strong id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_b1356974913149">must</strong> specifies that both <span class="parmname" id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_parmname1930534412469"><b>title</b></span> and <span class="parmname" id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_parmname17306744104611"><b>content</b></span> must include <span class="parmvalue" id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_parmvalue2306194410468"><b>Search</b></span>.</p>
<div class="note" id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_note196387513488"><img src="public_sys-resources/note_3.0-en-us.png"><span class="notetitle"> </span><div class="notebody"><p id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_p107541385475">The difference between <strong id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_b859423171615">must</strong> and <strong id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_b26001369161">filter</strong> is that <strong id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_b01054175163">filter</strong> is equivalent to <strong id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_b4121203013164">where</strong> in SQL but its results are not used for scoring. The <strong id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_b17395121471810">must</strong> field is also a mandatory filter criteria, but the matching documents are scored based on relevance. The most relevant documents are displayed at the top.</p>
</div></div>
<pre class="screen" id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_screen12896143643812">GET /_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "Search"
}
},
{
"match": {
"content": "search"
}
}
],
"filter": [
{
"term": {
"status": "published"
}
},
{
"range": {
"publish_date": {
"gte": "2015-01-01"
}
}
}
]
}
}
}</pre>
</li><li id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_li169821932163813"><strong id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_b2820579225">Aggregations</strong> are similar to <strong id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_b176144422220">Group by</strong> in SQL.<p id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_p1318110279496">An aggregation summarizes your data as metrics, statistics, or other analytics. In the example below, the results are aggregated based on the title field in the <strong id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_b08541525102416">test</strong> index. If <strong id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_b3520942162416">title</strong> is of the text (including keyword) type, use <span class="parmvalue" id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_parmvalue9835747105018"><b>title.keyword</b></span> for aggregation. By default, a cluster cannot directly aggregate data of the text type. <strong id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_b814692272516">titles</strong> is only an example name of the aggregation. You can name the aggregation <strong id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_b8330101772618">titleaggs</strong> instead.</p>
<pre class="screen" id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_screen437744944813">GET /test/_search
{
"aggs": {
"titles": {
"terms": {
"field": "title.keyword"
}
}
}
}</pre>
<p id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_p51815276497">The example above for query aggregation includes all documents in the <strong id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_b9137259152610">test</strong> index. That is, <strong id="EN-US_TOPIC_0000001992165589__en-us_topic_0000001945472646_b162584202276">match_all</strong> is used. You can set search criteria to narrow the scope of the aggregation to specific documents.</p>
</li></ul>
</div>
</div>
<div>
<div class="familylinks">
<div class="parentlink"><strong>Parent topic:</strong> <a href="css_01_0074.html">Searching Data in an OpenSearch Cluster</a></div>
</div>
</div>