【问题标题】:Searching documents elasticsearch , fields that contains subvalue for long field搜索文档 elasticsearch ,包含长字段子值的字段
【发布时间】:2021-11-23 22:49:24
【问题描述】:

我想在 elasticsearch 中搜索包含一个子值的字段(注册为 long)的文档, 例如:如果我在我的 elasticsearch 中有一个包含 (field1 : 12345) 的文档,如果我搜索 12,我应该找到这个文档。

    {
"_index": "overlog-mouvement-index",
"_type": "_doc",
"_id": "e707b9c1-8fb7-4cca-a659-c1ccdc3ed2f3",
"_version": 1,
"_score": null,
"_source": {
"typeEvent": 0,
"field1": 12345,
},
"sort": [
1633098180340
]
}

我正在尝试使用 QueryContainer,这是我的代码,你能帮我使用正确的查询吗?

long tempValue = 12;
 query &= filtreQuery.QueryString(t => t.DefaultField(f => f.field1.ToString()).Query("*" + tempValue.ToString() + "*"));

【问题讨论】:

    标签: c# .net elasticsearch


    【解决方案1】:

    在您的长字段中创建一个字符串子字段,例如

    {
        "field1": {
            "type": "long",
            "fields": {
                "to_string": {
                    "type": "string",
                    "index": "not_analyzed"
                }
            }
        }
    }
    

    现在,查询字符串字段,但从长字段中检索术语聚合

    {
      "size": 0,
      "query": {
        "prefix": {
          "field1.to_string": "12"
        }
      },
      "aggs": {
        "myAggregation": {
          "terms": {
            "field": "field1",
            "size": 5
          }
        }
      }
    }
    

    注意:不需要子字段,也可以创建完全独立的字段

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多