使用Elasticsearch搜索部分单词或通配符

时间:2019-03-15 13:52:33

标签: elasticsearch

当我如下搜索时,我成功获得了结果。这对句子(或完整单词)也有效。但是,部分单词找不到任何东西。

例如,让我们看看这句话:

embedded image can place here.

当我搜索embedded时,它会找到此内容。但是embed找不到任何东西。

让我告诉你:

GET _search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "content": "Embedded"
          }
        }
      ],
      "filter": [
        {
          "term": {
            "user_id": 10
          }
        }
      ]
    }
  }
}

结果

{
  "took" : 1,
  "timed_out" : false,
  "_shards" : {
    "total" : 6,
    "successful" : 6,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 1,
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "inbox",
        "_type" : "mailbox",
        "_id" : "8c76f6a5-115a-4102-94e6-a3abef914d13",
        "_score" : 0.2876821,
        "_source" : {
          "user_id" : 10,
          "content" : "Embedded image"
        }
      }
    ]
  }
}

但是,只允许搜索单词embed

GET _search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "content": "Embed"
          }
        }
      ],
      "filter": [
        {
          "term": {
            "user_id": 10
          }
        }
      ]
    }
  }
}

结果:为空...

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 6,
    "successful" : 6,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : 0,
    "max_score" : null,
    "hits" : [ ]
  }
}

这样搜索时是否可以找到相关内容?请注意,当我搜索embed image

时,它也会找到
GET _search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "content": "embed image"
          }
        }
      ],
      "filter": [
        {
          "term": {
            "user_id": 10
          }
        }
      ]
    }
  }
}

1 个答案:

答案 0 :(得分:0)

我通过使用query_string

解决了这个问题
GET _search
{
  "query": {
    "bool": {
      "must": [
        {
          "query_string": {
            "query": "embed image",
            "fields": [
              "content"
            ]
          }
        }
      ],
      "filter": [
        {
          "term": {
            "user_id": 10
          }
        }
      ]
    }
  }
}