SQL查询中包含多个单词字符串时,W10桌面搜索索引出现异常。

huangapple go评论53阅读模式
英文:

Exception when SQL query has multiple word string to W10 desktop search index

问题

我已经创建了一个基本的PowerShell脚本来查询Windows 10的Windows桌面搜索(WDS)索引。以下是相关部分:

$query = @"
    SELECT System.DateModified, System.ItemPathDisplay 
    FROM SystemIndex 
    WHERE CONTAINS(System.Search.Contents, '$($text)')
"@

$objConnection = New-Object -ComObject adodb.connection
$objrecordset = New-Object -ComObject adodb.recordset

$objrecordset.CursorLocation = 3

$objconnection.open("Provider=Search.CollatorDSO;Extended Properties='Application=Windows';")

$objrecordset.open($query, $objConnection, $adOpenStatic)

到目前为止,我的测试一直在使用单词,一切正常。但当我开始使用两个单词时,出现以下错误:

正在搜索 'and then'...

SELECT System.DateModified, System.ItemPathDisplay 
FROM SystemIndex 
WHERE CONTAINS(System.Search.Contents, 'and then')

来自 HRESULT 的异常: 0x80040E14
在 D:\searchSystemIndex.ps1:72 字符:1
+ $objrecordset.open($query, $objConnection, $adOpenStatic)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

使用资源管理器查询索引使用content:"and then"可以正常工作。

有什么想法?

英文:

I've gobbled together a basic Powershell script to query W10's Windows Desktop Search (WDS) index. Here is the relevant bits,

$query = "
    SELECT System.DateModified, System.ItemPathDisplay 
    FROM SystemIndex 
    WHERE CONTAINS(System.Search.Contents, '$($text)')
"

$objConnection = New-Object -ComObject adodb.connection
$objrecordset = New-Object -ComObject adodb.recordset

$objrecordset.CursorLocation = 3

$objconnection.open("Provider=Search.CollatorDSO;Extended Properties='Application=Windows';")

$objrecordset.open($query, $objConnection, $adOpenStatic)

Until now my tests have been using single words and everything works. But when I started using two words, it falls apart with the following error,

Searching for 'and then'...

SELECT System.DateModified, System.ItemPathDisplay 
FROM SystemIndex 
WHERE CONTAINS(System.Search.Contents, 'and then')

Exception from HRESULT: 0x80040E14
At D:\searchSystemIndex.ps1:72 char:1
+ $objrecordset.open($query, $objConnection, $adOpenStatic)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

Using Explorer to query the index using content:"and then" works fine.

Any ideas?

答案1

得分: 0

根据Windows搜索SQL语法CONTAINS谓词的文档以及示例,如果您想要搜索包含"多个单词或包含空格"的文本,您需要在查询中引用该短语:

...WHERE CONTAINS(' "多个单词或包含空格" ')

因此,在您的示例中,您可能希望执行以下操作:

$text = "and then"

$query = @"
    SELECT System.DateModified, System.ItemPathDisplay 
    FROM SystemIndex 
    WHERE CONTAINS(System.Search.Contents, ' `" + $text + "` ')
"@
#                                           ^^        ^^ 
#                                           引用搜索短语

(请注意,引号前面带有反引号,否则引号将终止整个查询字符串。)

如果您不是要寻找确切的短语"and then",而只想要包含"and"和"then"的结果,似乎需要执行类似以下操作:

$query = @"
    SELECT System.DateModified, System.ItemPathDisplay 
    FROM SystemIndex 
    WHERE CONTAINS(System.Search.Contents, ' `"and`" AND `"then`" ')
#                                          ^^^^^^^^^^^^^^^^^^^^^^
#                                          多个独立的单词
"@
英文:

According to the documentation for Windows Search SQL Syntax and the examples in the CONTAINS predicate, if you want to search for a literal phrase with "multiple words or included spaces" you need to quote the phrase inside the query:

> Type: Phrase
>
> Description: Multiple words or included spaces.
>
> Examples
>
>...WHERE CONTAINS('"computer software"')

So in your example you probably want:

$text = "and then"

$query = "
    SELECT System.DateModified, System.ItemPathDisplay 
    FROM SystemIndex 
    WHERE CONTAINS(System.Search.Contents, '`"$($text)`"')
"
#                                           ^^        ^^ 
#                                           quoted search phrase

(note the quotes are prefixed with a backtick as the quote would otherwise terminate your entire query string.)

If you're not looking for the exact phrase "and then", and you just want results that contain "and" and "then" it looks like you need to to do something like this:

> Type: Boolean
>
> Description: Words, phrases, and wildcard strings combined by using the Boolean operators AND, OR, or NOT. Enclose the Boolean terms in double quotation marks.
>
> Example:
>
> ...WHERE CONTAINS('"computer monitor" AND "software program" AND "install component"')
>
> ...WHERE CONTAINS(' "computer" AND "software" AND "install" ' )

$query = "
    SELECT System.DateModified, System.ItemPathDisplay 
    FROM SystemIndex 
    WHERE CONTAINS(System.Search.Contents, '`"and`" AND `"then`"')
#                                          ^^^^^^^^^^^^^^^^^^^^^^
#                                          multiple independent words
"

huangapple
  • 本文由 发表于 2023年2月8日 20:30:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/75385825.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定