我对ES相当陌生,并且正在尝试解决以下问题。
我已经在Elasticsearch中使用以下配置创建了索引:
client.Indices.Create(lineItemIndex,
c => c
.Settings(s => s
.Setting("max_ngram_diff", 13)
.Analysis(a => a
.Tokenizers(tf => tf
.NGram("mynGram", td => td
.MaxGram(15).MinGram(2)))
.Analyzers(aa => aa
.Custom("mynGram_analyzer", ca => ca
.Filters(new List<string> {"lowercase"})
.Tokenizer("mynGram")))))
.Map<ElasticSearchLineItem>(m => m
.Properties(ps => ps
.Text(ss => ss
.Name(na => na.LineItemName)
.Fields(ff => ff
.Keyword(k => k
.Name("keyword"))
.Text(tx => tx
.Name("fulltext")
.Analyzer("whitespace")
.Boost(10.0))
.Text(tx => tx
.Name("partial")
.Analyzer("mynGram_analyzer")
.Boost(1.0)))))
.Properties(ps => ps
.Keyword(kw => kw
.Name(na => na.LineItemId)
.Index(false)))
.Properties(ps => ps
.Keyword(kw => kw
.Name(na => na.Id)
.Index(false)))
.Properties(ps => ps
.Text(ss => ss
.Name(na => na.LineItemNumber)
.Fields(ff => ff
.Keyword(k => k
.Name("keyword"))
.Text(tx => tx
.Name("fulltext")
.Analyzer("whitespace")
.Boost(10.0))
.Text(tx => tx
.Name("partial")
.Analyzer("mynGram_analyzer")
.Boost(1.0)))))
.Properties(ps => ps
.Keyword(ss => ss
.Name(na => na.SupplierName)
.Index(false)))
.Properties(ps => ps
.Keyword(ss => ss
.Name(na => na.Unit)
.Index(false)))
.Properties(ps => ps
.Number(ss => ss
.Name(na => na.PriceAmount)
.Type(NumberType.ScaledFloat).ScalingFactor(100)
.Index(false)))
.Properties(ps => ps
.Keyword(ss => ss
.Name(na => na.Currency)
.Index(false)))
.Properties(ps => ps
.Keyword(ss => ss
.Name(na => na.SupplierId)
.Index(false)))
.Properties(ps => ps
.Text(ss => ss
.Name(na => na.ImageUrl)
.Index(false)))
.Properties(ps => ps
.Text(ss => ss
.Name(na => na.SupplierPriceListId)
.Index(false)))));
我的解决方案中有一个用于搜索的搜索框。
但是,我们还假设能够基于
SupplierId
过滤搜索。因此,进行搜索的某人可能会有多个SupplierId
,他们只想查看结果。我尝试创建以下查询:
var esSearch2 = new SearchDescriptor<ElasticSearchLineItem>()
.From(0)
.Take(250)
.Query(q => q
.Bool(b => b
.Must(mu => mu
.MultiMatch(m => m
.Fields(f => f
.Field(ff => ff
.LineItemName.Suffix("fulltext"))
.Field(ff => ff
.LineItemName.Suffix("partial"))
.Field(ff => ff
.LineItemNumber.Suffix("fulltext"))
.Field(ff => ff
.LineItemNumber.Suffix("partial")))
.Query(request.SearchWord)
.Fuzziness(Fuzziness.Auto)
))
.Filter(f => f
.Terms(t => t
.Verbatim()
.Field(p => p
.SupplierId.Suffix("keyword"))
.Terms(request.ListOfFavorites.ToArray())))));
无论
request.ListOfFavorites
是否为空,都不会返回任何内容。但是,如果我删除过滤器,它将正确返回结果。我想我缺少了一些东西,或者我的订单混乱了。有人可以在这里帮忙吗?
注意:我使用的是ES 7.5.1和NEST 7.5.1
编辑:
我对索引进行了更改,并从我的SupplierId字段中删除了
Index(false)
。这是更新后在kibana中显示的映射
{
"mapping": {
"properties": {
"currency": {
"type": "keyword",
"index": false
},
"id": {
"type": "keyword",
"index": false
},
"imageUrl": {
"type": "text",
"index": false
},
"lineItemId": {
"type": "keyword",
"index": false
},
"lineItemName": {
"type": "text",
"fields": {
"fulltext": {
"type": "text",
"boost": 10,
"analyzer": "whitespace"
},
"keyword": {
"type": "keyword"
},
"partial": {
"type": "text",
"analyzer": "mynGram_analyzer"
}
}
},
"lineItemNumber": {
"type": "text",
"fields": {
"fulltext": {
"type": "text",
"boost": 10,
"analyzer": "whitespace"
},
"keyword": {
"type": "keyword"
},
"partial": {
"type": "text",
"analyzer": "mynGram_analyzer"
}
}
},
"priceAmount": {
"type": "scaled_float",
"index": false,
"scaling_factor": 100
},
"supplierId": {
"type": "keyword"
},
"supplierName": {
"type": "keyword",
"index": false
},
"supplierPriceListId": {
"type": "text",
"index": false
},
"unit": {
"type": "keyword",
"index": false
}
}
}
}
最佳答案
在映射中,您将SupplierId
指定为excluded from the index,因此将无法对其进行搜索。
.Properties(ps => ps
.Keyword(ss => ss
.Name(na => na.SupplierId)
.Index(false)))
另外,您无需为字段指定后缀,因为它没有定义为多字段,因此只需
.Filter(f => f
.Terms(t => t
.Verbatim()
.Field(p => p.SupplierId)
.Terms(request.ListOfFavorites.ToArray())))));
足够。
希望能有所帮助。