Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex ElasticSearch正则表达式查询不';行不通_Regex_<img Src="//i.stack.imgur.com/RUiNP.png" Height="16" Width="18" Alt="" Class="sponsor Tag Img">elasticsearch - Fatal编程技术网 elasticsearch,Regex,elasticsearch" /> elasticsearch,Regex,elasticsearch" />

Regex ElasticSearch正则表达式查询不';行不通

Regex ElasticSearch正则表达式查询不';行不通,regex,elasticsearch,Regex,elasticsearch,我将ES 2.4.6与Java 8一起使用,并创建了一个document对象,如下所示: @Document(indexName = "airports", type = "airport") public class Airport { @Id private String id; @Field(type = String) private String name; } { "took": 2, "timed_out": false, "_shards": {

我将ES 2.4.6与Java 8一起使用,并创建了一个document对象,如下所示:

@Document(indexName = "airports", type = "airport")
public class Airport {

  @Id
  private String id;

  @Field(type = String)
  private String name;
}
{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 1,
    "hits": [
      {
        "_index": "airports",
        "_type": "airport",
        "_id": "SSMlsTWIYefbXHCnYEwEY",
        "_score": 1,
        "_source": {
          "id": "SSMlsTWIYefbXHCnYEwEY",
          "name": "Santiago"
        }
      },
      {
        "_index": "airports",
        "_type": "airport",
        "_id": "LlDcKuywPjURNeIISjXLjC",
        "_score": 1,
        "_source": {
          "id": "LlDcKuywPjURNeIISjXLjC",
          "name": "San Mateo"
        }
      },
      {
        "_index": "airports",
        "_type": "airport",
        "_id": "CVIjEHYphSmZIjYbHCMwtkqfKWtEHVh",
        "_score": 1,
        "_source": {
          "id": "CVIjEHYphSmZIjYbHCMwtkqfKWtEHVh",
          "name": "San Francisco"
        }
      },
      {
        "_index": "airports",
        "_type": "airport",
        "_id": "gbntKR",
        "_score": 1,
        "_source": {
          "id": "gbntKR",
          "name": "Palo Alto"
        }
      },
      {
        "_index": "airports",
        "_type": "airport",
        "_id": "bKosUdHeseMMboyaejv",
        "_score": 1,
        "_source": {
          "id": "bKosUdHeseMMboyaejv",
          "name": "Big San"
        }
      }
    ]
  }
}
我成功地搜索了几个机场对象到ES,如下所示 名称:“旧金山”、“圣马特奥”、“圣地亚哥”、“帕洛阿尔托”、“大旧金山” ES中的JSON内容如下所示:

@Document(indexName = "airports", type = "airport")
public class Airport {

  @Id
  private String id;

  @Field(type = String)
  private String name;
}
{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 5,
    "max_score": 1,
    "hits": [
      {
        "_index": "airports",
        "_type": "airport",
        "_id": "SSMlsTWIYefbXHCnYEwEY",
        "_score": 1,
        "_source": {
          "id": "SSMlsTWIYefbXHCnYEwEY",
          "name": "Santiago"
        }
      },
      {
        "_index": "airports",
        "_type": "airport",
        "_id": "LlDcKuywPjURNeIISjXLjC",
        "_score": 1,
        "_source": {
          "id": "LlDcKuywPjURNeIISjXLjC",
          "name": "San Mateo"
        }
      },
      {
        "_index": "airports",
        "_type": "airport",
        "_id": "CVIjEHYphSmZIjYbHCMwtkqfKWtEHVh",
        "_score": 1,
        "_source": {
          "id": "CVIjEHYphSmZIjYbHCMwtkqfKWtEHVh",
          "name": "San Francisco"
        }
      },
      {
        "_index": "airports",
        "_type": "airport",
        "_id": "gbntKR",
        "_score": 1,
        "_source": {
          "id": "gbntKR",
          "name": "Palo Alto"
        }
      },
      {
        "_index": "airports",
        "_type": "airport",
        "_id": "bKosUdHeseMMboyaejv",
        "_score": 1,
        "_source": {
          "id": "bKosUdHeseMMboyaejv",
          "name": "Big San"
        }
      }
    ]
  }
}
然后我使用下面的curl命令使用regex查询来查找所有机场 名字以“san”开头忽略大小写,我做到了:

curl -XGET 'localhost:9200/airports/airport/_search?pretty' -H 'Content-Type: application/json' -d'
{
    "query": {
        "regexp":{
            "name": "^(?i)san"
        }
    }
}
'
我使用regex“^(?I)san”直接匹配这些机场名称, 它的工作原理如下:

String regex = "^(?i)san";
assertTrue("San Francisco".matches(regex));
assertTrue("San Mateo".matches(regex));
assertTrue("Santiago".matches(regex));
assertTrue(!"Big San".matches(regex));
那么有人知道为什么ES正则表达式查询返回空结果吗?现在,如果 我使用“san”作为正则表达式,所有4个名称都返回,如果我使用“san”,则不会返回任何内容。

您可以使用它解决上述问题

 {
  "query": {
    "match_phrase_prefix": {
       "name": "San"
      }
    }
 }
看看它是否能解决您的问题。

您可以使用它解决上述问题

 {
  "query": {
    "match_phrase_prefix": {
       "name": "San"
      }
    }
 }


查看它是否解决了您的问题。

如果您想获取以
san
san
开头的名称,请尝试
“名称”:“[sS]an.*”
它不起作用。我只想要以“san”开头的名字忽略大小写。为什么普通的java正则表达式在这里不起作用?在实际生产中,“san”将是传入的运行时参数。我更喜欢“^(?I)”+name+“*”作为正则表达式,您不能期望Lucene风格的ES正则表达式支持java.util.regex。它的模式总是锚定的,您不应该使用
^
<代码>(?i)在Lucene regex flavor中不受支持<代码>“名称”:“[sS][aA][nN].*”是您使用该正则表达式的唯一选项。当我使用“[sS]an.*”时,返回了“大San”。首先,根据ES文档,我必须使用锚点“^”:这意味着该模式不是由ES(Lucene)正则表达式引擎处理的。根据您提供的文档,Lucene的模式总是固定的。提供的模式必须与整个字符串匹配。而且
^
不应该在那里。参见示例。这意味着您的问题现在还不清楚。如果您想获取以
san
san
开头的名称,请尝试
“名称”:“[sS]an.*”
它不起作用。我只想要以“san”开头的名字忽略大小写。为什么普通的java正则表达式在这里不起作用?在实际生产中,“san”将是传入的运行时参数。我更喜欢“^(?I)”+name+“*”作为正则表达式,您不能期望Lucene风格的ES正则表达式支持java.util.regex。它的模式总是锚定的,您不应该使用
^
<代码>(?i)在Lucene regex flavor中不受支持<代码>“名称”:“[sS][aA][nN].*”是您使用该正则表达式的唯一选项。当我使用“[sS]an.*”时,返回了“大San”。首先,根据ES文档,我必须使用锚点“^”:这意味着该模式不是由ES(Lucene)正则表达式引擎处理的。根据您提供的文档,Lucene的模式总是固定的。提供的模式必须与整个字符串匹配。而且
^
不应该在那里。参见示例。这意味着你的问题现在还不清楚。我以前试过这个问题。它不起作用。它返回空列表。我试过了,它成功了。。索引的映射是什么?你用的是什么ES版本?请看问题的顶部。对不起,我错过了。因此,
name
属于
String
类型,默认情况下,
analysisted
。所以理想情况下,上述查询应该可以工作。我有相同的映射,它正在工作请参见我的命令行结果:curl-XGET'localhost:9200/airports/airport/\u search?pretty'-H'内容类型:application/json'-d'>{>“query”:{>“match_短语前缀”:{>“name”:“san”>>}>}>}>{“take”:2,“timed_out”:false,{“shards”:{“total”:5,“successful”:5,“failed”:0},“hits”:{“total”:0,“max_score”:null,“hits”:[]}我以前尝试过这个查询。它不起作用。它返回空列表。我试过了,它成功了。。索引的映射是什么?你用的是什么ES版本?请看问题的顶部。对不起,我错过了。因此,
name
属于
String
类型,默认情况下,
analysisted
。所以理想情况下,上述查询应该可以工作。我有相同的映射,它正在工作请参见我的命令行结果:curl-XGET'localhost:9200/airports/airport/\u search?pretty'-H'内容类型:application/json'-d'>{>“query”:{>“match_短语前缀”:{>“name”:“san”>>}>}>}>{“take”:2,“timed_out”:false,{“shards”:{“total”:5,“成功”:5,“失败”:0},“命中”:{“总计”:0,“最大分数”:null,“命中”:[]}