[英]elastic search match query over array object
假設我有 3 個文檔
doc_1 = {
"citedIn": [
"Bar Councils Act, 1926 - Section 15",
"Contract Act, 1872 - Section 23"
]
}
doc_2 = {
"citedIn":[
"15 C. B 400",
"Contract Act, 1872 - Section 55"
]
}
doc_3 = {
"citedIn":[
"15 C. B 400",
"Contract Act, 1872 - Section 15"
]
}
這里被citedIn
字段是一個數組對象。現在我想運行一個標准match
查詢
{
"query":
{
"match": {"citedIn":{"query": "Contract act 15" , "operator":"and" }}
}
}
上面的查詢返回所有 3 doc,但它假設返回doc_3
因為只有doc_3
包含Contract
, act
和15
一起在單個數組 element 中。
我將如何實現這一目標?
任何建議/解決方案都會更可取
嵌套數據類型更新:
我確實嘗試過嵌套字段。 這是我的映射
{
"mappings": {
"properties": {
"citedIn": {
"type": "nested",
"include_in_parent": true,
"properties": {
"someFiled": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}
}
}
這是我的數據
doc_1 = {
"citedIn": [
{"someFiled" : "Bar Councils Act, 1926 - Section 15"},
{"someFiled" : "Contract Act, 1872 - Section 23"}
]
}
doc_2 = {
"citedIn":[
{"someFiled" : "15 C. B 400"}
{"someFiled" : "Contract Act, 1872 - Section 55"}
]
}
doc_3 = {
"citedIn":[
{"someFiled" : "15 C. B 400"},
{"someFiled" : "Contract Act, 1872 - Section 15"}
]
}
這是我的查詢
{
"query":
{
"match": {"citedIn.someFiled":{"query": "Contract act 15" , "operator":"and" }}
}
}
但仍然得到相同的結果
您無法實現這一點,因為您要索引的是一個指向citedIn
字段中字符串的數組,並且所有 Elasticsearch 字段默認都是多值的,因為它是在 Lucene 中以這種方式設計的,並且 elasticsearch 構建在頂部Lucene 搜索庫。
請閱讀elasticsearch 中的數組以獲取更多信息,尤其是最后一個重要說明,如下圖所示:
如上圖所述,數組中的所有字符串實際上都是同一字段的一部分,因此 ES 無法識別您的搜索字符串是否屬於數組中相同字符串的一部分,因此您獲得了所有文檔正在搜索。
除非您將這些字符串作為其他字段(如nested
字段)的一部分進行索引,但為此,您需要提供字段名稱及其像映射,其中鍵是您的字段名稱,值是字段值,然后您查詢字段名稱,您將無法實現您的用例。
添加包含索引數據、映射、搜索查詢和搜索結果的工作示例。
您需要使用嵌套查詢來搜索嵌套字段
索引映射
{
"mappings": {
"properties": {
"citedIn": {
"type": "nested"
}
}
}
}
指數數據:
{
"citedIn": [
{
"someFiled": "Bar Councils Act, 1926 - Section 15"
},
{
"someFiled": "Contract Act, 1872 - Section 23"
}
]
}
{
"citedIn": [
{
"someFiled": "15 C. B 400"
},
{
"someFiled": "Contract Act, 1872 - Section 55"
}
]
}
{
"citedIn": [
{
"someFiled": "15 C. B 400"
},
{
"someFiled": "Contract Act, 1872 - Section 15"
}
]
}
搜索查詢:
{
"query": {
"nested": {
"path": "citedIn",
"query": {
"bool": {
"must": [
{
"match": {
"citedIn.someFiled": "contract"
}
},
{
"match": {
"citedIn.someFiled": "act"
}
},
{
"match": {
"citedIn.someFiled": 15
}
}
]
}
},
"inner_hits": {}
}
}
}
搜索結果:
"inner_hits": {
"citedIn": {
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.620718,
"hits": [
{
"_index": "stof_64170705",
"_type": "_doc",
"_id": "3",
"_nested": {
"field": "citedIn",
"offset": 1
},
"_score": 1.620718,
"_source": {
"someFiled": "Contract Act, 1872 - Section 15"
}
}
]
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.