假设我有这些数据
[
{
name: "Apple Company"
},
{
name: "Microsoft Company"
}
]
我想得到这样的查询结果:
查询1:
name: apple
结果1:
[
{
name: "Apple Company"
}
]
查询2:
name: app
结果2:
[
{
name: "Apple Company"
}
]
查询3:
name: apple com
结果3:
[
{
name: "Apple Company"
}
]
查询4:
name: apple company
结果4:
[
{
name: "Apple Company"
}
]
查询5:
name: company
结果5:
[
{
name: "Apple Company"
},
{
name: "Microsoft Company"
}
]
查询6:
name: ap com
结果6:
[]
如何在elasticsearch中做到这一点?
2条答案
按热度按时间uyto3xhc1#
您可以使用elasticsearch中的Shingle Token filter和前缀查询,这将涵盖您提到的所有场景:
索引Map:
查询:
iq0todco2#
您可以使用
query_string
来搜索数据。通过Kibana DevTools为您的数据建立索引。
使用query_string搜索数据。
您还可以使用match_bool_prefix查询
查询字符串的引用:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html
自动完成参考:https://opster.com/guides/elasticsearch/how-tos/elasticsearch-auto-complete-guide/
下面是match_bool_prefix查询的结果。