我正在使用Elasticsearch搜索模板 mustache 语言
搜索“Joe Gray”将返回名为“Joe Gray”的任何人,或者搜索“J Gray”将返回“Joe Gray”。
但是搜索“Joe Gray”不会以“J Gray”返回任何名称
如何在我的 mustache 查询中使用分析器来实现此目的。
最佳答案
不太清楚您的索引映射是什么样的。我这里有两个例子:
1个名称包含在一个字段中:
PUT t2/doc/1
{
"name":"Joe Gray"
}
PUT t2/doc/2
{
"name":"J Gray"
}
POST t2/_search
{
"query": {
"match": {
"name": "j gray"
}
}
}
##Result
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.51623213,
"hits": [
{
"_index": "t2",
"_type": "doc",
"_id": "2",
"_score": 0.51623213,
"_source": {
"name": "J Gray"
}
},
{
"_index": "t2",
"_type": "doc",
"_id": "1",
"_score": 0.25811607,
"_source": {
"name": "Joe Gray"
}
}
]
}
}
2如果您将名称作为两个单独的字段(根据我在注释中看到的),则可以将
bool
查询与should
子句一起使用:PUT t3/doc/1
{
"firstname":"Joe",
"lastname":"Gray"
}
PUT t3/doc/2
{
"firstname":"J",
"lastname":"Gray"
}
POST t3/_search
{
"query": {
"bool": {
"should": [
{
"match": {
"firstname": "J"
}
},
{
"match": {
"lastname": "Gray"
}
}
]
}
}
}
## Result
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.5753642,
"hits": [
{
"_index": "t3",
"_type": "doc",
"_id": "2",
"_score": 0.5753642,
"_source": {
"firstname": "J",
"lastname": "Gray"
}
},
{
"_index": "t3",
"_type": "doc",
"_id": "1",
"_score": 0.2876821,
"_source": {
"firstname": "Joe",
"lastname": "Gray"
}
}
]
}
}
关于elasticsearch - Elasticsearch将名称与首字母匹配,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55936641/