我想通过搜索在Elasticsearch中只显示一条记录

b91juud3  于 2022-12-22  发布在  ElasticSearch
关注(0)|答案(1)|浏览(173)

使用以下查询:

GET feeds/_search
{
  "query": {
    "nested": {
      "path": "comment",
      "query": {
        "multi_match": {
          "query": "ali",
          "fields": ["body","title","comment.c_text"]
        }
      }
    }
  }
}

我正在得到结果

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.8025915,
    "hits" : [
      {
        "_index" : "feeds",
        "_type" : "_doc",
        "_id" : "NeoTNIUB1Qg_7rFp-8RV",
        "_score" : 0.8025915,
        "_source" : {
          "feed_id" : "1",
          "title" : "Mateen",
          "body" : "This is mateen",
          "comment" : [
            {
              "c_id" : "11",
              "feed_id" : "1",
              "c_text" : "get well soon mateen"
            },
            {
              "c_id" : "12",
              "feed_id" : "1",
              "c_text" : " Ali here"
            }
          ]
        }
      }
    ]
  }
}

我不想看到我的第一条记录,因为它在body、title、comment_text中不包含“ali”。有人能帮忙吗?我想如果它搜索“ali”,那么它将返回提要id title body以及注解id feed id和c_text,但不显示不包含ali的数组的内容。隐藏它

{
  "c_id" : "11",
  "feed_id" : "1",
  "c_text" : "get well soon mateen"
}

我不想看到这内容在我的输出

lvmkulzt

lvmkulzt1#

首先,按如下方式更新Map:

POST feeds/_mapping
{
  "properties": {
    "title": {
      "type": "text",
      "analyzer": "lowercase"
    },
    "body": {
      "type": "text",
      "analyzer": "lowercase"
    },
    "comment": {
      "type": "nested",
      "properties": {
        "c_text": {
          "type": "text",
          "analyzer": "lowercase"
        }
      }
    }
  }
}

然后运行此查询

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "title": {
              "query": "ali",
              "analyzer": "lowercase"
            }
          }
        },
        {
          "match": {
            "body": {
              "query": "ali",
              "analyzer": "lowercase"
            }
          }
        },
        {
          "nested": {
            "path": "comment",
            "query": {
              "match": {
                "comment.c_text": {
                  "query": "ali",
                  "analyzer": "lowercase"
                }
              }
            }
          }
        }
      ]
    }
  }
}

相关问题