1

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?

5
  • Are you using the search API module with solr? if so this might help drupal.org/docs/8/modules/search-api/developer-documentation/…
    – Leigh
    Commented Dec 6, 2018 at 11:41
  • Thanks, Leigh. I am using Search API and Solr Search - and I am already testing some approaches with the code in your link. But it seems to be pretty difficult. Commented Dec 6, 2018 at 11:47
  • Thanks again, I got it working with your link. Now I am puzzling with my geofield and search radius. I will change the question. Commented Dec 6, 2018 at 12:18
  • I have some examples I can post later. You can leverage SearchAPI to some extent - or use the Solarium client directly.
    – Kevin
    Commented Dec 6, 2018 at 12:23
  • Here is a very very basic example: gist.github.com/kevinquillen/cad07541347385744b161a26690ab75d
    – Kevin
    Commented Dec 6, 2018 at 12:26

1 Answer 1

2

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 agree to our terms of service and acknowledge you have read our privacy policy.

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