如何获得包含 elasticsearch-dsl 查询关键字的所有结果?

How can I get all results containing elasticsearch-dsl query keyword?

当我查询我的 PostDocument 时,它 return 的结果只包含查询中的完整单词。例如,如果有 4 个帖子:

1. "Post 1"
2. "Post 2"
3. "Posts 3"
4. "Po 4"

我查询它:posts = PostDocument.search().query('match', body="Post")它会return项目1和2,如果body="Po"它会return只有项目4。我怎么写查询所以 return 包含关键字的所有结果?例如,如果我这样做 body="Po" 我会得到所有 4 件物品。

You can use the edge_ngram tokenizer first breaks text down into words whenever it encounters one of a list of specified characters, then it emits N-grams of each word where the start of the N-gram is anchored to the beginning of the word.

添加包含索引数据、映射、搜索查询和搜索结果的工作示例

索引映射:

{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "my_tokenizer"
        }
      },
      "tokenizer": {
        "my_tokenizer": {
          "type": "edge_ngram",
          "min_gram": 2,
          "max_gram": 10,
          "token_chars": [
            "letter",
            "digit"
          ]
        }
      }
    },
    "max_ngram_diff": 50
  },
  "mappings": {
    "properties": {
      "body": {
        "type": "text",
        "analyzer": "my_analyzer"
      }
    }
  }
}

索引数据:

{
  "body": "Post 1"
}
{
  "body": "Post 2"
}
{
  "body": "Posts 3"
}
{
  "body": "Po 4"
}

搜索查询:

{
    "query": {
        "match": {
            "body": "Po"
        }
    }
}

搜索结果:

"hits": [
      {
        "_index": "64684245",
        "_type": "_doc",
        "_id": "4",
        "_score": 0.1424427,
        "_source": {
          "body": "Po 4"
        }
      },
      {
        "_index": "64684245",
        "_type": "_doc",
        "_id": "1",
        "_score": 0.10158265,
        "_source": {
          "body": "Post 1"
        }
      },
      {
        "_index": "64684245",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.10158265,
        "_source": {
          "body": "Post 2"
        }
      },
      {
        "_index": "64684245",
        "_type": "_doc",
        "_id": "3",
        "_score": 0.088840574,
        "_source": {
          "body": "Posts 3"
        }
      }
    ]