c# - 如何索引 NotAnalyzed 字段类型列表?
问题描述
class MyObject{
public string Name{get;set;}
public List<string> Tags{get;set;}
}
/*Create mapping */
client.Map<MyObject>(m =>
m.Properties(props =>
props.String(s =>
s.Name(p => p.Name)
.Path(MultiFieldMappingPath.Full)
.Index(FieldIndexOption.NotAnalyzed)
.Fields(f =>
f.String(ps =>
ps.Name(p => p.Name.Suffix("searchable"))
.Index(FieldIndexOption.Analyzed)
)
)
)
)
);
如何为字段标签相同的字段名称索引 NotAnalyzed
我想在标签字段中准确搜索一个短语
示例:我想搜索“弹性搜索”以找出在标签字段中哪个对象恰好包含该单词
Obj1:
{
"Name":"Object 1",
"Tags":["elastic search","how to code"]
}
Obj2:
{
"Name":"Object 2",
"Tags":["elastic","c#"]
}
Obj3:
{
"Name":"Object 2",
"Tags":["learn elastic search","learn C#"]
}
===> Result: Obj 1
解决方案
Based on your request I will create a test1 index.
PUT test1/doc/1
{
"Name": "Object 1",
"Tags": [
"elastic search",
"how to code"
]
}
PUT test1/doc/2
{
"Name":"Object 2",
"Tags":["elastic","c#"]
}
因此,我将编写查询以获取您在示例中提到的确切术语弹性搜索。
GET test1/doc/_search
{
"query": {
"term": {
"Tags.keyword":
"elastic search"
}
}
}
所以结果是下面的查询是
curl -XGET "http://localhost:9200/test1/doc/_search"
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "test1",
"_type": "doc",
"_id": "2",
"_score": 1,
"_source": {
"Name": "Object 2",
"Tags": [
"elastic",
"c#"
]
}
},
{
"_index": "test1",
"_type": "doc",
"_id": "1",
"_score": 1,
"_source": {
"Name": "Object 1",
"Tags": [
"elastic search",
"how to code"
]
}
}
]
}
}
所以现在查询根据您的字段获取文档。
curl -XGET "http://localhost:9200/test1/doc/_search" -H 'Content-Type: application/json' -d'
{
"query": {
"term": {
"Tags.keyword":
"elastic search"
}
}
}'
结果是
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.2876821,
"hits": [
{
"_index": "test1",
"_type": "doc",
"_id": "1",
"_score": 0.2876821,
"_source": {
"Name": "Object 1",
"Tags": [
"elastic search",
"how to code"
]
}
}
]
}
}
希望它有效。如果您仍然面临任何问题,请告诉我。
推荐阅读
- reactjs - 如何使用 Nextjs 将我的 URL 与应用程序状态同步
- vue.js - “公共”文件夹中文件的 VUE CLI 不同路径
- php - 数组索引没有按应有的方式执行?
- apache - 使用 WHM 和 apache2 将子域转发到 localhost 的端口
- python - 在 python 中,如何在具有 dtype 对象的列上使用 split('_')?
- c++ - 如何使地图按值排序C++
- flutter - 使用 Package flutter_webview_plugin 获取旧的滚动偏移量
- postgresql - 列索引超出范围:2,列数:1 更新 jsonb 列时出错
- c# - 在等待异步完成之前函数返回
- python - 将 ID 与一组不同的名称匹配