Nest를 사용하여 C #의 JObject 내부 속성을 동적으로 매핑하고 싶습니다. 목표는 개체의 모든 문자열 필드를 SearchAsYouType으로 매핑하는 것입니다. 작동하지 않는 세 가지 방법에 대해 생각했습니다.
public class Forfait
{
public long Id { get; set; }
[SearchAsYouType()]
public string Data { get; set; }
}
public class Act
{
public JObject Entity;
}
Forfait forfait = new Forfait()
{
Data = "data",
Id = 99
};
Act act = new Act()
{
Entity = JObject.FromObject(forfait)
};
await client.Indices.CreateAsync("index", o => o
.Map<Act>(m => m
.AutoMap()
2. DynamicTemplates를 사용하지만 매핑에서 SearchAsYouType을 찾을 수 없습니다. 아직 Nest 7.4.1에 존재하지 않는 것 같습니다.
await client.Indices.CreateAsync("index", o => o
.Map<Act>(m => m
.DynamicTemplates(d =>d
.DynamicTemplate("stringassearch",dt => dt
.Match("entity.*")
.MatchMappingType("string")
.Mapping(ma =>ma
.)))));
3. Visitor를 사용하여 모든 문자열을 SearchAsYouType으로 지정
public class EveryStringIsASearchAsYouTypePropertyVisitor : NoopPropertyVisitor
{
public override IProperty Visit(PropertyInfo propertyInfo, ElasticsearchPropertyAttributeBase attribute)
{
if (propertyInfo.PropertyType == typeof(String))
return new SearchAsYouTypeProperty();
return null;
}
}
await client.Indices.CreateAsync("index", o => o
.Map<Act>(m => m
.AutoMap(new EveryStringIsASearchAsYouTypePropertyVisitor(),2)
모든 것이 실패했습니다
솔루션이 NEST.JsonNetSerializer에 있다는 느낌이 들지만 매핑에 사용되는 설정을 JObject 내부에 적용하지만 유용한 것을 찾을 수 없습니다.
2. DynamicTemplates를 사용하지만 매핑에서 SearchAsYouType을 찾을 수 없습니다. 아직 Nest 7.4.1에 존재하지 않는 것 같습니다.
당신이 맞습니다.에서 누락 된 것처럼 보이지만 유형에 SingleMappingSelector
대한 지원을 추가 할이 확장 클래스를 사용하여 쉽게 해결할 수 있습니다 search_as_you_type
.
static class MappingExtension
{
public static IProperty SearchAsYouType<T>(this SingleMappingSelector<T> mappingSelector,
Func<SearchAsYouTypePropertyDescriptor<T>, ISearchAsYouTypeProperty> selector) where T : class
=> selector?.Invoke(new SearchAsYouTypePropertyDescriptor<T>());
}
다음과 같이 동적 템플릿을 만들 수 있습니다.
var createIndexResponse = await client.Indices.CreateAsync("index", o => o
.Map<Act>(m => m
.AutoMap<Act>()
.DynamicTemplates(d => d
.DynamicTemplate("stringassearch", dt => dt
.PathMatch("entity.*")
.MatchMappingType("string")
.Mapping(ma => ma.SearchAsYouType(s => s))))));
참고로 변경 Match(..)
했습니다 PathMatch(..)
. 이것이 필요한 것 같습니다. 또한 Act
정의를 다음 과 같이 변경 해야했습니다.
public class Act
{
public object Entity;
}
샘플 문서를 인덱싱 한 후이 인덱스 매핑이 생성되었습니다.
{
"index": {
"mappings": {
"dynamic_templates": [
{
"stringassearch": {
"path_match": "entity.*",
"match_mapping_type": "string",
"mapping": {
"type": "search_as_you_type"
}
}
}
],
"properties": {
"entity": {
"properties": {
"data": {
"type": "search_as_you_type",
"max_shingle_size": 3
},
"id": {
"type": "long"
}
}
}
}
}
}
}
다음 은 GH 문제입니다.
도움이되기를 바랍니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다