gpt4 book ai didi

elasticsearch - 如何通过NEST更改要由Elasticsearch索引的字典中条目的映射字段名称?

转载 作者:行者123 更新时间:2023-12-03 01:58:39 25 4
gpt4 key购买 nike

我需要索引一些动态数据(在编译时未知字段),并且还需要索引GeoPoint。

我想使用NEST API并索引到Elasticsearch中。我有以下代码来处理此问题:

public class MyRow
{
public string Id { get; set; }

[ElasticProperty(Name = "GeometryHotspot", Type = FieldType.GeoPoint)]
public Coordinate GeometryHotspot { get; set; }

public Dictionary<string, object> DynamicValues { get; set; }
}

这可行,但是当我在数组中设置动态值时,如下所示:
row.DynamicValues["MyKey"] = "Some value";

然后,Elasticsearch为该值生成一个名为“DynamicValues.MyKey”的Lucene字段。由于某些遗留问题(该字段中的现有查询),我需要Lucene字段为“MyKey”-而不以“DynamicValues”为前缀。

有人知道如何实现吗?我看过各种映射方法,但是没有运气。我无法弄清楚如何指定Lucene字段名称-只能确定是否应对其进行分析等。

最佳答案

有一种方法可以实现您想要的。将IdGeometryHotspot键值添加到DynamicValues字典中,然后索引DynamicValues而不是MyRow对象。您可以在此处使用 dynamic templates 的功能来实现所需的映射。我写了一个小程序来说明这一点。

public class Coordinate
{
public float lat { get; set; }
public float lon { get; set; }
}

public class MyRow
{
public string Id { get; set; }
public Coordinate GeometryHotspot { get; set; }
public Dictionary<string, object> DynamicValues { get; set; }
}

class Program
{
static void Main(string[] args)
{
var row = new MyRow
{
Id = "randomid",
GeometryHotspot = new Coordinate
{
lat = 1.23f,
lon = 4.56f
},
DynamicValues = new Dictionary<string, object>
{
["numberField"] = 1,
["stringField"] = "TWO",
["dateField"] = DateTime.UtcNow,
["realNumberField"] = 25.6,
["booleanField"] = true
}
};

// Add the concrete fields of MyRow class to its DynamicValues property
row.DynamicValues["id"] = row.Id;
row.DynamicValues["geometryHotspot"] = row.GeometryHotspot;

var client = new ElasticClient(new ConnectionSettings(new Uri("http://localhost:9200")));

// Create a mapping called "myRow" in index "myindex". Make sure "myindex" exists already.
client.Map<object>(d => d
.Index("myindex")
.Type("myRow")
.DynamicTemplates(dtd => dtd
.Add(dd => dd
.Name("geopoint")
.Match("geometryHotspot")
.Mapping(fm => fm
.GeoPoint(f => f
.IndexLatLon())))));

client.Index(row.DynamicValues, d => d // Notice how we index row.DynamicValues and not row
.Index("myindex")
.Type("myRow"));
}
}

之后,只需搜索“myindex”索引和“myRow”类型的所有文档,即可看到动态字段不再以“DynamicFields”为前缀。串。

以下是 _search API的输出。
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1,
"hits": [
{
"_index": "myindex",
"_type": "myRow",
"_id": "AVIqRf4rqbcnf7z9goAa",
"_score": 1,
"_source": {
"numberField": 1,
"stringField": "TWO",
"dateField": "2016-01-10T06:42:55.7535106Z",
"realNumberField": 25.6,
"booleanField": true,
"id": "randomid",
"geometryHotspot": {
"lat": 1.23,
"lon": 4.56
}
}
}
]
}
}

希望这可以帮助。

关于elasticsearch - 如何通过NEST更改要由Elasticsearch索引的字典中条目的映射字段名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34686990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com