My nodes have a geofield and my search query should find all the nodes within a range.

I've managed to create a search form against my Solr Server with views. But I want to do the request by myself (having my own form and submit method). There's a solution for D7 here: How to send a custom Solr Query using Search API Solr but none of that seems to be usable with the D8 modules.

I believe there must be something like

$result = SolrSearchService('myServer', 'myIndex')->request(['title' => 'Test']);

But I can not find anything.

The views-built search page renders a query against my Solr Server like

path=/select params={q=(phonm_rendered_item:nintendo^1+tm_title:nintendo^8)&json.nl=flat&pt=48.1371079,11.5753822&d=50&omitHeader=true&fl=ss_search_api_id,ss_search_api_language,score,fts_field_geo_koordinaten__distance:geodist()&start=0&fq=%2Bindex_id:default_solr_index+%2Bhash:ozztjn&fq={!geofilt}&rows=10&sfield=locs_field_geo_koordinaten&wt=json} 

Apparently there are some parameters responsible for the distance calculation:

  • pt=48.1371079,11.5753822 - it's where I am
  • d=50 - it's the max distance
  • field_geo_koordinaten - it's the geofield in the node
  • geodist() - the function ?

How can I render such a query with Search API?

up vote 0 down vote accepted

Debugging around I found the solution:

$index = Index::load('default_solr_index');
/* @var \Drupal\search_api\Query\Query $query */
$query = $index->query();
$query->keys($form_state->getValue("keys"));
$location_options = (array) $query->getOption('search_api_location', []);

$location_options[] = [
      'field' => "field_geo_koordinaten",
      'lat' => 48.1371079,
      'lon' => 11.5753822,
      'radius' => 50,
    ];

$query->setOption('search_api_location', $location_options);
$results = $query->execute();

Thanks for pointing me in the right direction.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.