這聽起來像你可能沒有明確提出的映射類型到索引之前索引你的文檔,因此Elasticsearch根據所見文檔中字段的默認映射推斷了映射。作爲一個例子,給出以下類型
[ElasticType(Name = "importFile")]
public class ImportFile
{
[ElasticProperty(Store = false, Index = FieldIndexOption.NotAnalyzed)]
public string FileName { get; set; }
[ElasticProperty(Store = false, Index = FieldIndexOption.NotAnalyzed)]
public string GroupId { get; set; }
[ElasticProperty(Store = true, Index = FieldIndexOption.Analyzed)]
public string FilePath { get; set; }
}
如果我們的索引一些文檔如下
void Main()
{
var settings = new ConnectionSettings(new Uri("http://localhost:9200"));
var client = new ElasticClient(settings);
client.Index<ImportFile>(
new ImportFile{
FileName = "Group-1.uhh",
FilePath = "",
GroupId = "0ae1206d0644eabd82ae490e612732df" +
"5da2cd141fdee70dc64207f86c96094"
},
index => index
.Index("reviewer-bdd-test-index")
.Type("importFile")
.Refresh());
client.Index<ImportFile>(
new ImportFile
{
FileName = "group-1.uhh",
FilePath = "",
GroupId = "0ae1206d0644eabd82ae490e612732df" +
"5da2cd141fdee70dc64207f86c96094"
},
index => index
.Index("reviewer-bdd-test-index")
.Type("importFile")
.Refresh());
var results = client.Search<ImportFile>(s => s
.Index("reviewer-bdd-test-index")
.Type("importFile")
.Query(q => q
.Filtered(fq => fq
.Filter(f => f
.Term(p => p.FileName, "Group-1.uhh")
)
)
)
);
Console.WriteLine(string.Format("{0} {1}", results.RequestInformation.RequestMethod, results.RequestInformation.RequestUrl));
Console.WriteLine(Encoding.UTF8.GetString(results.RequestInformation.Request));
Console.WriteLine("Matching document count: {0}", results.Documents.Count());
}
下面是在控制檯
POST http://localhost:9200/reviewer-bdd-test-index/importFile/_search
{
"query": {
"filtered": {
"filter": {
"term": {
"fileName": "Group-1.uhh"
}
}
}
}
}
Matching document count: 0
我們沒有得到匹配的文檔輸出。檢查中Elasticsearch映射與
curl -XGET "http://localhost:9200/reviewer-bdd-test-index/_mapping"
我們看到,對於類型importFile
的映射是
{
"reviewer-bdd-test-index": {
"mappings": {
"importFile": {
"properties": {
"fileName": {
"type": "string"
},
"groupId": {
"type": "string"
}
}
}
}
}
}
這不是我們所期望; fileName
和groupId
也應該有"index": "not_analyzed"
和filePath
甚至不在映射中。這兩者都是因爲Elasticsearch根據已通過的文檔推斷了映射 - 已將fileName
和groupId
映射爲字符串類型,並將使用標準分析器進行分析,並且我相信filePath
還未映射,因爲兩者都是所看到的文檔對於該字段具有空字符串值,因此應用於該字段的standard analyzer不會爲倒排索引產生任何標記,因此該字段未包含在映射中。
因此,爲了確保事情按預期工作,我們需要一個映射添加到索引編入索引的文檔
void Main()
{
var settings = new ConnectionSettings(new Uri("http://localhost:9200"));
var client = new ElasticClient(settings);
// Add the mapping for ImportFile to the index
client.CreateIndex(indexSelector => indexSelector
.Index("reviewer-bdd-test-index")
.AddMapping<ImportFile>(mapping => mapping
.MapFromAttributes()
)
);
// ... Same as above after this point
}
導致
POST http://localhost:9200/reviewer-bdd-test-index/importFile/_search
{
"query": {
"filtered": {
"filter": {
"term": {
"fileName": "Group-1.uhh"
}
}
}
}
}
Matching document count: 1
成功之前!我們有一個匹配的文檔。檢查中Elasticsearch映射產生我們所期待的
{
"reviewer-bdd-test-index": {
"mappings": {
"importFile": {
"properties": {
"fileName": {
"type": "string",
"index": "not_analyzed"
},
"filePath": {
"type": "string",
"store": true
},
"groupId": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
此外,屬性映射可以用流利的映射來代替,而不是
var indexResult = client.CreateIndex(indexDescriptor => indexDescriptor
.Index("reviewer-bdd-test-index")
.AddMapping<ImportFile>(mapping => mapping
.Type("importFile")
.MapFromAttributes()
.Properties(properties => properties
.String(s => s
.Name(file => file.FileName)
.Store(false)
.Index(FieldIndexOption.NotAnalyzed))
.String(s => s
.Name(file => file.GroupId)
.Store(false)
.Index(FieldIndexOption.NotAnalyzed))
.String(s => s
.Name(file => file.FilePath)
.Store(true))
)
)
);
任一屬性映射或流利的映射但會在這一點上做的,有有些事情只能通過流利的映射才能實現,如multi_fields。