Yos*_*hen 9 attachment elasticsearch nest
我正在尝试使用elasticsearch/NEST索引pdf文档.
该文件已编制索引,但搜索结果返回0次点击.
我需要搜索结果只返回文档ID和突出显示结果
(没有base64内容)
这是代码:
我会感激任何帮助,
谢谢,
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; }
}
Run Code Online (Sandbox Code Playgroud)
小智 8
安装Attachment Plugin并重新启动ES
bin/plugin -install elasticsearch/elasticsearch-mapper-attachments/2.3.2
Run Code Online (Sandbox Code Playgroud)
创建一个映射到Attachment Plugin Documentation的Attachment Class
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; }
}
Run Code Online (Sandbox Code Playgroud)
在要使用名称"File"进行索引的Document类上添加属性并更正映射
[ElasticProperty(Type = FieldType.Attachment, TermVector = TermVectorOption.WithPositionsOffsets, Store = true)]
public Attachment File { get; set; }
Run Code Online (Sandbox Code Playgroud)
在索引类的任何实例之前,显式创建索引.如果不这样做,它将使用动态映射并忽略您的属性映射.如果将来更改映射,请始终重新创建索引.
client.CreateIndex("index-name", c => c
.AddMapping<Document>(m => m.MapFromAttributes())
);
Run Code Online (Sandbox Code Playgroud)
索引您的项目
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);
Run Code Online (Sandbox Code Playgroud)
在File属性上搜索
var query = Query<Document>.Term("file", "searchTerm");
var searchResults = client.Search<Document>(s => s
.From(start)
.Size(count)
.Query(query)
);
Run Code Online (Sandbox Code Playgroud)
小智 1
// 我正在使用 FSRiver 插件 - 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; }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6898 次 |
| 最近记录: |