[英]how to search a document in elastic if two documents contains same word?
我有以下兩個彈性文件:
文檔- 1
{"name":"doc1","tags":["Hello World"]}
文檔-2
{"name":"doc2", "tags":["World"]}
我想在文檔中搜索包含“World”作為單個單詞的文檔,但此查詢返回兩個文檔:
{"query":{"bool":{"must":[{"match":{"tags":{"query":"World"}}}]}}}
我怎樣才能做到這一點?
如果映射具有keyword
for tags
字段,則可以使用它等
GET your_index/doc
{
"query": {
"bool": {
"must": [
{
"match": {
"tags.keyword": { // use `keyword` field here
"query": "World"
}
}
}
]
}
}
}
僅當您的映射具有如下所示時
{
"your_index": {
"mappings": {
"doc": {
"properties": {
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"tags": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword", // this mapping is required
"ignore_above": 256
}
}
}
}
}
}
}
}
你需要使用關鍵字分析器
curl -X PUT "http://localhost:9200/my_index" -H 'Content-Type: application/json' -d'
{
"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"type": "custom",
"tokenizer": "keyword"
}
}
}
},
"mappings": {
"_doc": {
"properties": {
"filedname": {
"type": "text",
"analyzer": "my_analyzer"
}
}
}
}
}'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.