簡體   English   中英

Elasticsearch 查詢多個術語

[英]Elasticsearch query for multiple terms

我正在嘗試創建一個允許按名稱和類型進行搜索的搜索查詢。 我已經索引了這些值,我在 Elasticsearch 中的記錄如下所示:

{
  _index: "assets",
  _type: "asset",
  _id: "eAOEN28BcFmQazI-nngR",
  _score: 1,
  _source: {
    name: "test.png",
    mediaType: "IMAGE",
    meta: {
      content-type: "image/png",
      width: 3348,
      height: 1890,
    },
    createdAt: "2019-12-24T10:47:15.727Z",
    updatedAt: "2019-12-24T10:47:15.727Z",
  }
}

那么我將如何創建例如一個查詢來查找所有名稱為“test”並且是圖像的資產?

我嘗試了 multi_mach 查詢,但沒有返回正確的結果:

{
  "query": {
    "multi_match" : {
      "query":      "*test* IMAGE",
      "type":       "cross_fields",
      "fields":     [ "name", "mediaType" ],
      "operator":   "and" 
    }
  }
}

上面的查詢返回 0 個結果,如果我將運算符更改為“或”,它將返回所有這些 IMAGE 類型的資產。

任何建議將不勝感激。 蒂亞!

編輯:添加映射下面是映射:

{
    "assets": {
        "aliases": {},
        "mappings": {
            "properties": {
                "__v": {
                    "type": "long"
                },
                "createdAt": {
                    "type": "date"
                },
                "deleted": {
                    "type": "date"
                },
                "mediaType": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "meta": {
                    "properties": {
                        "content-type": {
                            "type": "text",
                            "fields": {
                                "keyword": {
                                    "type": "keyword",
                                    "ignore_above": 256
                                }
                            }
                        },
                        "width": {
                            "type": "long"
                        },
                        "height": {
                          "type": "long"
                      }
                    }
                },
                "name": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "originalName": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "updatedAt": {
                    "type": "date"
                }
            }
        },
        "settings": {
            "index": {
                "creation_date": "1575884312237",
                "number_of_shards": "1",
                "number_of_replicas": "1",
                "uuid": "nSiAoIIwQJqXQRTyqw9CSA",
                "version": {
                    "created": "7030099"
                },
                "provided_name": "assets"
            }
        }
    }
}

對於這個簡單的查詢,您不需要使用通配符表達式。

首先,在name字段上更改您的分析器。

您需要創建一個自定義分析器來替換. 使用space作為默認標准分析器不會這樣做,因此您在搜索test會得到test.png因為倒排索引中將同時包含testpng 這樣做的主要好處是避免了非常昂貴的正則表達式查詢

使用自定義分析器更新映射,這將為您完成工作。 只需更新您的映射並重新索引所有文檔。

{
    "aliases": {},
    "mappings": {
        "properties": {
            "__v": {
                "type": "long"
            },
            "createdAt": {
                "type": "date"
            },
            "deleted": {
                "type": "date"
            },
            "mediaType": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "meta": {
                "properties": {
                    "content-type": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    },
                    "width": {
                        "type": "long"
                    },
                    "height": {
                        "type": "long"
                    }
                }
            },
            "name": {
                "type": "text",
                "analyzer" : "my_analyzer"
            },
            "originalName": {
                "type": "text",
                "fields": {
                    "keyword": {
                        "type": "keyword",
                        "ignore_above": 256
                    }
                }
            },
            "updatedAt": {
                "type": "date"
            }
        }
    },
    "settings": {
        "analysis": {
            "analyzer": {
                "my_analyzer": {
                    "tokenizer": "standard",
                    "char_filter": [
                        "replace_dots"
                    ]
                }
            },
            "char_filter": {
                "replace_dots": {
                    "type": "mapping",
                    "mappings": [
                        ". => \\u0020"
                    ]
                }
            }
        },
        "index": {
            "number_of_shards": "1",
            "number_of_replicas": "1"
        }
    }
}

其次,您應該將查詢更改為 bool 查詢,如下所示:

{
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "name": "test"
                    }
                },
                {
                    "match": {
                        "mediaType.keyword": "IMAGE"
                    }
                }
            ]
        }
    }
}

使用 must 和 2 個匹配查詢意味着,只有在 must 查詢的所有子句中都有匹配時,它才會返回文檔。

我已經通過創建索引、插入一些示例文檔並查詢它們來測試我的解決方案,如果您需要任何幫助,請告訴我。

你試過best_fields嗎?

{
  "query": {
    "multi_match" : {
      "query":      "Will Smith",
      "type":       "best_fields",
      "fields":     [ "name", "mediaType" ],
      "operator":   "and" 
    }
  }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM