2015-08-25 2 views
2

Voici mon schémaElasticSearch requête NEST terme Aucun résultat

[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; } 
} 

J'ai fait une requête NEST comme celui-ci:

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

et renvoie zéro éléments!

Si je PEEK dans la db (en utilisant postman) Je peux voir mes documents:

{ 
    "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": "" 
     } 
     } 
    ] 
    } 
} 

Répondre

2

Il semble que vous ne pouvez pas avoir mis explicitement un mappage pour le type dans l'index avant l'indexation de vos documents , donc Elasticsearch a inféré le mappage basé sur les mappages par défaut pour les champs dans les documents qu'il a vu. À titre d'exemple, étant donné le type suivant

[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; } 
} 

si nous index des documents comme suit

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()); 
} 

ce qui suit est sortie dans la console

POST http://localhost:9200/reviewer-bdd-test-index/importFile/_search 
{ 
    "query": { 
    "filtered": { 
     "filter": { 
     "term": { 
      "fileName": "Group-1.uhh" 
     } 
     } 
    } 
    } 
} 
Matching document count: 0 

Nous recevons aucun document correspondant. Vérification de la cartographie en ElasticSearch avec

curl -XGET "http://localhost:9200/reviewer-bdd-test-index/_mapping" 

On voit que le mappage pour le type importFile est

{ 
    "reviewer-bdd-test-index": { 
     "mappings": { 
     "importFile": { 
      "properties": { 
       "fileName": { 
        "type": "string" 
       }, 
       "groupId": { 
        "type": "string" 
       } 
      } 
     } 
     } 
    } 
} 

qui n'est pas ce que nous attendons; à la fois fileName et groupId devrait également avoir "index": "not_analyzed" et filePath n'est même pas dans la cartographie. Les deux sont parce que Elasticsearch a déduit le mappage basé sur les documents qu'il a été passé - fileName et groupId ont été mappés en tant que types de chaîne et auront subi une analyse avec l'analyseur standard, et je croisfilePath n'a pas été mappé car les deux La valeur standard analyzer appliquée au champ ne produirait aucun jeton pour l'index inversé, donc le champ n'est pas inclus

Donc, pour faire en sorte que les choses fonctionnent comme prévu, nous devons ajouter un mappage à l'index avant l'indexation des documents

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 
} 

Quels sont les résultats dans

POST http://localhost:9200/reviewer-bdd-test-index/importFile/_search 
{ 
    "query": { 
    "filtered": { 
     "filter": { 
     "term": { 
      "fileName": "Group-1.uhh" 
     } 
     } 
    } 
    } 
} 
Matching document count: 1 

succès! Nous avons un document correspondant.Vérification de la cartographie dans ElasticSearch donne ce que nous attendons

{ 
    "reviewer-bdd-test-index": { 
     "mappings": { 
     "importFile": { 
      "properties": { 
       "fileName": { 
        "type": "string", 
        "index": "not_analyzed" 
       }, 
       "filePath": { 
        "type": "string", 
        "store": true 
       }, 
       "groupId": { 
        "type": "string", 
        "index": "not_analyzed" 
       } 
      } 
     } 
     } 
    } 
} 

De plus, la mise en correspondance d'attribut peut être remplacé par la mise en correspondance couramment la place

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)) 
     ) 
    ) 
); 

Soit le mappage des attributs ou mappage couramment fera à ce stade cependant, il sont certaines choses qui ne peuvent être obtenues avec des applications fluides telles que multi_fields.