如何在elasticsearch中搜索查询到的文档?

xtfmy6hx  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(3)|浏览(421)

我是elasticsearch的新手,我面临一个问题。我的任务是搜索一组文档。例如,我的数据结构如下:

type Doc struct{
   id string
   project_id string
   code string
   name string
   status string
}

但困难的是,我如何获取所有project_id=的文档,然后通过与关键字“test”匹配的任何其他字段(code、name、status)对其进行搜索(例如)。如何在elasticsearch查询中做到这一点,请帮助我!
谢谢。

wbrvyc0a

wbrvyc0a1#

可以使用与文档匹配的布尔查询来匹配其他查询的布尔组合。 must 与逻辑and运算符and相同 should 与逻辑或运算符相同
添加索引数据、搜索查询和搜索结果的工作示例
索引数据:

{
  "id": 2,
  "project_id":"abc",
  "code": "a",
  "name":"bhavya",
  "status":"engineer"
}
{
  "id": 1,
  "project_id":"abc",
  "code": "a",
  "name":"bhavya",
  "status":"student"
}
{
  "id": 3,
  "project_id":"def",
  "code": "a",
  "name":"deeksha",
  "status":"engineer"
}

搜索查询:
给定的查询满足以下条件: "project_id" = "" 以及 "name" : "bhavya" 以及 "status":"student" ```
{
"query": {
"bool": {
"must": [
{
"match": {
"project_id": "abc"
}
},
{
"match": {
"name": "bhavya"
}
},
{
"match": {
"status": "student"
}
}
]
}
}
}

搜索结果:

"hits": [
{
"_index": "stof_64274465",
"_type": "_doc",
"_id": "1",
"_score": 1.7021472,
"_source": {
"id": 1,
"project_id": "abc",
"code": "a",
"name": "bhavya",
"status": "student"
}
}
]

92dk7w1h

92dk7w1h2#

我想也许使用logstash和过滤一些数据可以帮助你。

hfsqlsce

hfsqlsce3#

这是我的解决办法

GET /[index_name]/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "some text here"
          }
        }
      ],
      "filter": [
        {
          "term": {
            "project_id.keyword": "abc"
          }
        }
      ]
    }
  }
}

相关问题