【问题标题】:Elastic Search: How to extract All "FieldNames" that Match the Query Phrase/String弹性搜索:如何提取与查询短语/字符串匹配的所有“字段名称”
【发布时间】:2019-08-17 18:54:19
【问题描述】:

我想提取搜索文本出现在弹性搜索(存储)索引文档中的字段名称。

在弹性搜索中是否可以进行这种类型的查询,我在 C# 中使用 Nest Client

请参考以下示例:

示例:员工文档 { "first_name" : "emp first", "last_name" : "emp last" }

输入搜索文本:“first” 预期结果:["first_name"]

输入搜索文本:“emp” 预期输出:["first_name", "last_name"]

谢谢, AT

【问题讨论】:

    标签: c# elasticsearch lucene nest


    【解决方案1】:

    elasticsearch 中有一个特性"Named Queries",可以为每个查询命名,elasticsearch 会返回匹配的查询名称

    对于您的情况,您可以使用此查询

    GET index/doc_type/_search
    {
      "_source": [
        "first_name",
        "last_name"
      ],
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "first_name": {
                  "query": "emp",
                  "_name": "first_name"
                }
              }
            },
            {
              "match": {
                "last_name": {
                  "query": "emp",
                  "_name": "last_name"
                }
              }
            }
          ]
        }
      }
    }
    

    Elasticsearch 会返回这样的结果

    {
      "took": 90,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 1,
        "max_score": 16.399673,
        "hits": [
          {
            "_index": "index",
            "_type": "doc_type",
            "_id": "1",
            "_score": 16.399673,
            "_routing": "1",
            "_source": {
              "first_name": "emp first",
              "last_name": "emp last"
            },
            "matched_queries": [
              "first_name",
              "last_name"
            ]
          }
        ]
      }
    }
    

    你也可以用highlighting做同样的事情

    GET index/doc_type/_search
    {
      "_source": [
        "first_name",
        "last_name"
      ],
      "query": {
        "bool": {
          "should": [
            {
              "match": {
                "first_name": "emp"
              }
            },
            {
              "match": {
                "last_name": "emp"
              }
            }
          ]
        }
      },
      "highlight": {
        "fields": {
          "first_name": {},
          "last_name" : {}
        }
      }
    }
    

    示例响应:

    {
      "took": 90,
      "timed_out": false,
      "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
      },
      "hits": {
        "total": 1,
        "max_score": 16.399673,
        "hits": [
          {
            "_index": "index",
            "_type": "doc_type",
            "_id": "1",
            "_score": 16.399673,
            "_routing": "1",
            "_source": {
              "first_name": "emp first",
              "last_name": "emp last"
            },
            "highlight": [
              "first_name" : ["<em>emp</em> first"],
              "last_name" : ["<em>emp</em> last"]
            ]
          }
        ]
      }
    }
    

    【讨论】:

    • 谢谢,我会阅读并验证相同的内容。还有其他功能 'highlighting' ,这也可能做类似的工作,我必须找出来。
    • @adityaece 是的。您也可以通过突出显示来做到这一点
    猜你喜欢
    • 1970-01-01
    • 2016-04-10
    • 2014-06-02
    • 1970-01-01
    • 1970-01-01
    • 2018-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多