我正在构建一个搜索功能,允许用户按特定字段进行过滤。 DOI 就是其中之一。按 DOI 过滤应仅返回完全匹配的结果。
我正在尝试索引以下对象:
public class PaperElasticSearchModel
{
public string Title { get; set; }
public string Abstract { get; set; }
public string Keywords { get; set; }
public string DOI { get; set; }
public int Year { get; set; }
public string[] Author { get; set; }
}
字段
DOI
的格式与此类似:10.1523/JNEUROSCI.18-04-01622.1998
。
以下是索引的设置
Client.Indices.Create(index, pub => pub
.Settings(s => s
.Analysis(descriptor => descriptor
.Analyzers(analyzer => analyzer
.Custom("custom_analyzer", ca => ca
.Filters("lowercase", "stop", "classic", "word_delimiter")
)
)
.TokenFilters(bases => bases
.EdgeNGram("custom_analyzer", td => td
.MinGram(2)
.MaxGram(25))
)
)
.Setting(UpdatableIndexSettings.MaxNGramDiff, 23)
)
.Map<PubScreenSearch>(m => m
.AutoMap()
.Properties(p => p
.Text(t => t
.Name(f => f.Author)
.Analyzer("custom_analyzer"))
.Text(t => t
.Name(f => f.Keywords)
.Analyzer("custom_analyzer"))
.Text(t => t
.Name(f => f.Title)
.Analyzer("custom_analyzer"))
.Keyword(t => t
.Name(f => f.DOI)
)
)));
我应该使用什么分析器,这样如果我按 DOI 搜索,它会返回完全匹配的结果?
我尝试使用
Pattern
分析仪,但它不起作用:
.Text(t => t
.Name(f => f.DOI)
.Analyzer(new PatternAnalyzer().Pattern = @"(.(\\/|\\?)*)\\w+")
让我澄清一下 elasticsearch 分析过程以及它在搜索过程中的
term and match query
和索引过程中的 keyword and text
字段类型如何工作。
官方解释: 匹配查询:返回与提供的文本、数字、日期或布尔值匹配的文档。在匹配之前对提供的文本进行分析。 术语查询:返回在所提供字段中包含确切术语的文档。
text
字段类型。如果您想在 JNEUROSCI
数据中查看 10.1523/JNEUROSCI.18-04-01622.1998
查询的结果,请使用 match + text field type
term query + keyword field type
。注意:由于名称声明的完全匹配会查找完全匹配,因此区分大小写。
以下是一些示例:
#index the test data
PUT test-analyzer/_doc/1
{
"field_name": "10.1523/JNEUROSCI.18-04-01622.1998"
}
#term query with text field type - no hits
GET test-analyzer/_search
{
"query": {
"term": {
"field_name": "JNEUROSCI"
}
}
}
#term query with keyword field type - no hits
GET test-analyzer/_search
{
"query": {
"term": {
"field_name.keyword": "JNEUROSCI"
}
}
}
#match query with text field type - there is a hit
GET test-analyzer/_search
{
"query": {
"match": {
"field_name": "JNEUROSCI"
}
}
}
#match query with keyword field type - no hits
GET test-analyzer/_search
{
"query": {
"match": {
"field_name.keyword": "JNEUROSCI"
}
}
}
#exact match that you're looking for - term query with keyword field type - there is a hit
GET test-analyzer/_search
{
"query": {
"term": {
"field_name.keyword": "10.1523/JNEUROSCI.18-04-01622.1998"
}
}
}