沙尘暴 / 轻量级Elasticsearch
README
...这是为Neos CMS提供更轻量级Elasticsearch集成的一次尝试。这是基于我想尝试一些不同的设计决策在Neos <-> Elasticsearch集成部分。
这是一个围绕Flowpack.Elasticsearch.ContentRepositoryAdaptor的包装器,它替换了其API的部分。对维护Flowpack.Elasticsearch.ContentRepositoryAdaptor和Flowpack.Elasticsearch的所有人表示衷心的感谢,因为我们在此基础上构建并从中获得了巨大的利益。
目标和限制
项目具有以下目标和限制
-
仅用于全文搜索
这意味着只有文档节点或可能出现在全文搜索结果中的任何内容被放入Elasticsearch索引(在NodeTypes中标记为
search.fulltext.isRoot = TRUE
的内容)。这意味着(默认情况下)不存储内容节点或内容集合在索引中。 -
更简单的全文索引实现
全文收集在PHP中完成,而不是在Elasticsearch中使用Painless完成。
-
查询结果不特定于Neos
您可以轻松编写针对存储在Elasticsearch中的任何内容的查询;而不仅仅是Neos节点。我们提供了示例和实用程序,说明如何将其他数据源索引到Elasticsearch中。
-
更灵活和简单的查询API
查询API与Elasticsearch API相一致;可以编写任意Elasticsearch搜索查询。我们不支持
Neos\Flow\Persistence\QueryResultInterface
,因此没有<f:widget.paginate>
以保持简单。 -
仅支持批量索引
我们目前仅支持批量索引,因为这可以消除Neos UI中与Elasticsearch索引相关的问题。
这是一个“人为的限制”,可以取消;但我们目前不提供对此取消的支持。
-
仅支持单个Elasticsearch版本
我们目前只支持Elasticsearch 7。
-
仅索引工作空间
我们只索引工作空间,因为这支持99%的情况。
-
使用多个Elasticsearch请求进行分面/每个请求一个聚合
同时为所有分面和查询构建一个巨大的Elasticsearch请求是可能的,但很难调试和理解。
因此,我们在这里保持简单;如果您使用聚合,每个聚合将有一个查询来完成。
为开发启动Elasticsearch
docker run --rm --name neos7-es -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" docker.elastic.co/elasticsearch/elasticsearch:7.10.2
索引
提示:索引行为与在Flowpack.ElasticSearch.ContentRepositoryAdaptor中定义的方式相同。唯一的区别是内部实现:我们不是单独索引每个节点(内容和文档),然后让Elasticsearch进行合并,而是在PHP中将内容合并到父文档中,因为这更容易处理。
索引的全配置与Flowpack.ElasticSearch.ContentRepositoryAdaptor中的完全相同。以下是为了您的方便而包含的配置。
以下命令需要用于索引
./flow nodeindex:build ./flow nodeindex:cleanup
注意:只有被标记为search.fulltext.isRoot
的节点(在相应的NodeTypes.yaml
中)才会成为搜索索引的一部分,并且它们所有子内容节点的文本也将作为索引的一部分。
内部机制
- 不同的索引策略是通过自定义的
DocumentNodeIndexer
实现的,然后调用自定义的DocumentIndexerDriver
。
例如,您可以使用以下方式查询Elasticsearch索引
curl -X GET "localhost:9200/neoscr/_search?pretty" -H 'Content-Type: application/json' -d' { "query": { "match_all": {} } } '
按属性(索引字段)的配置
您可以在每个字段级别更改分析器;例如,您可以使用以下片段在NodeTypes.yaml中重新配置_all字段。通常这是通过在[nodeType].search.elasticSearchMapping
中定义全局映射来实现的。
'Neos.Neos:Node': search: elasticSearchMapping: myProperty: analyzer: custom_french_analyzer
排除节点类型进行索引
默认情况下,索引过程处理所有节点类型,但您可以在您的Settings.yaml中更改此设置。
Neos: ContentRepository: Search: defaultConfigurationPerNodeType: '*': indexed: true 'Neos.Neos:FallbackNode': indexed: false 'Neos.Neos:Shortcut': indexed: false 'Neos.Neos:ContentCollection': indexed: false
您需要明确配置单个节点类型(此功能不检查超类型配置)。但您可以使用特殊符号来配置完整命名空间,例如,Acme.AcmeCom:*
将应用于Acme.AcmeCom
命名空间中的所有节点类型。最具体的配置按以下顺序使用
- 节点类型名称(《Neos.Neos:Shortcut》)
- 完整命名空间表示(《Neos.Neos:*》)
- 通配符(《*》)
按数据类型进行索引配置
默认配置支持大多数用例,并且通常不需要修改,因为这个包为所有Neos数据类型提供了合理的默认值。
属性索引在两个地方进行配置。每个数据类型的默认值配置在Settings.yaml
中的Neos.ContentRepository.Search.defaultConfigurationPerType
内。此外,您可以使用NodeTypes.yaml
内的properties.[....].search
路径进行覆盖。
此配置包含两部分
- 在
elasticSearchMapping
下方,可以定义Elasticsearch属性映射。 - 在
indexing
下方,需要指定一个处理索引前值的Eel表达式。它可以访问当前的value
和当前的node
。
示例(来自默认配置)
# Settings.yaml Neos: ContentRepository: Search: defaultConfigurationPerType: # strings should just be indexed with their simple value. string: elasticSearchMapping: type: string indexing: '${value}'
按属性进行索引配置
# NodeTypes.yaml 'Neos.Neos:Timable': properties: '_hiddenBeforeDateTime': search: # A date should be mapped differently, and in this case we want to use a date format which # Elasticsearch understands elasticSearchMapping: type: DateTime format: 'date_time_no_millis' indexing: '${(node.hiddenBeforeDateTime ? Date.format(node.hiddenBeforeDateTime, "Y-m-d\TH:i:sP") : null)}'
如果您的节点类型模式定义了DateTime类型的自定义属性,您必须在您的NodeTypes.yaml
中为它们提供类似的配置,否则它们将无法正确索引。
在Indexing
命名空间中包含一些索引助手,可以在indexing
表达式中使用。在大多数情况下,您不需要修改此设置,但它们是构建标准索引配置所必需的。
Indexing.buildAllPathPrefixes
:对于如foo/bar/baz
之类的路径,构建一个路径前缀列表,例如['foo', 'foo/bar', 'foo/bar/baz']
。Indexing.extractNodeTypeNamesAndSupertypes(NodeType)
:提取传递的节点类型及其所有超类型节点类型的名称列表。Indexing.convertArrayOfNodesToArrayOfNodeIdentifiers(array $nodes)
:将给定的节点转换为它们的节点标识符。
跳过属性索引和映射
如果您不想索引属性,请设置indexing: false
。在这种情况下,不会为此字段配置映射。这也可以用来解决具有相同名称但类型不同的两个节点属性的类型冲突。
全文索引
为了启用全文索引,每个Document
节点必须配置为全文根。因此,默认配置中配置了以下内容
'Neos.Neos:Document': search: fulltext: isRoot: true
一个 全文根 包含了其非文档子项的所有 内容,因此当在这些文本中进行搜索时,文档本身会被作为结果返回。
为了指定节点中属性的全文应该如何提取,这需要在 NodeTypes.yaml
中的 properties.[propertyName].search.fulltextExtractor
进行配置。
示例:
'Neos.Neos.NodeTypes:Text': properties: 'text': search: fulltextExtractor: '${Indexing.extractHtmlTags(value)}' 'My.Blog:Post': properties: title: search: fulltextExtractor: '${Indexing.extractInto("h1", value)}'
处理日期
默认情况下,Elasticsearch 在 UTC 时区索引日期。为了使其使用当前在 PHP 中配置的时区进行索引,表示日期的任何节点属性的配置应如下所示:
'My.Blog:Post': properties: date: search: elasticSearchMapping: type: 'date' format: 'date_time_no_millis' indexing: '${(value ? Date.format(value, "Y-m-d\TH:i:sP") : null)}'
这很重要,以便日期和时间相关的搜索可以按预期工作,无论是使用格式化的 DateTime 字符串还是使用相对 DateTime 计算(例如:now
、now+1d
)。
如果您想按日期过滤项目,例如显示日期晚于今天的项目,您可以创建如下查询:
${...greaterThan('date', Date.format(Date.Now(), "Y-m-d\TH:i:sP"))...}
有关 Elasticsearch 日期格式的更多信息,请点击此处:点击这里。
处理资产/附件
如果您想索引附件,您需要安装 Elasticsearch Ingest-Attachment 插件。然后,您可以将以下内容添加到您的 Settings.yaml
中:
Neos: ContentRepository: Search: defaultConfigurationPerType: 'Neos\Media\Domain\Model\Asset': elasticSearchMapping: type: text indexing: ${Indexing.Indexing.extractAssetContent(value)}
或者将附件内容添加到您的 NodeType 配置中的全文字段中。
properties: file: type: 'Neos\Media\Domain\Model\Asset' ui: search: fulltextExtractor: ${Indexing.extractInto('text', Indexing.extractAssetContent(value))}
默认情况下,Indexing.extractAssetContent(value)
返回资产内容。您可以使用第二个参数返回资产元数据。字段参数可以设置为以下之一:content, title, name, author, keywords, date, content_type, content_length, language
。
有了这个,例如,您可以将文件的关键字添加到更高加权的字段中。
properties: file: type: 'Neos\Media\Domain\Model\Asset' ui: search: fulltextExtractor: ${Indexing.extractInto('h2', Indexing.extractAssetContent(value, 'keywords'))}
搜索组件
由于搜索组件通常需要大量调整,我们仅包含可以复制/粘贴并调整到您项目的代码片段。
prototype(My.Package:Search) < prototype(Neos.Fusion:Component) {
// for possibilities on how to build the query, see the next section in the documentation
_elasticsearchBaseQuery = ${Elasticsearch.createNeosFulltextQuery(site).fulltext(request.arguments.q)}
@context.mainSearchRequest = ${Elasticsearch.createRequest(site).query(this._elasticsearchBaseQuery))}
// Search Result Display is controlled through Flowpack.Listable
searchResults = Flowpack.Listable:PaginatedCollection {
collection = ${mainSearchRequest}
itemsPerPage = 12
// we use cache mode "dynamic" for the full Search component; so we do not need an additional cache entry
// for the PaginatedCollection.
@cache.mode = "embed"
}
renderer = afx`
<form action="." method="get">
<input name="q" value={request.arguments.q}/>
<button type="submit">Search</button>
<div @if.isError={mainSearchRequest.execute().error}>
There was an error executing the search request. Please try again in a few minutes.
</div>
<p>Showing {mainSearchRequest.execute().count()} of {mainSearchRequest.execute().total()} results</p>
{props.searchResults}
</form>
`
// If you want to see the full request going to Elasticsearch, you can include
// the following snippet in the renderer above:
// <Neos.Fusion:Debug v={Json.stringify(mainSearchRequest.requestForDebugging())} />
// The parameter "q" should be included in this pagination
prototype(Flowpack.Listable:PaginationParameters) {
q = ${request.arguments.q}
}
// We configure the cache mode "dynamic" here.
@cache {
mode = 'dynamic'
entryIdentifier {
node = ${node}
type = 'searchForm'
}
entryDiscriminator = ${request.arguments.q + '-' + request.arguments.currentPage}
context {
1 = 'node'
2 = 'documentNode'
3 = 'site'
}
entryTags {
1 = ${Neos.Caching.nodeTag(node)}
}
}
}
// The result display is done here.
// In the context, you'll find an object `searchResultDocument` which is of type
// Sandstorm\LightweightElasticsearch\Query\Result\SearchResultDocument.
prototype(Sandstorm.LightweightElasticsearch:SearchResultCase) {
neosNodes {
// all Documents in the index which are Nodes have a property "index_discriminator" set to "neos_nodes";
// This is in preparation for displaying other kinds of data.
condition = ${searchResultDocument.property('index_discriminator') == 'neos_nodes'}
renderer.@context.node = ${searchResultDocument.loadNode()}
renderer = afx`
<Neos.Neos:NodeLink node={node} />
`
// If you want to see the full Search Response hit, you can include the following
// snippet in the renderer above:
// <Neos.Fusion:Debug result={searchResultDocument.fullSearchHit} />
}
}
查询API
简单示例作为 Eel 表达式
Elasticsearch.createRequest(site)
.query(
Elasticsearch.createNeosFulltextQuery(site)
.fulltext(request.arguments.q)
)
- 如果您想搜索 Neos 节点,我们需要将 上下文节点 作为第一个参数传递给
Elasticsearch.createRequest()
。- 这样,就可以自动搜索正确的索引(在当前语言中)。
- 您可以对单个文档调用
searchResultDocument.loadNode()
。
Elasticsearch.createNeosFulltextQuery
也需要一个 上下文节点,它指定了我们想要搜索的节点树部分。
存在一个查询 API 用于更复杂的情况,即您可以进行以下操作:
Elasticsearch.createRequest(site)
.query(
Elasticsearch.createNeosFulltextQuery(site)
.fulltext(request.arguments.q)
// only the results to documents where myKey = myValue
.filter(Elasticsearch.createTermQuery("myKey", "myValue"))
)
通过多个索引进行搜索的更复杂查询可能看起来像这样:
Elasticsearch.createRequest(site, ['index2', 'index3')
.query(
Elasticsearch.createBooleanQuery()
.should(
Elasticsearch.createNeosFulltextQuery(site)
.fulltext(request.arguments.q)
.filter(Elasticsearch.createTermQuery("index_discriminator", "neos_nodes"))
)
.should(
// add query for index2 here
)
)
我们建议通过自定义 Eel 辅助程序构建更复杂的查询;直接调用此包的查询构建器。
聚合和分面
实现分面搜索比最初看起来要困难得多 - 因此,让我们首先构建查询应该如何工作的心理模型。
分面通常看起来像这样:
[ Global Search Input Field ] <-- global info
Categories
- News
- FAQ Entries
- ...
Products
- Product 1
- Product 2 (chosen)
[Result Listing]
全局搜索输入字段是最简单的,因为它同时影响上面的分面(类别和产品)和结果列表。
对于分面,事情要复杂一些。为了 计算 分面值(即“类别”标题下显示的内容),需要执行一个 聚合查询。在这个查询中,我们需要考虑全局搜索字段以及所有其他分面的选择(但不是我们自己的分面)。
对于结果列表,我们需要考虑全局搜索以及所有分面的选择。
在 Elasticsearch 中建模这一点,我们建议使用多个查询:一个用于每个分面;一个用于渲染结果列表。
以下是对上述模板所做的修改列表:
@@ -1,7 +1,24 @@ prototype(My.Package:Search) < prototype(Neos.Fusion:Component) { - // for possibilities on how to build the query, see the next section in the documentation + // this is the base query from the user which should *always* be applied. _elasticsearchBaseQuery = ${Elasticsearch.createNeosFulltextQuery(site).fulltext(request.arguments.q)} - @context.mainSearchRequest = ${Elasticsearch.createRequest(site).query(this._elasticsearchBaseQuery))} + + // register a Terms aggregation with the URL parameter "nodeTypesFilter" + _nodeTypesAggregation = ${Elasticsearch.createTermsAggregation("neos_type", request.arguments.nodeTypesFilter)} + + // This is the main elasticsearch query which determines the search results: + // - this._elasticsearchBaseQuery is applied + // - this._nodeTypesAggregation is applied as well (if the user chose a facet value) + // <-- if you add additional aggregations, you need to add them here to this list. + @context.mainSearchRequest = ${Elasticsearch.createRequest(site).query(Elasticsearch.createBooleanQuery().must(this._elasticsearchBaseQuery).filter(this._nodeTypesAggregation))} + + // The Request is for displaying the Node Types aggregation (faceted search). + // + // For faceted search to work properly, we need to add all OTHER query parts as filter; so NOT ourselves. + // This means, for the `.aggregation()` part, we take the aggregation itself. + // For the `.filter()` part, we add: + // - this._elasticsearchBaseQuery to ensure the entered query string by the user is taken into account + // <-- if you add additional aggregations, you need to add them here to the list. + @context.nodeTypesFacet = ${Elasticsearch.createAggregationRequest(site).aggregation(this._nodeTypesAggregation).filter(this._elasticsearchBaseQuery).execute()} // Search Result Display is controlled through Flowpack.Listable searchResults = Flowpack.Listable:PaginatedCollection { @@ -12,6 +29,23 @@ // for the PaginatedCollection. @cache.mode = "embed" } + + nodeTypesFacet = Neos.Fusion:Component { + // the nodeTypesFacet is a "Terms" aggregation... + // ...so we can access nodeTypesFacet.buckets. + // To build a link to the facet, we use Neos.Neos:NodeLink with two additions: + // - addQueryString must be set to TRUE, to keep the search query and potentially other facets. + // - to build the arguments, we need to set `nodeTypesFilter` to the current bucket key (or to null in case we want to clear the facet) + renderer = afx` + <ul> + <Neos.Fusion:Loop items={nodeTypesFacet.buckets} itemName="bucket"> + <li><Neos.Neos:NodeLink node={documentNode} addQueryString={true} arguments={{nodeTypesFilter: bucket.key}}>{bucket.key}</Neos.Neos:NodeLink> {bucket.doc_count} <span @if.isTrue={bucket.key == nodeTypesFacet.selectedValue}>(selected)</span></li> + </Neos.Fusion:Loop> + </ul> + <Neos.Neos:NodeLink @if.isTrue={nodeTypesFacet.selectedValue} node={documentNode} addQueryString={true} arguments={{nodeTypesFilter: null}}>CLEAR FACET</Neos.Neos:NodeLink> + ` + } + renderer = afx` <form action="." method="get"> <input name="q" value={request.arguments.q}/> @@ -22,6 +56,8 @@ </div> <p>Showing {mainSearchRequest.execute().count()} of {mainSearchRequest.execute().total()} results</p> + {props.nodeTypesFacet} + {props.searchResults} </form> ` @@ -32,6 +68,8 @@ // The parameter "q" should be included in this pagination prototype(Flowpack.Listable:PaginationParameters) { q = ${request.arguments.q} + // <-- if you add additional aggregations, you need to add the parameter names here + nodeTypesFilter = ${request.arguments.nodeTypesFilter} } // We configure the cache mode "dynamic" here. @@ -41,7 +79,8 @@ node = ${node} type = 'searchForm' } - entryDiscriminator = ${request.arguments.q + '-' + request.arguments.currentPage} + // <-- if you add additional aggregations, you need to add the parameter names to the entryDiscriminator + entryDiscriminator = ${request.arguments.q + '-' + request.arguments.currentPage + '-' + request.arguments.nodeTypesFilter} context { 1 = 'node' 2 = 'documentNode'
您也可以复制/粘贴整个文件。
查看分面搜索示例。
prototype(My.Package:Search) < prototype(Neos.Fusion:Component) {
// this is the base query from the user which should *always* be applied.
_elasticsearchBaseQuery = ${Elasticsearch.createNeosFulltextQuery(site).fulltext(request.arguments.q)}
// register a Terms aggregation with the URL parameter "nodeTypesFilter"
_nodeTypesAggregation = ${Elasticsearch.createTermsAggregation("neos_type", request.arguments.nodeTypesFilter)}
// This is the main elasticsearch query which determines the search results:
// - this._elasticsearchBaseQuery is applied
// - this._nodeTypesAggregation is applied as well (if the user chose a facet value)
// <-- if you add additional aggregations, you need to add them here to this list.
@context.mainSearchRequest = ${Elasticsearch.createRequest(site).query(Elasticsearch.createBooleanQuery().must(this._elasticsearchBaseQuery).filter(this._nodeTypesAggregation))}
// The Request is for displaying the Node Types aggregation (faceted search).
//
// For faceted search to work properly, we need to add all OTHER query parts as filter; so NOT ourselves.
// This means, for the `.aggregation()` part, we take the aggregation itself.
// For the `.filter()` part, we add:
// - this._elasticsearchBaseQuery to ensure the entered query string by the user is taken into account
// <-- if you add additional aggregations, you need to add them here to the list.
@context.nodeTypesFacet = ${Elasticsearch.createAggregationRequest(site).aggregation(this._nodeTypesAggregation).filter(this._elasticsearchBaseQuery).execute()}
// Search Result Display is controlled through Flowpack.Listable
searchResults = Flowpack.Listable:PaginatedCollection {
collection = ${mainSearchRequest}
itemsPerPage = 12
// we use cache mode "dynamic" for the full Search component; so we do not need an additional cache entry
// for the PaginatedCollection.
@cache.mode = "embed"
}
nodeTypesFacet = Neos.Fusion:Component {
// the nodeTypesFacet is a "Terms" aggregation...
// ...so we can access nodeTypesFacet.buckets.
// To build a link to the facet, we use Neos.Neos:NodeLink with two additions:
// - addQueryString must be set to TRUE, to keep the search query and potentially other facets.
// - to build the arguments, we need to set `nodeTypesFilter` to the current bucket key (or to null in case we want to clear the facet)
renderer = afx`
<ul>
<Neos.Fusion:Loop items={nodeTypesFacet.buckets} itemName="bucket">
<li><Neos.Neos:NodeLink node={documentNode} addQueryString={true} arguments={{nodeTypesFilter: bucket.key}}>{bucket.key}</Neos.Neos:NodeLink> {bucket.doc_count} <span @if.isTrue={bucket.key == nodeTypesFacet.selectedValue}>(selected)</span></li>
</Neos.Fusion:Loop>
</ul>
<Neos.Neos:NodeLink @if.isTrue={nodeTypesFacet.selectedValue} node={documentNode} addQueryString={true} arguments={{nodeTypesFilter: null}}>CLEAR FACET</Neos.Neos:NodeLink>
`
}
renderer = afx`
<form action="." method="get">
<input name="q" value={request.arguments.q}/>
<button type="submit">Search</button>
<div @if.isError={mainSearchRequest.execute().error}>
There was an error executing the search request. Please try again in a few minutes.
</div>
<p>Showing {mainSearchRequest.execute().count()} of {mainSearchRequest.execute().total()} results</p>
{props.nodeTypesFacet}
{props.searchResults}
</form>
`
// If you want to see the full request going to Elasticsearch, you can include
// the following snippet in the renderer above:
// <Neos.Fusion:Debug v={Json.stringify(mainSearchRequest.requestForDebugging())} />
// The parameter "q" should be included in this pagination
prototype(Flowpack.Listable:PaginationParameters) {
q = ${request.arguments.q}
// <-- if you add additional aggregations, you need to add the parameter names here
nodeTypesFilter = ${request.arguments.nodeTypesFilter}
}
// We configure the cache mode "dynamic" here.
@cache {
mode = 'dynamic'
entryIdentifier {
node = ${node}
type = 'searchForm'
}
// <-- if you add additional aggregations, you need to add the parameter names to the entryDiscriminator
entryDiscriminator = ${request.arguments.q + '-' + request.arguments.currentPage + '-' + request.arguments.nodeTypesFilter}
context {
1 = 'node'
2 = 'documentNode'
3 = 'site'
}
entryTags {
1 = ${Neos.Caching.nodeTag(node)}
}
}
}
// The result display is done here.
// In the context, you'll find an object `searchResultDocument` which is of type
// Sandstorm\LightweightElasticsearch\Query\Result\SearchResultDocument.
prototype(Sandstorm.LightweightElasticsearch:SearchResultCase) {
neosNodes {
// all Documents in the index which are Nodes have a property "index_discriminator" set to "neos_nodes";
// This is in preparation for displaying other kinds of data.
condition = ${searchResultDocument.property('index_discriminator') == 'neos_nodes'}
renderer.@context.node = ${searchResultDocument.loadNode()}
renderer = afx`
<Neos.Neos:NodeLink node={node} />
`
// If you want to see the full Search Response hit, you can include the following
// snippet in the renderer above:
// <Neos.Fusion:Debug result={searchResultDocument.fullSearchHit} />
}
}
结果高亮
结果高亮是通过使用Elasticsearch的高亮API实现的。
要启用它,您需要更改以下部分
-
要使用默认高亮,请将
.highlight(Elasticsearch.createNeosFulltextHighlight())
部分添加到您的主Elasticsearch查询中。 -
此外,您可以为每个结果调用getter
searchResultDocument.processedHighlights
,它包含高亮摘要,您可以简单地像这样连接它们Array.join(searchResultDocument.processedHighlights, '…')
下面是一个完整的示例
@@ -9,7 +9,7 @@ // - this._elasticsearchBaseQuery is applied // - this._nodeTypesAggregation is applied as well (if the user chose a facet value) // <-- if you add additional aggregations, you need to add them here to this list. - @context.mainSearchRequest = ${Elasticsearch.createRequest(site).query(Elasticsearch.createBooleanQuery().must(this._elasticsearchBaseQuery).filter(this._nodeTypesAggregation))} + @context.mainSearchRequest = ${Elasticsearch.createRequest(site).query(Elasticsearch.createBooleanQuery().must(this._elasticsearchBaseQuery).filter(this._nodeTypesAggregation)).highlight(Elasticsearch.createNeosFulltextHighlight())} // The Request is for displaying the Node Types aggregation (faceted search). // @@ -102,7 +102,9 @@ condition = ${searchResultDocument.property('index_discriminator') == 'neos_nodes'} renderer.@context.node = ${searchResultDocument.loadNode()} renderer = afx` - <Neos.Neos:NodeLink node={node} /> + <Neos.Neos:NodeLink node={node}> + {Array.join(searchResultDocument.processedHighlights, '…')} + </Neos.Neos:NodeLink> ` // If you want to see the full Search Response hit, you can include the following // snippet in the renderer above:
您也可以复制/粘贴整个文件。
查看分面+高亮搜索示例
prototype(My.Package:Search) < prototype(Neos.Fusion:Component) {
// this is the base query from the user which should *always* be applied.
_elasticsearchBaseQuery = ${Elasticsearch.createNeosFulltextQuery(site).fulltext(request.arguments.q)}
// register a Terms aggregation with the URL parameter "nodeTypesFilter"
_nodeTypesAggregation = ${Elasticsearch.createTermsAggregation("neos_type", request.arguments.nodeTypesFilter)}
// This is the main elasticsearch query which determines the search results:
// - this._elasticsearchBaseQuery is applied
// - this._nodeTypesAggregation is applied as well (if the user chose a facet value)
// <-- if you add additional aggregations, you need to add them here to this list.
@context.mainSearchRequest = ${Elasticsearch.createRequest(site).query(Elasticsearch.createBooleanQuery().must(this._elasticsearchBaseQuery).filter(this._nodeTypesAggregation)).highlight(Elasticsearch.createNeosFulltextHighlight())}
// The Request is for displaying the Node Types aggregation (faceted search).
//
// For faceted search to work properly, we need to add all OTHER query parts as filter; so NOT ourselves.
// This means, for the `.aggregation()` part, we take the aggregation itself.
// For the `.filter()` part, we add:
// - this._elasticsearchBaseQuery to ensure the entered query string by the user is taken into account
// <-- if you add additional aggregations, you need to add them here to the list.
@context.nodeTypesFacet = ${Elasticsearch.createAggregationRequest(site).aggregation(this._nodeTypesAggregation).filter(this._elasticsearchBaseQuery).execute()}
// Search Result Display is controlled through Flowpack.Listable
searchResults = Flowpack.Listable:PaginatedCollection {
collection = ${mainSearchRequest}
itemsPerPage = 12
// we use cache mode "dynamic" for the full Search component; so we do not need an additional cache entry
// for the PaginatedCollection.
@cache.mode = "embed"
}
nodeTypesFacet = Neos.Fusion:Component {
// the nodeTypesFacet is a "Terms" aggregation...
// ...so we can access nodeTypesFacet.buckets.
// To build a link to the facet, we use Neos.Neos:NodeLink with two additions:
// - addQueryString must be set to TRUE, to keep the search query and potentially other facets.
// - to build the arguments, we need to set `nodeTypesFilter` to the current bucket key (or to null in case we want to clear the facet)
renderer = afx`
<ul>
<Neos.Fusion:Loop items={nodeTypesFacet.buckets} itemName="bucket">
<li><Neos.Neos:NodeLink node={documentNode} addQueryString={true} arguments={{nodeTypesFilter: bucket.key}}>{bucket.key}</Neos.Neos:NodeLink> {bucket.doc_count} <span @if.isTrue={bucket.key == nodeTypesFacet.selectedValue}>(selected)</span></li>
</Neos.Fusion:Loop>
</ul>
<Neos.Neos:NodeLink @if.isTrue={nodeTypesFacet.selectedValue} node={documentNode} addQueryString={true} arguments={{nodeTypesFilter: null}}>CLEAR FACET</Neos.Neos:NodeLink>
`
}
renderer = afx`
<form action="." method="get">
<input name="q" value={request.arguments.q}/>
<button type="submit">Search</button>
<div @if.isError={mainSearchRequest.execute().error}>
There was an error executing the search request. Please try again in a few minutes.
</div>
<p>Showing {mainSearchRequest.execute().count()} of {mainSearchRequest.execute().total()} results</p>
{props.nodeTypesFacet}
{props.searchResults}
</form>
`
// If you want to see the full request going to Elasticsearch, you can include
// the following snippet in the renderer above:
// <Neos.Fusion:Debug v={Json.stringify(mainSearchRequest.requestForDebugging())} />
// The parameter "q" should be included in this pagination
prototype(Flowpack.Listable:PaginationParameters) {
q = ${request.arguments.q}
// <-- if you add additional aggregations, you need to add the parameter names here
nodeTypesFilter = ${request.arguments.nodeTypesFilter}
}
// We configure the cache mode "dynamic" here.
@cache {
mode = 'dynamic'
entryIdentifier {
node = ${node}
type = 'searchForm'
}
// <-- if you add additional aggregations, you need to add the parameter names to the entryDiscriminator
entryDiscriminator = ${request.arguments.q + '-' + request.arguments.currentPage + '-' + request.arguments.nodeTypesFilter}
context {
1 = 'node'
2 = 'documentNode'
3 = 'site'
}
entryTags {
1 = ${Neos.Caching.nodeTag(node)}
}
}
}
// The result display is done here.
// In the context, you'll find an object `searchResultDocument` which is of type
// Sandstorm\LightweightElasticsearch\Query\Result\SearchResultDocument.
prototype(Sandstorm.LightweightElasticsearch:SearchResultCase) {
neosNodes {
// all Documents in the index which are Nodes have a property "index_discriminator" set to "neos_nodes";
// This is in preparation for displaying other kinds of data.
condition = ${searchResultDocument.property('index_discriminator') == 'neos_nodes'}
renderer.@context.node = ${searchResultDocument.loadNode()}
renderer = afx`
<Neos.Neos:NodeLink node={node}>
{Array.join(searchResultDocument.processedHighlights, '…')}
</Neos.Neos:NodeLink>
`
// If you want to see the full Search Response hit, you can include the following
// snippet in the renderer above:
// <Neos.Fusion:Debug result={searchResultDocument.fullSearchHit} />
}
}
索引其他数据
我们建议将index_discriminator
设置为不同数据源的不同值,以便能够正确识别不同的来源。
您可以使用CustomIndexer
作为索引的基础,如下所示
$indexer = CustomIndexer::create('faq'); $indexer->createIndexWithMapping(['properties' => [ 'faqEntryTitle' => [ 'type' => 'text' ] ]]); $indexer->index([ 'faqEntryTitle' => 'FAQ Dresden' ]); // index other documents here $indexer->finalizeAndSwitchAlias(); // Optionally for cleanup $indexer->removeObsoleteIndices();
为了您的方便,下面可以复制/粘贴完整的CommandController
自定义索引的Command Controller
<?php
namespace Your\Package\Command;
use Neos\Flow\Cli\CommandController;
use Sandstorm\LightweightElasticsearch\CustomIndexing\CustomIndexer;
class CustomIndexCommandController extends CommandController
{
public function indexCommand()
{
$indexer = CustomIndexer::create('faq');
$indexer->createIndexWithMapping(['properties' => [
'faqEntryTitle' => [
'type' => 'text'
]
]]);
$indexer->index([
'faqEntryTitle' => 'FAQ Dresden'
]);
$indexer->index([
'faqEntryTitle' => 'FAQ Berlin'
]);
$indexer->finalizeAndSwitchAlias();
}
public function cleanupCommand()
{
$indexer = CustomIndexer::create('faq');
$removedIndices = $indexer->removeObsoleteIndices();
foreach ($removedIndices as $index) {
$this->outputLine('Removed ' . $index);
}
}
}
查看下一节以查询其他数据源
查询其他数据
查询其他数据源需要调整三个部分
- 调整
Elasticsearch.createRequest()
和Elasticsearch.createAggregationRequest()
调用 - 构建并使用您的自定义数据的全文查询
- 自定义结果渲染。
现在我们将一步步进行。
调整Elasticsearch.createRequest()
和Elasticsearch.createAggregationRequest()
在这里,您需要将其他索引作为第二个参数包含在内;例如,Elasticsearch.createRequest(site, ['faq'])
是一个有效的调用。
构建全文查询
我们建议您为在自定义数据中进行全文搜索构建自定义Eel辅助器,例如通过过滤index_discriminator
并使用如下所示的simple_query_string
查询
return BooleanQueryBuilder::create() ->filter(TermQueryBuilder::create('index_discriminator', 'faq')) ->must( SimpleQueryStringBuilder::create($query ?? '')->fields([ 'faqEntryTitle^5', ]) );
作为一个例子,您还可以查看完整的Eel辅助器
自定义数据的全文查询Eel辅助器
<?php
declare(strict_types=1);
namespace My\Package\Eel;
use Neos\Eel\ProtectedContextAwareInterface;
use Sandstorm\LightweightElasticsearch\Query\Query\BooleanQueryBuilder;
use Sandstorm\LightweightElasticsearch\Query\Query\SearchQueryBuilderInterface;
use Sandstorm\LightweightElasticsearch\Query\Query\SimpleQueryStringBuilder;
use Sandstorm\LightweightElasticsearch\Query\Query\TermQueryBuilder;
class MyQueries implements ProtectedContextAwareInterface
{
public function faqQuery(string $query): SearchQueryBuilderInterface
{
return BooleanQueryBuilder::create()
->filter(TermQueryBuilder::create('index_discriminator', 'faq'))
->must(
SimpleQueryStringBuilder::create($query ?? '')->fields([
'faqEntryTitle^5',
])
);
}
public function allowsCallOfMethod($methodName)
{
return true;
}
}
**请记住,像往常一样在Settings.yaml
中注册Eel辅助器
Neos: Fusion: defaultContext: MyQueries: My\Package\Eel\MyQueries
使用全文查询
要同时使用Neos和您的自定义全文查询,这两个查询应使用Terms
查询中的should
子句组合;因此,这是一个“或”查询组合
Elasticsearch.createBooleanQuery()
.should(Elasticsearch.createNeosFulltextQuery(site).fulltext(request.arguments.q))
.should(MyQueries.faqQuery(request.arguments.q))}
调整结果渲染
通过向prototype(Sandstorm.LightweightElasticsearch:SearchResultCase)
添加条件分支,您可以自定义结果渲染
prototype(Sandstorm.LightweightElasticsearch:SearchResultCase) {
faqEntries {
condition = ${searchResultDocument.property('index_discriminator') == 'faq'}
renderer = afx`
{searchResultDocument.properties.faqEntryTitle}
`
}
}
把它们放在一起
查看以下diff,或下面的完整源代码
@@ -1,26 +1,15 @@ prototype(My.Package:Search) < prototype(Neos.Fusion:Component) { - // this is the base query from the user which should *always* be applied. - _elasticsearchBaseQuery = ${Elasticsearch.createNeosFulltextQuery(site).fulltext(request.arguments.q)} + // for possibilities on how to build the query, see the next section in the documentation + _elasticsearchBaseQuery = ${Elasticsearch.createBooleanQuery().should(Elasticsearch.createNeosFulltextQuery(site).fulltext(request.arguments.q)).should(MyQueries.faqQuery(request.arguments.q))} - // register a Terms aggregation with the URL parameter "nodeTypesFilter" + // register a Terms aggregation with the URL parameter "nodeTypesFilter". + // we also need to pass in the request, so that the aggregation can extract the currently selected value. _nodeTypesAggregation = ${Elasticsearch.createTermsAggregation("neos_type", request.arguments.nodeTypesFilter)} - // This is the main elasticsearch query which determines the search results: - // - this._elasticsearchBaseQuery is applied - // - this._nodeTypesAggregation is applied as well (if the user chose a facet value) - // <-- if you add additional aggregations, you need to add them here to this list. - @context.mainSearchRequest = ${Elasticsearch.createRequest(site).query(Elasticsearch.createBooleanQuery().must(this._elasticsearchBaseQuery).filter(this._nodeTypesAggregation))} - - // The Request is for displaying the Node Types aggregation (faceted search). - // - // For faceted search to work properly, we need to add all OTHER query parts as filter; so NOT ourselves. - // This means, for the `.aggregation()` part, we take the aggregation itself. - // For the `.filter()` part, we add: - // - this._elasticsearchBaseQuery to ensure the entered query string by the user is taken into account - // <-- if you add additional aggregations, you need to add them here to the list. - @context.nodeTypesFacet = ${Elasticsearch.createAggregationRequest(site).aggregation(this._nodeTypesAggregation).filter(this._elasticsearchBaseQuery).execute()} - - // Search Result Display is controlled through Flowpack.Listable + // this is the main elasticsearch query which determines the search results - here, we also apply any restrictions imposed + // by the _nodeTypesAggregation + @context.mainSearchRequest = ${Elasticsearch.createRequest(site, ['faq']).query(Elasticsearch.createBooleanQuery().must(this._elasticsearchBaseQuery).filter(this._nodeTypesAggregation))} + @context.nodeTypesFacet = ${Elasticsearch.createAggregationRequest(site, ['faq']).aggregation(this._nodeTypesAggregation).filter(this._elasticsearchBaseQuery).execute()} searchResults = Flowpack.Listable:PaginatedCollection { collection = ${mainSearchRequest} itemsPerPage = 12 @@ -35,7 +24,7 @@ // ...so we can access nodeTypesFacet.buckets. // To build a link to the facet, we use Neos.Neos:NodeLink with two additions: // - addQueryString must be set to TRUE, to keep the search query and potentially other facets. - // - to build the arguments, we need to set `nodeTypesFilter` to the current bucket key (or to null in case we want to clear the facet) + // - to build the arguments, each aggregation result type (e.g. TermsAggregationResult) has a specific method with the required arguments. renderer = afx` <ul> <Neos.Fusion:Loop items={nodeTypesFacet.buckets} itemName="bucket"> @@ -50,7 +39,6 @@ <form action="." method="get"> <input name="q" value={request.arguments.q}/> <button type="submit">Search</button> - <div @if.isError={mainSearchRequest.execute().error}> There was an error executing the search request. Please try again in a few minutes. </div> @@ -68,8 +56,7 @@ // The parameter "q" should be included in this pagination prototype(Flowpack.Listable:PaginationParameters) { q = ${request.arguments.q} - // <-- if you add additional aggregations, you need to add the parameter names here - nodeTypesFilter = ${request.arguments.nodeTypesFilter} + nodeTypes = ${request.arguments.nodeTypesFilter} } // We configure the cache mode "dynamic" here. @@ -79,7 +66,6 @@ node = ${node} type = 'searchForm' } - // <-- if you add additional aggregations, you need to add the parameter names to the entryDiscriminator entryDiscriminator = ${request.arguments.q + '-' + request.arguments.currentPage + '-' + request.arguments.nodeTypesFilter} context { 1 = 'node' @@ -96,6 +82,12 @@ // In the context, you'll find an object `searchResultDocument` which is of type // Sandstorm\LightweightElasticsearch\Query\Result\SearchResultDocument. prototype(Sandstorm.LightweightElasticsearch:SearchResultCase) { + faqEntries { + condition = ${searchResultDocument.property('index_discriminator') == 'faq'} + renderer = afx` + {searchResultDocument.properties.faqEntryTitle} + ` + } neosNodes { // all Documents in the index which are Nodes have a property "index_discriminator" set to "neos_nodes"; // This is in preparation for displaying other kinds of data.
您也可以复制/粘贴整个文件。
查看分面搜索示例。
prototype(My.Package:Search) < prototype(Neos.Fusion:Component) {
// for possibilities on how to build the query, see the next section in the documentation
_elasticsearchBaseQuery = ${Elasticsearch.createBooleanQuery().should(Elasticsearch.createNeosFulltextQuery(site).fulltext(request.arguments.q)).should(MyQueries.faqQuery(request.arguments.q))}
// register a Terms aggregation with the URL parameter "nodeTypesFilter".
// we also need to pass in the request, so that the aggregation can extract the currently selected value.
_nodeTypesAggregation = ${Elasticsearch.createTermsAggregation("neos_type", request.arguments.nodeTypesFilter)}
// this is the main elasticsearch query which determines the search results - here, we also apply any restrictions imposed
// by the _nodeTypesAggregation
@context.mainSearchRequest = ${Elasticsearch.createRequest(site, ['faq']).query(Elasticsearch.createBooleanQuery().must(this._elasticsearchBaseQuery).filter(this._nodeTypesAggregation))}
@context.nodeTypesFacet = ${Elasticsearch.createAggregationRequest(site, ['faq']).aggregation(this._nodeTypesAggregation).filter(this._elasticsearchBaseQuery).execute()}
searchResults = Flowpack.Listable:PaginatedCollection {
collection = ${mainSearchRequest}
itemsPerPage = 12
// we use cache mode "dynamic" for the full Search component; so we do not need an additional cache entry
// for the PaginatedCollection.
@cache.mode = "embed"
}
nodeTypesFacet = Neos.Fusion:Component {
// the nodeTypesFacet is a "Terms" aggregation...
// ...so we can access nodeTypesFacet.buckets.
// To build a link to the facet, we use Neos.Neos:NodeLink with two additions:
// - addQueryString must be set to TRUE, to keep the search query and potentially other facets.
// - to build the arguments, each aggregation result type (e.g. TermsAggregationResult) has a specific method with the required arguments.
renderer = afx`
<ul>
<Neos.Fusion:Loop items={nodeTypesFacet.buckets} itemName="bucket">
<li><Neos.Neos:NodeLink node={documentNode} addQueryString={true} arguments={{nodeTypesFilter: bucket.key}}>{bucket.key}</Neos.Neos:NodeLink> {bucket.doc_count} <span @if.isTrue={bucket.key == nodeTypesFacet.selectedValue}>(selected)</span></li>
</Neos.Fusion:Loop>
</ul>
<Neos.Neos:NodeLink @if.isTrue={nodeTypesFacet.selectedValue} node={documentNode} addQueryString={true} arguments={{nodeTypesFilter: null}}>CLEAR FACET</Neos.Neos:NodeLink>
`
}
renderer = afx`
<form action="." method="get">
<input name="q" value={request.arguments.q}/>
<button type="submit">Search</button>
<div @if.isError={mainSearchRequest.execute().error}>
There was an error executing the search request. Please try again in a few minutes.
</div>
<p>Showing {mainSearchRequest.execute().count()} of {mainSearchRequest.execute().total()} results</p>
{props.nodeTypesFacet}
{props.searchResults}
</form>
`
// If you want to see the full request going to Elasticsearch, you can include
// the following snippet in the renderer above:
// <Neos.Fusion:Debug v={Json.stringify(mainSearchRequest.requestForDebugging())} />
// The parameter "q" should be included in this pagination
prototype(Flowpack.Listable:PaginationParameters) {
q = ${request.arguments.q}
nodeTypes = ${request.arguments.nodeTypesFilter}
}
// We configure the cache mode "dynamic" here.
@cache {
mode = 'dynamic'
entryIdentifier {
node = ${node}
type = 'searchForm'
}
entryDiscriminator = ${request.arguments.q + '-' + request.arguments.currentPage + '-' + request.arguments.nodeTypesFilter}
context {
1 = 'node'
2 = 'documentNode'
3 = 'site'
}
entryTags {
1 = ${Neos.Caching.nodeTag(node)}
}
}
}
// The result display is done here.
// In the context, you'll find an object `searchResultDocument` which is of type
// Sandstorm\LightweightElasticsearch\Query\Result\SearchResultDocument.
prototype(Sandstorm.LightweightElasticsearch:SearchResultCase) {
faqEntries {
condition = ${searchResultDocument.property('index_discriminator') == 'faq'}
renderer = afx`
{searchResultDocument.properties.faqEntryTitle}
`
}
neosNodes {
// all Documents in the index which are Nodes have a property "index_discriminator" set to "neos_nodes";
// This is in preparation for displaying other kinds of data.
condition = ${searchResultDocument.property('index_discriminator') == 'neos_nodes'}
renderer.@context.node = ${searchResultDocument.loadNode()}
renderer = afx`
<Neos.Neos:NodeLink node={node} />
`
// If you want to see the full Search Response hit, you can include the following
// snippet in the renderer above:
// <Neos.Fusion:Debug result={searchResultDocument.fullSearchHit} />
}
}
调试Elasticsearch查询
-
将
.log("!!!FOO")
添加到您的Eel ElasticSearch查询中 -
检查System_Development日志文件以获取完整查询并将其保存到文件(req.json)中
-
我们建议使用https://httpie.io/(可以使用
brew install httpie
安装)来进行请求http 127.0.0.1:9200/_cat/aliases cat req.json | http 127.0.0.1:9200/neoscr,foo/_search
开发
我们乐于接受pull请求或贡献者 :-)
修改此README
首先更改Documentation/README_template.php;然后运行php Documentation/build_readme.php
。
许可证
MIT