我已将一些附件编入索引到我的Elasticsearch数据库中。当我使用附件中的单词进行全文搜索时,我会得到结果(点击),但我没有得到"突出显示"在回应中。
我无法弄清楚我做错了什么,这是我的代码:
[ElasticsearchType(Name = "temporaryapplication")]
public class Attachment
{
[String(Name = "_content", Store = true, TermVector = TermVectorOption.WithPositionsOffsets)]
public string Content { get; set; }
[String(Name = "_content_type")]
public string ContentType { get; set; }
[String(Name = "_name")]
public string Name { get; set; }
}
}
public class Document
{
[Attachment]
public Attachment File { get; set; }
}
我在这里提出搜索请求:
public async Task<StructuredSearchResponse> GetApplications(Guid positionId, SearchQuery query)
{
var searchResult =
await _client.LowLevel.SearchAsync<string>(ApplicationsIndexName, "temporaryapplication", new SearchRequest()
{
From = (query.PageSize*query.PageNumber) - query.PageSize,
Size = query.PageSize,
Query = GetQuery(query),
Source = GetFields(),
Aggregations = GetAggregations(),
Highlight = new Highlight() {
Fields = new Dictionary<Field, IHighlightField>
{
{ "file.content", new HighlightField
{
Type = HighlighterType.Postings,
HighlightQuery = new MatchQuery
{
Field = "file.content",
Query = query.Freetext
}
}
}
}
}
});
return searchResult.ToStructuredSearchResponse(query.PageSize, query.PageNumber);
}
索引映射:
var indexDescriptor =
new CreateIndexDescriptor(new IndexName {Name = ApplicationsIndexName}).Mappings(
ms => ms.Map<TemporaryApplication>(m => m.AutoMap())
.Map<Attachment>(m => m.AutoMap()));
_client.CreateIndex(indexDescriptor);
用于制图的POCO:
public class TemporaryApplication
{
[String(Name = "FirstName")]
public string FirstName { get; set; }
[String(Name = "LastName")]
public string LastName { get; set; }
[String(Name = "Education")]
public string Education { get; set; }
[String(Name = "WorkExperience")]
public string WorkExperience { get; set; }
[String(Name = "Age")]
public string Age { get; set; }
[String(Name = "Id")]
public int Id { get; set; }
[Attachment]
public Attachment File { get; set; }
}
索引附件的方法:
public async void IndexDocument(Attachment attachmentDocument)
{
var doc = new Document()
{
File = attachmentDocument,
};
var updateResponse = _client.Update<TemporaryApplication, object>(5, descriptor => descriptor
.Doc(new { file = doc.File }));
}
JSON for document:
{
"_index": "applications",
"_type": "temporaryapplication",
"_id": "5",
"_version": 2,
"found": true,
"_source": {
"FirstName": "Petter",
"LastName": "Stordalen",
"Education": "master",
"WorkExperience": "3years",
"Age": "8.0",
"Id": 5,
"file": {
"_name": "07443a5a-ad56-4610-bd53-8b8ea43a4c89 (1).doc",
"_content_type": "application/msword",
"_content":
"e1xydGYxXGFuc2lcYW5zaWNwZzEyNTJcZGVmZjBcbm91aWNvbXBhdFxkZWZsYW5nMTA0NHtcZm9udHRibHtcZjBcZm5pbFxmY2hhcnNldDAgQ2FsaWJyaTt9fQ0Ke1wqXGdlbmVyYXRvciBSaWNoZWQyMCAxMC4wLjE0MzkzfVx2aWV3a2luZDRcdWMxIA0KXHBhcmRcc2EyMDBcc2wyNzZcc2xtdWx0MVxmMFxmczIyXGxhbmcyMCBWQ1hDVlhDVlhDVlhDVlhWWENWWENWXHBhcg0KfQ0KAA=="
}
}
}
用于映射的JSON: Elasticsearch mapping updated GetQuery方法:
public static class ElasticsearchExtensions
{
public static StructuredSearchResponse ToStructuredSearchResponse(this ElasticsearchResponse<string> response, int pageSize, int pageNumber)
{
return new StructuredSearchResponse(JObject.Parse(response.Body), pageSize,pageNumber);
}
public static QueryContainer AddEducation(this QueryContainer queryContainer, SearchQuery query)
{
if (query.Education.Count > 0)
{
for (var i = 0; i < query.Education.Count; i++)
{
queryContainer |= new MatchQuery()
{
Field = "Education",
Query = query.Education[i]
};
}
}
return queryContainer;
}
public static QueryContainer AddWorkExperience(this QueryContainer queryContainer, SearchQuery query)
{
if (query.WorkExperience.Count > 0)
{
if (queryContainer != null)
{
queryContainer &= new MatchQuery()
{
Field = "WorkExperience",
Query = query.WorkExperience[0]
};
for (var i = 1; i < query.WorkExperience.Count; i++)
{
queryContainer |= new MatchQuery()
{
Field = "WorkExperience",
Query = query.WorkExperience[i]
};
}
}
else
{
for (var i = 0; i < query.WorkExperience.Count; i++)
{
queryContainer |= new MatchQuery()
{
Field = "WorkExperience",
Query = query.WorkExperience[i]
};
}
}
}
return queryContainer;
}
public static QueryContainer AddAgeInterval(this QueryContainer queryContainer, SearchQuery query)
{
if (query.FromAge != "" || query.ToAge != "")
{
dynamic From = null;
dynamic To = null;
if (query.FromAge != null)
{
From = Convert.ToDouble(query.FromAge);
}
if (query.ToAge != null)
{
To = Convert.ToDouble(query.ToAge);
}
if (queryContainer == null)
{
queryContainer = new NumericRangeQuery()
{
Field = "Age",
GreaterThanOrEqualTo = From,
LessThanOrEqualTo = To,
};
}
else
{
queryContainer &= new NumericRangeQuery()
{
Field = "Age",
GreaterThanOrEqualTo = From,
LessThanOrEqualTo = To
};
}
}
return queryContainer;
}
public static QueryContainer AddFreeTextSearch(this QueryContainer queryContainer, SearchQuery query)
{
if (!(string.IsNullOrEmpty(query.Freetext)))
{
if (queryContainer == null)
{
queryContainer |= new QueryStringQuery()
{
Fields =
Field.Create("FirstName").And("LastName").And("Education").And("WorkExperience").And("Age").And("file.content"),
Query = "*" + query.Freetext + "*"
};
}
else
{
queryContainer &= new QueryStringQuery()
{
Fields =
Field.Create("FirstName").And("LastName").And("Education").And("WorkExperience").And("Age").And("file.content"),
Query = "*" + query.Freetext + "*"
};
}
}
return queryContainer;
}
}
}
这是查询参数包含的内容:
有人能看出我做错了吗?
答案 0 :(得分:1)
According to documentation您的映射错误
如果要突出显示附件内容,则需要进行设置 &#34; store&#34;:true和&#34; term_vector&#34;:&#34; with_positions_offsets&#34;为您 附件领域。这是一个完整的脚本:
我看到你在代码中拥有所有这些,所以也许你在创建索引后添加了它。尝试删除并重新创建它
seems that Nest无法映射复杂类型
var indexDescriptor =
new CreateIndexDescriptor(new IndexName {Name = ApplicationsIndexName}).Mappings(
ms => ms.Map<TemporaryApplication>(m => m.AutoMap())
.Map<Attachment>(m => m.AutoMap()
.Properties(ps => ps
.Attachment(s => s.Name(p => p.File)
.FileField(ff => ff.Name(p => p.File).TermVector(TermVectorOption.WithPositionsOffsets))
)
)));