【问题标题】:Searching all columns in a table and displaying only those columns where keywords are matched in elasticsearch搜索表中的所有列并仅显示在elasticsearch中匹配关键字的那些列
【发布时间】:2021-08-07 11:56:52
【问题描述】:

我有一个包含 100 行和 10 列的 csv 文件,我已将其索引到 Elastic Search 中。现在我想在整个数据中搜索几个关键字。如何只显示那些关键字匹配的那些列?

【问题讨论】:

  • 能否分享一些示例索引数据和预期的搜索结果?
  • 说我的桌子有 4 个列:姓名、年龄、性别和身高,如果我搜索“男性”,我只想显示性别列,因为只有那个列会有匹配关键词。有没有办法做到这一点?

标签: csv elasticsearch


【解决方案1】:

据我所知,没有确切的方法只显示与您的查询匹配的列。但您可以使用highlighting 来突出显示匹配的字词。

添加一个带有索引数据的工作示例、映射、搜索查询一个搜索结果

索引映射:

{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "age": {
        "type": "integer"
      },
      "gender": {
        "type": "text"
      },
      "height": {
        "type": "integer"
      }
    }
  }
}

索引数据:

{
  "name": "c",
  "gender": "female",
  "age": 21,
  "height": 160
}
{
  "name": "a",
  "gender": "male",
  "age": 20,
  "height": 150
}
{
  "name": "b",
  "gender": "male",
  "age": 30,
  "height": 100
}

搜索查询:

{
  "query": {
    "multi_match": {
      "query": "male"
    }
  },
  "highlight": {
    "fields": {
      "*": {
        "type": "plain",
        "fragment_size": 20,
        "pre_tags": "<span class='bold'>",
        "post_tags": "</span>",
        "number_of_fragments": 1
      }
    }
  }
}

搜索结果:

 "hits": [
      {
        "_index": "67582942",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.35667494,
        "_source": {
          "name": "a",
          "gender": "male",
          "age": 20,
          "height": 150
        },
        "highlight": {
          "gender": [
            "<span class='bold'>male</span>"     // note this
          ]
        }
      },
      {
        "_index": "67582942",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.35667494,
        "_source": {
          "name": "b",
          "gender": "male",
          "age": 30,
          "height": 100
        },
        "highlight": {
          "gender": [
            "<span class='bold'>male</span>"          // note this
          ]
        }
      }
    ]

【讨论】:

  • 非常感谢@ESCoder,现在可以使用了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-12-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多