001/** 002 * Copyright (c) 2025-2026, Michael Yang 杨福海 (fuhai999@gmail.com). 003 * <p> 004 * Licensed under the GNU Lesser General Public License (LGPL) ,Version 3.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 * <p> 008 * http://www.gnu.org/licenses/lgpl-3.0.txt 009 * <p> 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 dev.tinyflow.core.node; 017 018import dev.tinyflow.core.chain.Chain; 019import dev.tinyflow.core.searchengine.SearchEngine; 020import dev.tinyflow.core.searchengine.SearchEngineManager; 021import dev.tinyflow.core.util.Maps; 022import dev.tinyflow.core.util.StringUtil; 023import dev.tinyflow.core.util.TextTemplate; 024import org.slf4j.Logger; 025 026import java.util.Collections; 027import java.util.List; 028import java.util.Map; 029 030public class SearchEngineNode extends BaseNode { 031 032 private static final Logger logger = org.slf4j.LoggerFactory.getLogger(SearchEngineNode.class); 033 034 private String engine; 035 private String limit; 036 private String keyword; 037 038 public String getEngine() { 039 return engine; 040 } 041 042 public void setEngine(String engine) { 043 this.engine = engine; 044 } 045 046 public String getLimit() { 047 return limit; 048 } 049 050 public void setLimit(String limit) { 051 this.limit = limit; 052 } 053 054 public String getKeyword() { 055 return keyword; 056 } 057 058 public void setKeyword(String keyword) { 059 this.keyword = keyword; 060 } 061 062 @Override 063 public Map<String, Object> execute(Chain chain) { 064 Map<String, Object> formatParameters = getFormatParameters(chain); 065 String realKeyword = TextTemplate.of(keyword).formatToString(formatParameters); 066 String realLimitString = TextTemplate.of(limit).formatToString(formatParameters); 067 int realLimit = 10; 068 if (StringUtil.hasText(realLimitString)) { 069 try { 070 realLimit = Integer.parseInt(realLimitString); 071 } catch (Exception e) { 072 logger.error(e.toString(), e); 073 } 074 } 075 076 SearchEngine searchEngine = SearchEngineManager.getInstance().geSearchEngine(engine); 077 078 if (searchEngine == null) { 079 return Collections.emptyMap(); 080 } 081 082 List<Map<String, Object>> result = searchEngine.search(realKeyword, realLimit, this, chain); 083 return Maps.of("documents", result); 084 } 085 086 087 @Override 088 public String toString() { 089 return "SearchEngineNode{" + 090 "engine='" + engine + '\'' + 091 ", limit='" + limit + '\'' + 092 ", keyword='" + keyword + '\'' + 093 ", parameters=" + parameters + 094 ", outputDefs=" + outputDefs + 095 ", id='" + id + '\'' + 096 ", name='" + name + '\'' + 097 ", description='" + description + '\'' + 098 ", condition=" + condition + 099 ", validator=" + validator + 100 ", loopEnable=" + loopEnable + 101 ", loopIntervalMs=" + loopIntervalMs + 102 ", loopBreakCondition=" + loopBreakCondition + 103 ", maxLoopCount=" + maxLoopCount + 104 ", retryEnable=" + retryEnable + 105 ", resetRetryCountAfterNormal=" + resetRetryCountAfterNormal + 106 ", maxRetryCount=" + maxRetryCount + 107 ", retryIntervalMs=" + retryIntervalMs + 108 ", computeCostExpr='" + computeCostExpr + '\'' + 109 '}'; 110 } 111}