【发布时间】:2017-04-26 21:28:04
【问题描述】:
我正在尝试为最终用户提供搜索时使用的类型,这更像是 sqlserver。 我能够为给定的 sql 场景实现 ES 查询:
select * from table where name like '%pete%' and type != 'xyz and type!='abc'
但是 ES 查询不适用于这个 sql 查询
select * from table where name like '%peter tom%' and type != 'xyz and type!='abc'
在我的弹性搜索以及通配符查询中,我还需要执行一些布尔过滤查询
{
"query": {
"filtered": {
"filter": {
"bool": {
"should": [
{
"query": {
"wildcard": {
"name": { "value": "*pete*" }
}
}
}
],
"must_not": [
{
"match": { "type": "xyz" }
},
{
"match": { "type": "abc" }
}
]
}
}
}
}
}
上面带有通配符搜索的弹性查询可以正常工作,并为我获取所有与 pete 匹配且不属于 xyz 和 abc 类型的文档。但是当我尝试使用由空格分隔的 2 个单独的单词执行通配符时,相同的查询返回给我如下图为空。例如
{
"query": {
"filtered": {
"filter": {
"bool": {
"should": [
{
"query": {
"wildcard": {
"name": { "value": "*peter tom*" }
}
}
}
],
"must_not": [
{
"match": { "type": "xyz" }
},
{
"match": { "type": "abc" }
}
]
}
}
}
}
}
我的映射如下:
{
"properties": {
"name": {
"type": "string"
},
"type": {
"type": "string"
}
}
}
我应该使用什么查询来使通配符搜索由空格分隔的单词成为可能
【问题讨论】:
-
你的问题是你不明白ES如何索引数据。看看这个elastic.co/guide/en/elasticsearch/guide/current/…。也请看 ngGram elastic.co/guide/en/elasticsearch/reference/current/…
-
所以问题是当你在 ES 中索引文本 "hello world" 时,它会变成 ["hello", "world"]。
-
我了解文档是如何存储在 ES 中的,但是 ES 提供了什么来允许用户执行 sql,例如搜索由空格分隔的单词是我的问题
-
你需要喜欢还是需要找到类似的?因此,例如,如果用户搜索“piter tom”,如果您还显示“peter tom”就可以了吗?
-
我希望双方都进行 LIKE 操作。即像我们在 sql 中所做的 '%peter to%'