Elasticsearch Nest带有空格的通配符查询



短版本:

我想使用Nest编写一个弹性搜索查询,以获得已索引的完整索引项(在我的情况下,ContentIndexables是我的自定义类型)。该查询受[some string]+*(即string.StartsWith())的术语查询的约束,其中[some stringe]可能包含空格,也可能不包含空格。

这与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",搜索结果为空,而不是我期望返回的两行。

更新:更多信息:我在我的类型ContentIndexeable:上创建了一个索引

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。MY 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进行两个单独的调用(一个用于完整的suggestor,第二个用于术语查询),但理想情况下,如果可能的话,我希望不进行往返。

非常感谢,

这是如何处理Title字段问题的示例。

将您的映射更改为类似(或使用MultiField,但我找不到将字段映射为字符串并同时完成的选项):

client.CreateIndex(indexName, i => i
.AddMapping<ContentIndexable>(m => m
.Properties(
ps => ps
.Completion(c => c.Name("title.completion")
.IndexAnalyzer("standard")
.SearchAnalyzer("standard")
.MaxInputLength(30)
.Payloads()
.PreserveSeparators()
.PreservePositionIncrements())
.String(s => s.Name(x => x.Title).CopyTo("title.completion")))));

SuggestCompletion更改为

var result = client.Search<ContentIndexable>(body => body
.Index(indexName)
.SuggestCompletion("content-suggest" + Guid.NewGuid(),
descriptor => descriptor
.OnField(t => t.Title.Suffix("completion"))
.Text("title")
.Size(10)));

QueryString

var searchResponse = client.Search<ContentIndexable>(body => body
.Index(indexName)
.Query(query => query
.QueryString(
qs => qs
.OnFields(f => f.Title.Suffix("completion"))
.Query("title tha" + "*")
.MinimumShouldMatchPercentage(100))));

这个解决方案的问题是,我们为Title字段存储了两次数据。这就是为什么我之前提到使用MultiField会很好,但我无法使用NEST做到这一点。

希望这能有所帮助。

最新更新