space 的 Elasticsearch 正则表达式不起作用

Elasticsearch regexp with space not working

假设我有一些书的书名被 ElasticSearch 索引如下:

curl -XPUT "http://localhost:9200/_river/books/_meta" -d'
{
"type": "jdbc",
"jdbc": {
"driver": "org.postgresql.Driver",
"url": "jdbc:postgresql://localhost:5432/...",
"user": "...",
"password": "...",
"index": "books",
"type": "books",
"sql": "SELECT * FROM books"}

}'

例如,我有一本书叫 "Afoo barb"

以下代码(搜索'.*foo.*')returns好书:

client.search({
  index: 'books',
  'from': 0,
  'size': 10,
  'body' : {
    'query': {
      'filtered': {
         'filter': {
           'bool': {
              'must': {
                'regexp': { title: '.*foo.*' }
               }
            }
          }
        }
     }
  }
});

但是下面的代码(搜索'.*foo bar.*')没有:

client.search({
  index: 'books',
  'from': 0,
  'size': 10,
  'body' : {
    'query': {
      'filtered': {
         'filter': {
           'bool': {
              'must': {
                'regexp': { title: '.*foo bar.*' }
               }
            }
          }
        }
     }
  }
});

我尝试用 '\s''.*' 替换 space 但它也不起作用。

我认为标题是用术语 (['Afoo', 'barb']) 分隔的,所以找不到 '.*foo bar.*'

如何让 Elasticsearch 搜索完整标题中的正则表达式?

Elasticsearch will apply the regexp to the terms produced by the tokenizer for that field, and not to the original text of the field.

您可以使用不同的 tokenizer 来为您的字段编制索引,或者以 returns 需要高分文档的方式定义正则表达式。

关键字分词器示例:

'regexp': { title: '*(foo bar)*' }