搜索包含在一个值中的ElasticSearch字段 [英] Search ElasticSearch field contained in a value
本文介绍了搜索包含在一个值中的ElasticSearch字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图在ElasticSearch中运行类似的字段查询:
I'm trying to run similar field query in ElasticSearch:
从产品中选择' %'+名称+'%'
含义我正在尝试查找此案中产品名称的所有文件是一个牛奶的子字符串。
Meaning I'm trying to find all the documents that the product name in this case is a sub string of 'milk'.
我该怎么做?
推荐答案
我将使用自定义分析器,利用 ngrams 一>。首先创建一个这样的索引:
I would go with a custom analyzer leveraging ngrams. First create an index like this:
curl -XPUT 'localhost:9200/tests' -d '
{
"settings" : {
"analysis" : {
"analyzer" : {
"my_analyzer" : {
"tokenizer" : "ngrams"
}
},
"tokenizer" : {
"ngrams" : {
"type" : "nGram",
"min_gram" : "2",
"max_gram" : "10"
}
}
}
},
"mappings": {
"test": {
"properties": {
"product_name": {
"type": "string",
"analyzer": "standard",
"search_analyzer": "my_analyzer"
}
}
}
}
}'
然后你可以索引一些数据:
Then you can index some data:
curl -XPOST 'localhost:9200/tests/test/_bulk' -d '
{"index":{}}
{"product_name": "bc"}
{"index":{}}
{"product_name": "cd"}
{"index":{}}
{"product_name": "def"}
'
最后你可以这样搜索:
curl -XPOST 'localhost:9200/tests/_search' -d '{
"query": {
"match": {
"product_name": "abcde"
}
}
}'
您将获得前两个文档 bc
和 cd
And you'll get the first two documents bc
and cd
这篇关于搜索包含在一个值中的ElasticSearch字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文