2015-08-25 63 views
2

這裏我的架構ElasticSearch NEST術語查詢返回任何結果

[ElasticType(Name = "importFile")] 
public class ImportFile : DocumentMapping 
{ 
    [ElasticProperty(Store = false, Index = FieldIndexOption.NotAnalyzed)] 
    public string FileName { get; set; } 

    [ElasticProperty(Store = false, Index = FieldIndexOption.NotAnalyzed)] 
    public string GroupId { get; set; } 

    [ElasticProperty(Store = false, Index = FieldIndexOption.Analyzed)] 
    public string FilePath { get; set; } 
} 

我做了一個窩查詢像這樣的:

var res = ElasticClient.Search<ImportFile>(s => s 
     .Index(ElasticIndexName) 
     .Filter(f => 
      f.Term(t => t.FileName, "Group-1.uhh"))).Documents.ToArray(); 

並返回0元!

如果我的數據庫裏面偷看(使用郵遞員),我可以看到我的文件:

{ 
    "took": 5, 
    "timed_out": false, 
    "_shards": { 
    "total": 2, 
    "successful": 2, 
    "failed": 0 
    }, 
    "hits": { 
    "total": 2, 
    "max_score": 14.069489, 
    "hits": [ 
     { 
     "_index": "reviewer-bdd-test-index", 
     "_type": "importFile", 
     "_id": "AU9kUka2hr5Jg98UXOae", 
     "_score": 14.069489, 
     "_source": { 
      "fileName": "Group-1.uhh", 
      "groupId": "0ae1206d0644eabd82ae490e612732df5da2cd141fdee70dc64207f86c96094f", 
      "filePath": "" 
     } 
     }, 
     { 
     "_index": "reviewer-bdd-test-index", 
     "_type": "importFile", 
     "_id": "AU9kZO25hr5Jg98UXRnk", 
     "_score": 14.069489, 
     "_source": { 
      "fileName": "group-1.uhh", 
      "groupId": "0ae1206d0644eabd82ae490e612732df5da2cd141fdee70dc64207f86c96094f", 
      "filePath": "" 
     } 
     } 
    ] 
    } 
} 

回答

2

這聽起來像你可能沒有明確提出的映射類型到索引之前索引你的文檔,因此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" 
       } 
      } 
     } 
     } 
    } 
} 

這不是我們所期望; fileNamegroupId也應該有"index": "not_analyzed"filePath甚至不在映射中。這兩者都是因爲Elasticsearch根據已通過的文檔推斷了映射 - 已將fileNamegroupId映射爲字符串類型,並將使用標準分析器進行分析,並且我相信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