SQL如'%term%',除了没有字母

时间:2015-08-15 21:55:36

标签: sql sql-server tsql wildcard sql-like

我正在搜索一篇新闻报道。 2个相关列为ArticleTitleArticleText。当我想搜索特定术语的文章时,我开始使用

column LIKE '%term%'

然而,这给了我很多关于锚链接内的术语的文章,例如<a href="example.com/*term*>可能会返回一篇不相关的文章。

然后我切换到

column LIKE '% term %'

此查询的问题是它没有找到文章的标题或文本以术语开头/结尾。它也与我{I} term-term's之类的内容不匹配。

似乎我想要的查询应该可以做这样的事情

'%[^a-z]term[^a-z]%

这应该排除锚链接中的术语,但不包括其他所有内容。我认为这个查询仍然排除了以该术语开头/结尾的字符串。有更好的解决方案吗? SQL-Server的FULL TEXT INDEXING是否解决了这个问题?

此外,将ArticleTitleArticleText存储为无HTML列是一个好主意吗?然后我可以使用'%term%'而无需获取锚链接。这些将是2个额外的列,因为最终我将需要原始的HTML用于格式化目的。

感谢。

1 个答案:

答案 0 :(得分:2)

SQL Server的[SqlException (0x80131904): A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)] System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +6573870 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +717 System.Data.SqlClient.TdsParser.Connect(ServerInfo serverInfo, SqlInternalConnectionTds connHandler, Boolean ignoreSniOpenTimeout, Int64 timerExpire, Boolean encrypt, Boolean trustServerCert, Boolean integratedSecurity, Boolean withFailover) +6600312 System.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean ignoreSniOpenTimeout, TimeoutTimer timeout, Boolean withFailover) +219 System.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout) +6602662 System.Data.SqlClient.SqlInternalConnectionTds.OpenLoginEnlist(TimeoutTimer timeout, SqlConnectionString connectionOptions, SqlCredential credential, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance) +6603203 System.Data.SqlClient.SqlInternalConnectionTds..ctor(DbConnectionPoolIdentity identity, SqlConnectionString connectionOptions, SqlCredential credential, Object providerInfo, String newPassword, SecureString newSecurePassword, Boolean redirectedUserInstance, SqlConnectionString userConnectionOptions, SessionData reconnectSessionData) +942 System.Data.SqlClient.SqlConnectionFactory.CreateConnection(DbConnectionOptions options, DbConnectionPoolKey poolKey, Object poolGroupProviderInfo, DbConnectionPool pool, DbConnection owningConnection, DbConnectionOptions userOptions) +1162 System.Data.ProviderBase.DbConnectionFactory.CreatePooledConnection(DbConnectionPool pool, DbConnection owningObject, DbConnectionOptions options, DbConnectionPoolKey poolKey, DbConnectionOptions userOptions) +72 System.Data.ProviderBase.DbConnectionPool.CreateObject(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) +6606457 System.Data.ProviderBase.DbConnectionPool.UserCreateRequest(DbConnection owningObject, DbConnectionOptions userOptions, DbConnectionInternal oldConnection) +103 System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) +2102 System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) +116 System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) +1079 System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions) +6610951 System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry) +233 System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry) +278 System.Data.SqlClient.SqlConnection.Open() +239 Hangfire.SqlServer.SqlServerStorage.CreateAndOpenConnection() +49 Hangfire.SqlServer.SqlServerStorage..ctor(String nameOrConnectionString, SqlServerStorageOptions options) +279 Hangfire.SqlServerStorageExtensions.UseSqlServerStorage(IGlobalConfiguration configuration, String nameOrConnectionString) +80 MyWebApplication1.Startup.Configuration(IAppBuilder app) +39 [TargetInvocationException: Exception has been thrown by the target of an invocation.] System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) +0 System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) +76 System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) +211 System.Reflection.MethodBase.Invoke(Object obj, Object[] parameters) +35 Owin.Loader.<>c__DisplayClass1.<LoadImplementation>b__0(IAppBuilder builder) +341 Microsoft.Owin.Host.SystemWeb.OwinAppContext.Initialize(Action`1 startup) +996 Microsoft.Owin.Host.SystemWeb.OwinHttpModule.InitializeBlueprint() +119 System.Threading.LazyInitializer.EnsureInitializedCore(T& target, Boolean& initialized, Object& syncLock, Func`1 valueFactory) +241 Microsoft.Owin.Host.SystemWeb.OwinHttpModule.Init(HttpApplication context) +108 System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +530 System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +304 System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +404 System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +475 [HttpException (0x80004005): Exception has been thrown by the target of an invocation.] System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +12618692 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +159 System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +12458309 允许您像您所描述的那样定义类似Regex的模式。

更好的选择是使用fulltext search

LIKE

正确利用索引(WHERE CONTAINS(ArticleTitle, 'term') 查询速度慢),并在搜索算法中提供其他好处。

此外,您可能会受益于将文章的纯文本版本与HTML版本一起存储,并在其上运行搜索查询。