字符串类型的 Elasticsearch 搜索问题
Elasticsearch searching issues on string type
我在搜索索引时遇到问题。这是我创建的索引:
curl -XPUT localhost:9200/my_supder_index -d '{
"mappings":
{
"doctype_I_index":
{
"properties":
{
"field_I_index":
{
"type":"string",
"term_vector":"yes"
}
}
}
}
}'
这是该索引中的示例内容:
{
_index:"my_super_index",
_type:"doctype_I_index",
_id:"676078",
_version:1,
found:true,
_source:{
created:"2015-05-02T00:24:03",
field_I_index:[
"21E0",
"19E0",
"5E0",
"6E0",
"4E0"
],
id:676078
}
}
现在当我这样搜索时:
curl -XGET 'http://127.0.0.1:9200/my_super_index/_search' -d '{
"sort":[
{
"created":{
"order":"desc"
}
}
],
"query":{
"bool":{
"must":[
{
"terms":{
"field_I_index":[
"21E0"
],
"minimum_should_match":1
}
}
]
}
}
}'
我得到零结果。它没有对文本进行匹配。谁能给我指明正确的方向?
检查此值的分析方式后,结果如下 -
curl -XPOST 'localhost:9200/news/_analyze?pretty' -d '21E0'
{
"tokens" : [ {
"token" : "21e0",
"start_offset" : 0,
"end_offset" : 4,
"type" : "<ALPHANUM>",
"position" : 1
} ]
}
在这里你可以看到文字是小写的。
此外,由于 term query 没有对搜索文本应用分析器,它会查找 21E0 的精确匹配,但索引的是 21e0。
因此在这种情况下,如果您使用 match query 而不是术语查询,它应该可以工作。
但我建议在字段中使用 not_analyzed,然后在此基础上使用术语查询。这可能是更好的方法。
我在搜索索引时遇到问题。这是我创建的索引:
curl -XPUT localhost:9200/my_supder_index -d '{
"mappings":
{
"doctype_I_index":
{
"properties":
{
"field_I_index":
{
"type":"string",
"term_vector":"yes"
}
}
}
}
}'
这是该索引中的示例内容:
{
_index:"my_super_index",
_type:"doctype_I_index",
_id:"676078",
_version:1,
found:true,
_source:{
created:"2015-05-02T00:24:03",
field_I_index:[
"21E0",
"19E0",
"5E0",
"6E0",
"4E0"
],
id:676078
}
}
现在当我这样搜索时:
curl -XGET 'http://127.0.0.1:9200/my_super_index/_search' -d '{
"sort":[
{
"created":{
"order":"desc"
}
}
],
"query":{
"bool":{
"must":[
{
"terms":{
"field_I_index":[
"21E0"
],
"minimum_should_match":1
}
}
]
}
}
}'
我得到零结果。它没有对文本进行匹配。谁能给我指明正确的方向?
检查此值的分析方式后,结果如下 -
curl -XPOST 'localhost:9200/news/_analyze?pretty' -d '21E0'
{
"tokens" : [ {
"token" : "21e0",
"start_offset" : 0,
"end_offset" : 4,
"type" : "<ALPHANUM>",
"position" : 1
} ]
}
在这里你可以看到文字是小写的。 此外,由于 term query 没有对搜索文本应用分析器,它会查找 21E0 的精确匹配,但索引的是 21e0。
因此在这种情况下,如果您使用 match query 而不是术语查询,它应该可以工作。
但我建议在字段中使用 not_analyzed,然后在此基础上使用术语查询。这可能是更好的方法。