我有一個包含兩個欄位html和url以下映射的彈性搜索索引:
{
"mappings": {
"properties": {
"html": {
"type": "text",
"fields": { "keyword": { "type": "keyword", "ignore_above": 256 } }
},
"url": {
"type": "text",
"fields": { "keyword": { "type": "keyword", "ignore_above": 256 } }
}
}
}
}
通過 url 檢索檔案的最佳方法是什么?例如,我想要 url 欄位包含 的檔案google.com。結果可能是帶有 urlshttps://www.google.com和www.google.com/search. 我嘗試了不同的查詢,但似乎并非一直有效。
query = {
"query" : {
"match_phrase" : {
"url" : f"google.com"
}
}
}
response = elasticsearch.helpers.scan(
es_client,
index=my_index,
doc_type="_doc",
query=query
)
uj5u.com熱心網友回復:
TLDR;
您應該使用keyword欄位而不是text欄位。
query = {
"query" : {
"match" : {
"url.keyword" : f"google.com"
}
}
}
response = elasticsearch.helpers.scan(
es_client,
index=my_index,
doc_type="_doc",
query=query)
但請記住,這將進行完全匹配,在 google.com
重現
創建索引并添加資料
PUT /so_search_url/
{
"mappings": {
"properties": {
"html": {
"type": "text",
"fields": { "keyword": { "type": "keyword", "ignore_above": 256 } }
},
"url": {
"type": "text",
"fields": { "keyword": { "type": "keyword", "ignore_above": 256 } }
}
}
}
}
POST /so_search_url/_doc
{
"html": "<h1>Plop</h1>",
"url": "https://www.google.com"
}
POST /so_search_url/_doc
{
"html": "<h1>Plop</h1>",
"url": "https://www.google.fr"
}
POST /so_search_url/_doc
{
"html": "<h1>Plop</h1>",
"url": "https://www.google.com/search"
}
搜索完全匹配的資料
GET /so_search_url/_search
{
"query": {
"match": {
"url.keyword": "https://www.google.com"
}
}
}
搜索前綴匹配的資料
GET /so_search_url/_search
{
"query": {
"prefix": {
"url.keyword": {
"value": "https://www.google.com"
}
}
}
}
了解
...兩種新型別:文本,應該用于全文搜索,以及關鍵字,應該用于關鍵字搜索。
[檔案]
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/408457.html
標籤:
