我试图将搜索提供给最终用户的类型,因为它们更像是SQLServer。我能够针对给定的SQL方案实现ES查询:
select * from table where name like '%peter tom%' and type != 'xyz
在ES中,我使用ngram tokenizer来获得所需的结果:
PUT sample
{
"settings": {
"analysis": {
"analyzer": {
"my_ngram_analyzer": {
"tokenizer": "my_ngram_tokenizer"
}
},
"tokenizer": {
"my_ngram_tokenizer": {
"type": "nGram",
"min_gram": "2",
"max_gram": "15"
}
}
}
},
"mappings": {
"typename": {
"properties": {
"name": {
"type": "string",
"fields": {
"search": {
"type": "string",
"analyzer": "my_ngram_analyzer"
}
}
},
"type": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
{
"query": {
"bool": {
"should": [
{
"term": {
"name.search": "peter tom"
}
}
],
"must_not": [
{
"match": {
"type": "xyz"
}
},
{
"match": {
"type": "abc"
}
}
]
}
}
}
所以如果我的文档行就像
name type
peter tomson efg
Peter tomson robert simson efg
上面的查询仅显示既是文档,但是当我尝试输入Peter Sims或Peter Simson时,除非我输入Peter Tomson Robert Sims或Peter Tomson Robert Simson。彼得之后和西姆森之前的所有单词都可以到达第二个文档。有什么方法可以将第二个文档与部分匹配。我可以使用查询匹配和"one_answers"操作,但仍然完全匹配。我正在寻找部分匹配,例如Peter Sims,应该给我第二行文件。谢谢
我找到了查询自己的答案,该解决方案发布了解决方案以供其他用户参考:
{
"settings": {
"analysis": {
"analyzer": {
"autocomplete": {
"tokenizer": "whitespace",
"filter": [
"lowercase",
"autocomplete"
]
},
"autocomplete_search": {
"tokenizer": "whitespace",
"filter": [
"lowercase"
]
}
},
"filter": {
"autocomplete": {
"type": "nGram",
"min_gram": 2,
"max_gram": 40
}
}
}
},
"mappings": {
"doc": {
"properties": {
"title": {
"type": "string",
"analyzer": "autocomplete",
"search_analyzer": "autocomplete_search"
}
}
}
}
}
PUT my_index/doc/1
{
"title": "peter tomson"
}
PUT my_index/doc/2
{
"title": "Peter tomson robert simson"
}
GET my_index/doc/_search
{
"query": {
"match": {
"title": {
"query": "Pete sim",
"operator": "and"
}
}
}
}