简洁版本:
我想使用 Nest 编写一个弹性搜索查询来获取完整的索引项(ContentIndexables
在我的例子中作为我的自定义类型)已被索引。该查询受 [some string] + * 术语查询的约束(即 String.StartsWith(),其中 [some string] 可能包含也可能不包含空格。
这不同于CompletionSuggester
因为我需要检索完整的对象而不是字符串建议。
到目前为止我尝试过的:
当我查询不带空格的文本时,使用下面的代码返回所需的输出。但是,如果我的搜索词包含空格,则它不会返回预期结果。
以下是我搜索字段的方法:
var searchResults = _client.Search<ContentIndexable>(
body =>
body
.Index(indexName)
.Query(
query =>
query.QueryString(
qs => qs.
OnFields(f => f.Title, f => f.TextContent)
.Query(searchTerm + "*"))));
这是一个演示如何重现问题的单元测试:
indexService.IndexUserItemsSync(testGuid, IndexType.submission, new ContentIndexable
{
ContentId = Guid.NewGuid(),
TextContent = "Some description",
Title = "title"
});
indexService.IndexUserItemsSync(testGuid, IndexType.submission, new ContentIndexable
{
ContentId = Guid.NewGuid(),
TextContent = "Some description",
Title = "title that is long"
});
indexService.IndexUserItemsSync(testGuid, IndexType.submission, new ContentIndexable
{
ContentId = Guid.NewGuid(),
TextContent = "Some description",
Title = "title that likes"
});
indexService.IndexUserItemsSync(testGuid, IndexType.submission, new ContentIndexable
{
ContentId = Guid.NewGuid(),
TextContent = "Some description",
Title = "titlethat"
});
var searchResult = indexService.SearchUserItems(testGuid, IndexType.submission, 10, "title");
Assert.IsNotNull(searchResult);
// this one works
Assert.AreEqual(4, searchResult.Count());
var searchResult2 = indexService.SearchUserItems(testGuid, IndexType.submission, 10, "title that");
Assert.IsNotNull(searchResult2);
// this one does not!!! searchREsult2.Count() evaluates to 0
Assert.AreEqual(2, searchResult2.Count());
正如您所看到的,然后我输入“title that”,搜索结果为空,而不是我期望返回的两行。
Update:更多信息:
我在 ContentIndexable 类型上创建索引:
public class ContentIndexable : IIndexable
{
public Guid ContentId { get; set; }
public string Title { get; set; }
public string TextContent { get; set; }
}
有了这个代码:
_client.CreateIndex(
indexName,
descriptor =>
descriptor.AddMapping<ContentIndexable>(
m => m.Properties(
p => p.Completion(s => s
.Name(n => n.Title)
.IndexAnalyzer("standard")
.SearchAnalyzer("standard")
.MaxInputLength(30)
.Payloads()
.PreserveSeparators()
.PreservePositionIncrements())
.Completion(s => s.Name(n => n.TextContent)
.IndexAnalyzer("standard")
.SearchAnalyzer("standard")
.MaxInputLength(50)
.Payloads()
.PreserveSeparators()
.PreservePositionIncrements())
)));
我什至在索引或查询时尝试转义空白string.Replace(" ", @"\ ")
但这没有帮助。
将搜索类型更改为通配符也没有帮助:
var searchResults = _client.Search<ContentIndexable>(
body =>
body
.Index(indexName)
.Query(
query => query.Wildcard(qd => qd.OnField(f => f.Title).Value(searchTerm + "*"))));
有谁知道我做错了什么?
请注意,我的CompletionSuggester
版本适用于空格,但不幸的是仅返回字符串。我需要得到完整的项目为了获取ContentId
。我的 CompletionSuggester 实施:
public IEnumerable<string> GetAutoCompleteSuggestions(Guid userId, IndexType indexType, int size, string searchTerm)
{
string indexName = getIndexName(indexType, userId);
var result = _client.Search<ContentIndexable>(
body => body.Index(indexName)
.SuggestCompletion("content-suggest" + Guid.NewGuid(),
descriptor => descriptor
.OnField(t => t.Title)
.Text(searchTerm)
.Size(size)));
if (result.Suggest == null)
{
return new List<string>();
}
return (from suggest in result.Suggest
from value in suggest.Value
from options in value.Options
select options.Text).Take(size);
}
我知道我可以接受建议,获取完整的值(这将产生我期望的两个项目),然后使用我的第一种方法进行完整的术语匹配,但这需要对 ElasticSearch 进行 2 次单独的调用(一次用于完整的建议者)第二个用于术语查询)但理想情况下,如果可能的话,我希望在没有往返的情况下完成此操作。
提前谢谢了,