001/*
002 *  Copyright 2026 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016package org.ametys.plugins.ai.search;
017
018import java.util.List;
019import java.util.Map;
020
021import org.apache.avalon.framework.service.ServiceException;
022import org.apache.avalon.framework.service.ServiceManager;
023
024import org.ametys.cms.repository.Content;
025import org.ametys.cms.search.query.JoinQuery;
026import org.ametys.cms.search.query.Query;
027import org.ametys.cms.search.query.Query.Operator;
028import org.ametys.cms.search.query.join.JoinKey;
029import org.ametys.cms.search.systemprop.AbstractIndexableSystemProperty;
030import org.ametys.plugins.ai.AIHelper;
031import org.ametys.runtime.model.type.ModelItemTypeConstants;
032
033/**
034 * Semantic search on contents
035 */
036public class SemanticSearchSystemProperty extends AbstractIndexableSystemProperty<String, String, Content>
037{
038    private AIHelper _aiHelper;
039
040    @Override
041    public void service(ServiceManager manager) throws ServiceException
042    {
043        super.service(manager);
044        
045        _aiHelper = (AIHelper) manager.lookup(AIHelper.ROLE);
046    }
047    
048    public Query getQuery(Object value, Operator operator, String language, Map<String, Object> contextualParameters)
049    {
050        List<Float> vector = _aiHelper.computeEmbedding(value.toString());
051        
052        if (vector == null)
053        {
054            throw new IllegalStateException("No AI provider found, cannot perform semantic search");
055        }
056        
057        return new JoinQuery(
058            new VectorQuery(AIAdditionalDataIndexer.SOLR_EMBEDDING, vector),
059            new JoinKey("id_dv", "chunk_object_s_dv", null)
060        );
061    }
062    
063    public Object getValue(Content ametysObject)
064    {
065        return null;
066    }
067
068    @Override
069    protected String getTypeId()
070    {
071        return ModelItemTypeConstants.STRING_TYPE_ID;
072    }
073    
074    public boolean isSortable()
075    {
076        return false; // According to doc, we can only sort when using q_vector
077    }
078    
079    public boolean isFacetable()
080    {
081        return false;
082    }
083}