4

This question is similar to another question I had, but now in a different context. My previous question was: Search text box for Search API + Database + Search Views + Facets API + Facets Bonus

That option worked, except I can't use Current Search to reset the search box. I can reset the filters, but not search box. I also can't find an option to allow the user to sort the fields. I installed the Search API Sort module, but was unable to get any fields to be available. I kept hosing my index trying to enable/disable fields, and couldn't figure it out.

My question is how can I use a search form box with Search API + Views that will let me reset the search box with Current Search box, and, how can I have a sort box that will let the visitor select the field to sort by?

My view is rendering nodes via Display Suite view modes. Right now, I am using exposed search and sort filters, but of course, they are grouped in one block. The whole point of my having to use the Search API was to provide filters and decouple my search and sort filters from each other. My sort option will be placed at the top right of the UI, and the sort in the left sidebar.

EDIT: Using Search API Sorts, per giga's advice below, I went back and tried to get my fields sortable. I stripped down fields and still no luck getting them sortable. It's a Commerce site, and I'm using the Commerce Search API, which gives me a Product Display index. I originally move fields of content out of the index, so I'm only left with my product display and product fields.

My indexed fields right now are (field, type, boost):

Content type -- String -- 1.0
Title -- String -- 1.0
Date created -- Date -- 1.0
Date changed -- Date -- 1.0
Product Tags -- Taxonomy term
Product Ref -- Commerce Product
Item Type -- Taxonomy term
Weight Sort -- Integer -- 1.0
Product Ref >> Price >> Amount (decimal)(Min) -- Decimal -- 1.0
Product Ref >> Price >> Amount (decimal)(Max) -- Decimal -- 1.0
Title (Fulltext) [search_api_aggregation_1] -- Fulltext -- 1.0
Product Tags (Fulltext) [search_api_aggregation_4] -- Fulltext - 1.0
Item Type name (Fulltext) [search_api_aggregation_5] -- Fulltext - 1.0
Product tags >> Name -- String -- 1.0
Proudct Ref >> Serving Size -- String -- 1.0
Product Ref >> Portion Size -- Fulltext -- 1.0
Item Type >> Name -- String -- 1.0
Product Ref >> Price >> Amount (decimal) -- Decimal -- 1.0

The only fields I'm trying to index are Title, Min and Max price, and the Weight Sort as the initial default sort. None of them are showing up in my table of sortable fields under the Sorts tab.

5 Answers 5

12
+50

Search API Sorts only lists the fields of supported types. Make sure the fields you want to sort on are single value strings or numbers.

If the fields are mutli-value you can use Search API Ranges‎ to give you max/min value fields that you can use.

If a field is full-text, one trick is to create an aggregated field containing just that field and assign it a string type.

7
  • I need to sort by a sort weight field (as a default sort -- doesn't need to be exposed to the user), min/max price (asc or dec), the title name, and a taxonomy term; I still couldn't get any fields listed as sortable. I don't understand the aggregated fields suggestion.
    – vintorg
    Mar 8, 2014 at 1:31
  • The aggregation thing is a workaround to have a field indexed twice as two different types e.g. full text for keyword search and string for sorting. You'd probably want to do this for your title field.
    – gigabates
    Mar 8, 2014 at 2:12
  • For the other fields make sure you have them checked as indexed in the fields tab, including the asc and desc variations that the search_api_ranges module creates for the price. I assume this is a Commerce site, in which case I have an almost identical setup working, so I'm struggling to think what else you could be missing.
    – gigabates
    Mar 8, 2014 at 2:16
  • I'm starting with the title field first. I go to add a new Aggregation Field, but I don't have the option for string -- just fulltext. Should I change the current indexed fulltext to string, and add this new field as fulltext? It seems last time I tried to change my title field from fulltext, I kept hosing my index and couldn't re-index.
    – vintorg
    Mar 8, 2014 at 2:35
  • Yeah you're right, I have it that way round on my site.
    – gigabates
    Mar 8, 2014 at 3:41
4

worked this out...with the help of answer 3,

Commerce kickstart, Search API,

Added Aggregation Field here : admin/config/search/search_api/index/product_display/workflow

under the "filters" tab then look for "Callback settings" Click on "Aggregated fields" to turn it in to the active tab

I Added a taxonomy weight field and named it something like "Aggregation Taxonomy Weight " I selected "full text" as the format.

Then Save & Reindex

Then in the "fields" section of admin/config/search/search_api/index/product_display/workflow

My new field was showing up for indexing....;-)

Tick new field to be index ...make sure it is a "string"

Save & Reindex

Go to view ...

Add a new CONTEXTUAL FILTER ....look for your new field "Aggregation Taxonomy Weight "

Goto SORT CRITERIA in your view....you new field is available to sort....

Took me three days to work it out LOL!

SO just had to share...Good Luck!

3

It is a known problem in the Sort module's last update. Commenting out the block of code posted here fixes my issue.

To fix the issue use the latest dev version.

https://drupal.org/node/2202093

0

For multi value fields you need denormalization provided by this module https://www.drupal.org/project/search_api_grouping

0

Well, I will try to explain my approach.

I didn't use Sorts module. The steps I took are the following:

  1. I made a custom field with this hook: hook_entity_property_info_alter() in a separate module.
  2. In my getter callback I found the weight of the term I wanted and returned it as INTEGER like: return intval($weight);
  3. I ticked the newly created field to index it and chose Integer as format. Re-indexed the index.
  4. Lastly, in my view in Sort criteria I chose Sort ascending and voila!!

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.