[TikiWiki-commits] [Git][tikiwiki/tiki][master] [NEW] Make it possible to search within specific fields from the index

"Victor Emanouilov \(@kroky\) via TikiWiki-cvs" <[email protected]>
Newsgroups gmane.comp.cms.tiki.cvs
Message-ID <6a018325e373b_38190cbc245d8@gitlab-sidekiq-low-urgency-cpu-bound-v2-7c5cd7b8d-65c6p.mail>

Victor Emanouilov pushed to branch master at Tiki Wiki CMS Groupware / Tiki


Commits:
d01f0399 by Merci Jacob at 2026-05-11T07:04:54+00:00
[NEW] Make it possible to search within specific fields from the index
---
* add fields search restriction

* elasticsearch: fetch field mappings from api when not yet generated

* Revert "[NEW] searchlib: Add a field-aggregator facet to select fields in which to find objects"

This reverts commit 92bfcd699e337a8bd0ae0b73997edc49182e8cb5.

* [NEW] searchlib: Add a field-aggregator facet to select fields in which to find objects

TODO:
- patch `Indexer->getAvailableFields()` to properly allow fields not not mapped yet from index
- the field selector should show fields only from selected object types

See merge request tikiwiki/tiki!10131

- - - - -


4 changed files:

- lib/core/Search/Elastic/Index.php
- lib/search/searchlib-unified.php
- templates/tiki-searchfacets.tpl
- tiki-searchindex.php


Changes:

=====================================
lib/core/Search/Elastic/Index.php
=====================================
@@ -49,9 +49,21 @@ class Search_Elastic_Index implements Search_Index_Interface, Search_Index_Query
      */
     public function getFieldMappings()
     {
+        if (empty($this->providedMappings)) {
+            return $this->getRawFieldMappings();
+        }
+
         return $this->providedMappings;
     }
 
+    protected function getRawFieldMappings()
+    {
+        $index = $this->index;
+        $mappings = $this->connection->rawApi("/$index/_mapping/field/*");
+        $mappings = get_object_vars($mappings->{$index}->mappings);
+        return $mappings;
+    }
+
     public function exists()
     {
         $indexStatus = $this->connection->getIndexStatus($this->index);


=====================================
lib/search/searchlib-unified.php
=====================================
@@ -1383,6 +1383,14 @@ class UnifiedSearchLib
             $query->filterTags($filter['tags']);
         }
 
+        if (isset($filter['fields']) && $filter['fields'] && isset($filter['content'])) {
+            $fields = array_map('trim', explode(',', $filter['fields']));
+            $query->filterContent($filter['content'], $fields);
+
+            unset($filter['fields']);
+            unset($filter['content']);
+        }
+
         if (isset($filter['content']) && $filter['content']) {
             $query->filterContent($filter['content'], $this->getFilterContentFields());
         }


=====================================
templates/tiki-searchfacets.tpl
=====================================
@@ -11,17 +11,32 @@
             <div class="pe-1">
                 <label class="sr-only" for="filter~content">{tr}Search Query{/tr}</label>
                 <div class="input-group">
-                    <input class="form-control" type="search" name="filter~content" value="{$filter.content|escape}"/>
-                    <input type="submit" class="btn btn-info" value="{tr}Search{/tr}"/>
+                    <input class="form-control" type="search" name="filter~content" value="{$filter.content|escape}" placeholder="{tr}Search{/tr}"/>
                 </div>
                 {foreach from=$facets item=facet}
                     <input type="hidden" name="filter~{$facet|escape}" value="{$postfilter[$facet]|default:$filter[$facet]|escape}"/>
                 {/foreach}
             </div>
+            <div class="ps-1">
+                <input type="hidden" name="filter~fields" value="{$filter.fields|escape}"/>
+                <select id="filter-fields" class="form-select" multiple placeholder="{tr}Search in fields{/tr}">
+                    {foreach from=$filter_fields item=field}
+                        <option value="{$field|escape}"{if in_array($field, explode(',', $filter.fields))} selected{/if}>{tr}{$field|escape}{/tr}</option>
+                    {/foreach}
+                </select>
+                {jq}
+                    $('#filter-fields').on('change', function () {
+                        $('input[name="filter~fields"]').val($(this).val().join(','));
+                    });
+                {/jq}
+            </div>
+            <div class="ps-1">
+                <input type="submit" class="btn btn-primary btn-sm" value="{tr}Search{/tr}"/>
+            </div>
             {if $prefs.tracker_tabular_enabled eq 'y' && ! empty($smarty.get.tabularId)}
                 <div class="px-1">
                     <input type="hidden" name="tabularId" value="{$smarty.get.tabularId|escape}">
-                    <button class="tabular-export btn btn-secondary">
+                    <button class="tabular-export btn btn-secondary btn-sm">
                         {icon name=export} {tr}Export{/tr}
                     </button>
                     {jq}
@@ -36,7 +51,7 @@
                 </div>
             {elseif $prefs.tracker_tabular_enabled eq 'y' && ! empty($filter.tracker_id)}
                 <div class="px-1">
-                    <button class="tabular-export btn btn-secondary">
+                    <button class="tabular-export btn btn-secondary btn-sm">
                         {icon name=export} {tr}Export{/tr}
                     </button>
                     {jq}
@@ -55,8 +70,8 @@
             {if $prefs.storedsearch_enabled eq 'y' and $user}
                 <div class="ps-1">
                     <input type="hidden" name="storeAs" value=""/>
-                    <a href="{service controller=search_stored action=select modal=true}" id="store-query" class="btn btn-secondary">{tr}Save Search{/tr}</a>
-                    <a href="{service controller=search_stored action=list}" class="btn btn-link">{tr}View Saved Searches{/tr}</a>
+                    <a href="{service controller=search_stored action=select modal=true}" id="store-query" class="btn btn-secondary btn-sm">{tr}Save Search{/tr}</a>
+                    <a href="{service controller=search_stored action=list}" class="btn btn-link btn-sm">{tr}View Saved Searches{/tr}</a>
                     {jq}
                         $('#store-query').clickModal({
                             success: function (data) {


=====================================
tiki-searchindex.php
=====================================
@@ -170,6 +170,9 @@ if (count($filter) || count($postfilter)) {
 $smarty->assign('filter', $filter);
 $smarty->assign('postfilter', $postfilter);
 $smarty->assign('facets', $facets);
+$fields = TikiLib::lib('unifiedsearch')->getAvailableFields();
+$fields = array_unique(array_merge(array_values($fields['global']), ...array_values($fields['object_types'])));
+$smarty->assign('filter_fields', $fields);
 
 // disallow robots to index page:
 $smarty->assign('metatag_robots', 'NOINDEX, NOFOLLOW');



View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/d01f0399c46644695692b0b66ac94c8f76b89b40

-- 
View it on GitLab: https://gitlab.com/tikiwiki/tiki/-/commit/d01f0399c46644695692b0b66ac94c8f76b89b40
You're receiving this email because of your account on gitlab.com. Manage all notifications: https://gitlab.com/-/profile/notifications | Help: https://gitlab.com/help

_______________________________________________
TikiWiki-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/tikiwiki-cvs
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.