Code Examples: Build Your Own Search UI

Code Examples

Your own search page or app can run the same hybrid search as the hosted search page: the meaning vector of what the visitor typed, then one Solr request that ranks by words and meaning together.

01 · The flow

01

Your page sends the search to your server. The API key and the password of the index stay on the server.

02

Your server asks for the meaning vector: GET https://api.opensolr.com/solr_manager/api/embed with email, api_key, index_name, is_query=1 and the text in payload. The answer is a JSON array of 1,024 numbers. The embed endpoint.

03

Your server sends one search to Solr: a POST to the /select URL of your index with the {!hybrid} parser and the vector.

04

Your server answers your page with the JSON of Solr, or only the fields the page shows.

is_query=1 marks the text as a search, the way the hosted search page embeds searches. Each vector counts as an AI request of the index owner, except text that was embedded before: what site search uses from your plan.

02 · When there is no vector

The embed endpoint answers an object instead of an array when it cannot make a vector, for example {"status":false,"msg":"VECTOR_NOT_ALLOWED"} when the plan of the index owner has no AI. Search with words alone then: the same edismax query, without the vector. Every example below does this on its own.

03 · The examples

PHP

search.php: an endpoint that answers JSON.

Node.js

server.mjs: a small server, no packages.

Python

search.py: a function you can run from the command line.

curl

Two commands to try it from a terminal.

Complete search page

The HTML page that shows results, highlights, facets and pages from any of these endpoints.

04 · Shortcuts

The exact request of your own search page, with its Search Tuning: the Query Inspector. The vector and the search in one call: embed_and_search.