2013-02-08 15 views
9

Sto cercando di indicizzare un documento PDF con elasticsearch/NEST.ElasticSearch e tipo di allegato (NEST C#)

Il file viene indicizzato ma i risultati della ricerca vengono restituiti con 0 risultati.

Ho bisogno risultato della ricerca per restituire solo l'ID del documento e il risultato clou

(senza il contenuto Base64)

Ecco il codice:

Io apprezzo qualsiasi aiuto qui,

Grazie,

class Program 
{ 
    static void Main(string[] args) 
    { 
     // create es client 
     string index = "myindex"; 

     var settings = new ConnectionSettings("localhost", 9200) 
      .SetDefaultIndex(index); 
     var es = new ElasticClient(settings); 

     // delete index if any 
     es.DeleteIndex(index); 

     // index document 
     string path = "test.pdf"; 
     var doc = new Document() 
     { 
      Id = 1, 
      Title = "test", 
      Content = Convert.ToBase64String(File.ReadAllBytes(path)) 
     }; 

     var parameters = new IndexParameters() { Refresh = true }; 
     if (es.Index<Document>(doc, parameters).OK) 
     { 
      // search in document 
      string query = "semantic"; // test.pdf contains the string "semantic" 

      var result = es.Search<Document>(s => s 
       .Query(q => 
        q.QueryString(qs => qs 
         .Query(query) 
        ) 
       ) 
       .Highlight(h => h 
        .PreTags("<b>") 
        .PostTags("</b>") 
        .OnFields(
         f => f 
         .OnField(e => e.Content) 
         .PreTags("<em>") 
         .PostTags("</em>") 
        ) 
       ) 
      ); 

      if (result.Hits.Total == 0) 
      { 
      } 
     } 
    } 
} 

[ElasticType(
    Name = "document", 
    SearchAnalyzer = "standard", 
    IndexAnalyzer = "standard" 
)] 
public class Document 
{ 
    public int Id { get; set; } 

    [ElasticProperty(Store = true)] 
    public string Title { get; set; } 

    [ElasticProperty(Type = FieldType.attachment, 
     TermVector = TermVectorOption.with_positions_offsets)] 
    public string Content { get; set; } 
} 
+0

Inoltre, ha verificato che i mapper-allegati plugin installato e caricato (utilizzando es.yml: plugin.mandatory: mapper -attachments). Ancora, nessun colpo per le parole contenute nel mio pdf. Ho cercato su Google le risposte a questo argomento (StackOverflow e altro) e ho trovato solo esempi di arricciature, nessun esempio di utilizzo con C#/NEST. (solo una nota: durante la ricerca di document.title ('test.pdf') riesco a recuperare il documento ma non ci sono risultati durante la ricerca 'test'. –

+0

solo per farti sapere che ho intenzione di creare test di integrazione per questo domani e rispondere alla domanda. Non sono in grado di rispondere prima. –

+1

eventuali aggiornamenti su questa domanda? – slimflem

risposta

1

// I sto usando FSRiver plugin - https://github.com/dadoonet/fsriver/

void Main() 
{ 
    // search in document 
    string query = "directly"; // test.pdf contains the string "directly" 
    var es = new ElasticClient(new ConnectionSettings(new Uri("http://*.*.*.*:9200")) 
     .SetDefaultIndex("mydocs") 
     .MapDefaultTypeNames(s=>s.Add(typeof(Doc), "doc"))); 
     var result = es.Search<Doc>(s => s 
     .Fields(f => f.Title, f => f.Name) 
     .From(0) 
     .Size(10000) 
      .Query(q => q.QueryString(qs => qs.Query(query))) 
      .Highlight(h => h 
       .PreTags("<b>") 
       .PostTags("</b>") 
       .OnFields(
        f => f 
        .OnField(e => e.File) 
        .PreTags("<em>") 
        .PostTags("</em>") 
       ) 
      ) 
     ); 
} 

[ElasticType(Name = "doc", SearchAnalyzer = "standard", IndexAnalyzer = "standard")] 
public class Doc 
{ 
    public int Id { get; set; } 

    [ElasticProperty(Store = true)] 
    public string Title { get; set; } 

    [ElasticProperty(Type = FieldType.attachment, TermVector = TermVectorOption.with_positions_offsets)] 
    public string File { get; set; } 
    public string Name { get; set; } 
} 
0

Sto lavorando sulla stessa così ora sto provando questo http://www.elasticsearch.cn/tutorials/2011/07/18/attachment-type-in-action.html

Questo articolo spiega problema

pay attension che si dovrebbe fare corretta mappatura

"title" : { "store" : "yes" }, 
"file" : { "term_vector":"with_positions_offsets", "store":"yes" } 

Cercherò di capire come farlo con NEST api e aggiornare questo post

+0

Eventuali aggiornamenti su come farlo funzionare? – bayCoder

-1

È necessario aggiungere la mappatura come di seguito prima di indicizzare gli articoli.

client.CreateIndex("yourindex", c => c.NumberOfReplicas(0).NumberOfShards(12).AddMapping<AssetSearchEntryModels>(m => m.MapFromAttributes())); 
8

Installare il plugin attaccamento e riavviare ES

bin/plugin -install elasticsearch/elasticsearch-mapper-attachments/2.3.2 

Creare una classe di attaccamento che mappa la Documentazione Plugin Allegato

public class Attachment 
    { 
     [ElasticProperty(Name = "_content")] 
     public string Content { get; set; } 

     [ElasticProperty(Name = "_content_type")] 
     public string ContentType { get; set; } 

     [ElasticProperty(Name = "_name")] 
     public string Name { get; set; } 
    } 

Aggiungere una proprietà sulla classe documento che si sta a molla con arresto il nome "File" e la mappatura corretta

[ElasticProperty(Type = FieldType.Attachment, TermVector = TermVectorOption.WithPositionsOffsets, Store = true)] 
    public Attachment File { get; set; } 

Crea il tuo indice in modo esplicito prima di indicizzare qualsiasi istanza della tua classe. Se non lo fai, utilizzerà la mappatura dinamica e ignorerà la mappatura degli attributi. Se cambi la mappatura in futuro, ricrea sempre l'indice.

client.CreateIndex("index-name", c => c 
    .AddMapping<Document>(m => m.MapFromAttributes()) 
); 

Indice vostro articolo

string path = "test.pdf"; 

    var attachment = new Attachment(); 
    attachment.Content = Convert.ToBase64String(File.ReadAllBytes(path)); 
    attachment.ContentType = "application/pdf"; 
    attachment.Name = "test.pdf"; 

    var doc = new Document() 
    { 
     Id = 1, 
     Title = "test", 
     File = attachment 
    }; 
    client.Index<Document>(item); 

Ricerca sulla proprietà del file

var query = Query<Document>.Term("file", "searchTerm"); 

    var searchResults = client.Search<Document>(s => s 
      .From(start) 
      .Size(count) 
      .Query(query) 
); 
+0

Ottimo funziona per me .... Grazie !! –

Problemi correlati