根据用户ID筛选全文搜索

时间:2019-03-15 10:16:15

标签: elasticsearch

GET _search
{
  "query": {
    "match": {
      "content": "this test"
    }
  }
}

这给了我以下结果:

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 6,
    "successful" : 6,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 3,
    "max_score" : 0.5753642,
    "hits" : [
      {
        "_index" : "inbox",
        "_type" : "mailbox",
        "_id" : "6bb174ab-a4ce-4409-a626-c9a42c98b89e",
        "_score" : 0.5753642,
        "_source" : {
          "user_id" : 13,
          "content" : "This is a test"
        }
      },
      {
        "_index" : "inbox",
        "_type" : "mailbox",
        "_id" : "1304cf2e-a1d4-40ca-9876-9abb08c4474d",
        "_score" : 0.36464313,
        "_source" : {
          "user_id" : 10,
          "content" : "This is a test"
        }
      },
      {
        "_index" : "inbox",
        "_type" : "mailbox",
        "_id" : "623c093c-4408-445e-abb1-460d2c5004cd",
        "_score" : 0.36464313,
        "_source" : {
          "user_id" : 15,
          "content" : "This is a test"
        }
      }
    ]
  }
}

哪个好。但是,我需要通过user_id对其进行过滤。我的意思是我只需要对特定用户及其内容进行评分。

GET _search
{
  "query": {
    "match": {
      "content": "this test",
      "user_id": 10
    }
  }
}

当我添加user_id时出现此错误:

{
  "error": {
    "root_cause": [
      {
        "type": "parsing_exception",
        "reason": "[match] query doesn't support multiple fields, found [content] and [user_id]",
        "line": 5,
        "col": 18
      }
    ],
    "type": "parsing_exception",
    "reason": "[match] query doesn't support multiple fields, found [content] and [user_id]",
    "line": 5,
    "col": 18
  },
  "status": 400
}

为什么?以及如何根据user_id进行适当过滤?

3 个答案:

答案 0 :(得分:1)

查询应如下所示:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "content": "this test"
          }
        },
        {
          "match": {
            "user_id": 10
          }
        }
      ]
    }
  }
}

答案 1 :(得分:1)

使用布尔查询来组合过滤器

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "content": "this is content"
          }
        },
        {
          "term": {
            "user_id": {
              "value": 47545
            }
          }
        }
      ]
    }
  }
}

答案 2 :(得分:1)

您可以使用term query通过user_id过滤结果。

如下更新查询:

{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "content": "this test"
          }
        }
      ],
      "filter": [
        {
          "term": {
            "user_id": 10
          }
        }
      ]
    }
  }
}