弹性搜索正则表达式空间不起作用 [英] Elasticsearch regexp with space not working

查看:90
本文介绍了弹性搜索正则表达式空间不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一本带有 ElasticSearch 索引的标题的书籍,如下所示:

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

}'



我有一本名为Afoo barb的书。



以下代码(搜索 '。* foo。*')返回本书:

  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''。*'但它也不起作用。



我认为标题是分开的( ['Afoo','barb'] ),所以它找不到'。* foo bar。*'



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

解决方案


Elasticsearch将将正则表达式应用于该字段的tokenizer生成的条款,而不是字段的原始文本。


您可以使用不同的 tokenizer 用于索引您的字段或定义正则表达式,以便返回所需的高分数文档。



使用关键字tokenizer的示例:

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

Let's assume I have books with titles indexed with ElasticSearch as following:

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"}

}'

For instance, I have a book called "Afoo barb".

The following code (searching for '.*foo.*') returns well the book:

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

But the following code (searching for '.*foo bar.*') does not:

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

I tried to replace the space by '\s' or '.*' but it does not work either.

I think the title is separated in terms (['Afoo', 'barb']) so it can't find '.*foo bar.*'.

How can I ask Elasticsearch to search the regexp in the complete title ?

解决方案

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

You can use different tokenizer for indexing your fields or define the regex in such a way that it returns required documents with high score.

Example with keyword tokenizer:

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

这篇关于弹性搜索正则表达式空间不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆