> For the complete documentation index, see [llms.txt](https://docs.hyper-space.io/hyperspace-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.hyper-space.io/hyperspace-docs/flows/setting-up/building-and-running-queries/building-a-lexical-search-query.md).

# Building a Lexical Search Query

Lexical Search, or Classic Search, is a fundamental approach to retrieve information based on keyword, integer, float, date etc. value matching. This search operates by assessing the similarity of a collection documents to a query document. It then selects the documents with the top scores and return their id's to the user.&#x20;

## Running the Lexical (Classic) Search Query in DSL Syntax

{% tabs %}
{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
query = {
        "query": {
            "bool": {
                "must": [
                    {"term": {"name": "John"}}
                ]
            }
        }
    }
results = hyperspace_client.dsl_search(query,
                                   size=10,                 
                                   collection_name=collection_name)
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
String queryJson =
                    "{" +
                    "  \"query\": {" +
                    "    \"bool\": {" +
                    "      \"must\": [" +
                    "        {" +
                    "          \"term\":{" +
                    "            \"name\":\"John\"" +
                    "           }" +
                    "        }" +
                    "      ]" +
                    "    }" +
                    "  }" +
                    "}";
JsonObject query = JsonParser.parseString(queryJson).getAsJsonObject();
Object response = client.dslSearch(collectionName, 10, query));
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}

<pre class="language-javascript" data-line-numbers><code class="lang-javascript">let size = 10;
let query = {
<strong>    "query": {
</strong>        "bool": {
            "must": [
                {"term": {"name": "John"}}
            ]
        }
    }
}
await hyperspaceClient.dslSearch(collectionName, size, query)
</code></pre>

{% endtab %}
{% endtabs %}

Where <mark style="color:purple;">query</mark> is your query logic, see example below.

{% tabs %}
{% tab title="Python" %}
{% code lineNumbers="true" %}

```python
query = {
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "genres": "genres_value"
              }
            },
            {
              "term": {
                "adult": "adult_value"
              }
            },
            {
              "bool": {
                "must_not": [
                  {
                    "term": {
                      "title": "title_value"
                    }
                  }
                ]
              }
            }
          ],
          "should": [
            {
              "range": {
                "rating": {
                  "gt": 7.0
                }
              }
            }
          ]
        }
      },
      "boost_mode": "multiply",
      "boost": 2.0
    }
  }
}
```

{% endcode %}
{% endtab %}

{% tab title="Java" %}
{% code lineNumbers="true" %}

```java
String queryJson =" { " + 
"    \"function_score\": { " + 
"      \"query\": { " + 
"        \"bool\": { " + 
"          \"must\": [ " + 
"            { " + 
"              \"term\": { " + 
"                \"genres\": \"genres_value\" " + 
"              } " + 
"            }, " + 
"            { " + 
"              \"term\": { " + 
"                \"adult\": \"adult_value\" " + 
"              } " + 
"            }, " + 
"            { " + 
"              \"bool\": { " + 
"                \"must_not\": [ " + 
"                  { " + 
"                    \"term\": { " + 
"                      \"title\": \"title_value\" " + 
"                    } " + 
"                  } " + 
"                ] " + 
"             } " + 
"            } " + 
"          ], " + 
"          \"should\": [ " + 
"            { " + 
"              \"range\": { " + 
"                \"rating\": { " + 
"                  \"gt\": 7.0 " + 
"                } " + 
"              } " + 
"            } " + 
"          ] " + 
"        } " + 
"      }, " + 
"      \"boost_mode\": \"multiply\", " + 
"      \"boost\": 2.0 " + 
"    } " + 
"  } " + 
"} " + 
JsonObject query = JsonParser.parseString(queryJson).getAsJsonObject();
Object response = client.dslSearch(collectionName, 10, query));
```

{% endcode %}
{% endtab %}

{% tab title="JavaScript" %}
{% code lineNumbers="true" %}

```javascript
let query = {
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "genres": "genres_value"
              }
            },
            {
              "term": {
                "adult": "adult_value"
              }
            },
            {
              "bool": {
                "must_not": [
                  {
                    "term": {
                      "title": "title_value"
                    }
                  }
                ]
              }
            }
          ],
          "should": [
            {
              "range": {
                "rating": {
                  "gt": 7.0
                }
              }
            }
          ]
        }
      },
      "boost_mode": "multiply",
      "boost": 2.0
    }
  }
};
```

{% endcode %}
{% endtab %}
{% endtabs %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.hyper-space.io/hyperspace-docs/flows/setting-up/building-and-running-queries/building-a-lexical-search-query.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
