[英]Elasticsearch search with multi fields
我如何像這樣為彈性搜索創建身體
select * from table where full_name like '%q%' or address like '%q%' or description like '%q%' order by full_name , description , address
通配符查詢可能非常昂貴,尤其是在多個字段中搜索時。 正確的方法是在僅要搜索一部分的字段上使用nGram
令牌過濾器 。
首先,使用自定義分析器創建如下所示的索引,該索引會將您的字段切成小塊並切成可搜索的標記:
curl -XPUT localhost:9200/tests -d '{
"settings": {
"analysis": {
"analyzer": {
"substring_analyzer": {
"tokenizer": "standard",
"filter": ["lowercase", "substring"]
}
},
"filter": {
"substring": {
"type": "nGram",
"min_gram": 1,
"max_gram": 15
}
}
}
},
"mappings": {
"test": {
"properties": {
"full_name": {
"type": "string",
"analyzer": "substring_analyzer"
},
"address": {
"type": "string",
"analyzer": "substring_analyzer"
},
"description": {
"type": "string",
"analyzer": "substring_analyzer"
}
}
}
}
}'
然后,您可以索引一些文檔:
curl -XPUT localhost:9200/tests/test/_bulk -d '
{"index":{"_id": 1}}
{"full_name": "Doe", "address": "1234 Quinn Street", "description": "Lovely guy"}
{"index":{"_id": 2}}
{"full_name": "Brennan", "address": "4567 Main Street", "description": "Not qualified"}
{"index":{"_id": 3}}
{"full_name": "Quantic", "address": "1234 Quinn Street", "description": "New friend"}
'
最后,您可以使用與上述SQL查詢等效的查詢進行搜索,並且所有三個測試文檔都將匹配:
curl -XPUT localhost:9200/tests/test/_search -d '{
"query": {
"bool": {
"should": [
{
"match": {
"full_name": "q"
}
},
{
"match": {
"address": "q"
}
},
{
"match": {
"description": "q"
}
}
]
}
}
}'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.