Hybrid Search Example With curl

Code Examples

Two commands to see hybrid search work on your index from a terminal: the meaning vector of a search, then one {!hybrid} request with it.

# 1. The meaning vector of the search: a JSON array of 1,024 numbers
VECTOR=$(curl -s -G "https://api.opensolr.com/solr_manager/api/embed" \
  --data-urlencode "email=YOUR_EMAIL" \
  --data-urlencode "api_key=YOUR_API_KEY" \
  --data-urlencode "index_name=YOUR_INDEX" \
  --data-urlencode "is_query=1" \
  --data-urlencode "payload=solar panels for a small roof")

# 2. The hybrid search, sent as a POST because the vector is long
curl -s -u "opensolr:YOUR_API_KEY" "https://YOUR_INDEX_HOST/solr/YOUR_INDEX/select" \
  --data-urlencode 'q={!hybrid lexical=$lexicalQuery vector=$vectorQuery mode=union alpha=0.85 topN=500}' \
  --data-urlencode 'lexicalQuery={!edismax qf="title^0.2 description^0.05 uri^0.01 text^0.01 text_t^0.01" mm="2<65% 4<50% 8<40%" v=$uq}' \
  --data-urlencode 'uq=solar panels for a small roof' \
  --data-urlencode "vectorQuery={!knn f=embeddings topK=500}${VECTOR}" \
  --data-urlencode 'fl=id,uri,title,score' \
  --data-urlencode 'rows=5' \
  --data-urlencode 'echoParams=none' \
  --data-urlencode 'wt=json'

If $VECTOR holds an object instead of an array, the plan of the index owner has no AI or the call was refused. Search with words alone then:

curl -s -u "opensolr:YOUR_API_KEY" "https://YOUR_INDEX_HOST/solr/YOUR_INDEX/select" \
  --data-urlencode 'q={!edismax qf="title^0.2 description^0.05 uri^0.01 text^0.01 text_t^0.01" mm="2<65% 4<50% 8<40%" v=$uq}' \
  --data-urlencode 'uq=solar panels for a small roof' \
  --data-urlencode 'fl=id,uri,title,score' \
  --data-urlencode 'rows=5' \
  --data-urlencode 'wt=json'
ReplaceWith
YOUR_EMAILThe email of your Opensolr account.
YOUR_API_KEYYour API key (Account › API Keys). The examples also use it as the password of the index, which is what a new index starts with; if you changed the username or password of the index, put yours: the username and password of your index.
YOUR_INDEXThe name of your Opensolr Index.
YOUR_INDEX_HOSTThe host shown in the Control Panel of the index.

The same flow in an application: PHP, Node.js, Python.