ElasticSearch & attachment type (NEST C#)

后端 未结 4 2015
孤城傲影
孤城傲影 2020-12-09 21:07

I\'m trying to index a pdf document with elasticsearch/NEST.

The file is indexed but search results returns with 0 hits.

I need the search result to return o

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-09 21:44

    Install the Attachment Plugin and restart ES

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

    Create an Attachment Class that maps to the Attachment Plugin Documentation

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

    Add a property on the Document class you are indexing with the name "File" and correct mapping

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

    Create your index explicitly before you index any instances of your class. If you don't do this, it will use dynamic mapping and ignore your attribute mapping. If you change your mapping in the future, always recreate the index.

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

    Index your item

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

    Search on the File property

      var query = Query.Term("file", "searchTerm");
    
      var searchResults = client.Search(s => s
              .From(start)
              .Size(count)
              .Query(query)
      );
    

提交回复
热议问题