簡體   English   中英

如何在彈性搜索中匹配每個數組項的多個字段

[英]How to match on multiple fields per array item in elastic search

我正在嘗試創建彈性搜索查詢以匹配數組內對象內的多個字段。

例如,我查詢的彈性搜索結構類似於以下內容:

"hits": [
            {
                "_index": "titles",
                "_type": "title",
                ...
                "_source": {
                    ...
                    "genres": [
                        {
                            "code": "adventure",
                            "priority": 1
                        },
                        {
                            "code": "action",
                            "priority": 2
                        },
                        {
                            "code": "horror",
                            "priority": 3
                        }
                    ],
                    ...
                },
                ...
        ]

而我想要做的是匹配具有特定類型/優先級配對的標題。 例如,我試圖匹配所有標題與code=actionpriority=1 ,但我的查詢返回太多結果。 在該示例中,由於類型列表包含具有code=action類型和匹配priority=1另一類型,因此上述標題被命中。 我的查詢類似於以下內容:

    "query": {
        "bool": {
            "filter": [
                {
                    "bool": {
                        "must":[
                            {"term": {
                                    "genres.code": {
                                        "value": "action",
                                        "boost": 1.0
                                    }
                            }},
                            {"term": {
                                    "genres.priority": {
                                        "value": 1,
                                        "boost": 1.0
                                    }
                            }}
                        ]
                    }                    
                },
                ...
            }

有沒有辦法形成查詢,以便將標題與包含priority=1 AND code=action的單一類型相匹配?

我重新創建了你的問題。 我添加了以下映射

PUT titles 
{
  "mappings": {
    "title": {
      "properties": {
        "author": {
          "type": "text"
        },
        "genres": {
          "type": "nested"
        }
      }
    }
  }
}

然后我將值添加到索引中。 這是插入的內容

  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "titles",
        "_type": "title",
        "_id": "2",
        "_score": 1,
        "_source": {
          "author": "Author 1",
          "genres": [
            {
              "code": "adventure",
              "priority": 2
            },
            {
              "code": "action",
              "priority": 3
            },
            {
              "code": "horror",
              "priority": 1
            }
          ]
        }
      },
      {
        "_index": "titles",
        "_type": "title",
        "_id": "1",
        "_score": 1,
        "_source": {
          "author": "Author 2",
          "genres": [
            {
              "code": "adventure",
              "priority": 3
            },
            {
              "code": "action",
              "priority": 1
            },
            {
              "code": "horror",
              "priority": 2
            }
          ]
        }
      },
      {
        "_index": "titles",
        "_type": "title",
        "_id": "3",
        "_score": 1,
        "_source": {
          "author": "Author 3",
          "genres": [
            {
              "code": "adventure",
              "priority": 3
            },
            {
              "code": "action",
              "priority": 1
            },
            {
              "code": "horror",
              "priority": 2
            }
          ]
        }
      }
    ]
  }

我的查詢是:

GET titles/title/_search 
{
  "query": {
    "nested": {
      "path": "genres",
      "query": {
        "bool": {
          "must": [
            {
              "term": {
                "genres.code": {
                  "value": "horror"
                }
              }
            },
            {
              "term": {
                "genres.priority": {
                  "value": 1
                }
              }
            }
          ]
        }
      }
    }
  }
}

查詢返回

        "_source": {
          "author": "Author 1",
          "genres": [
            {
              "code": "adventure",
              "priority": 2
            },
            {
              "code": "action",
              "priority": 3
            },
            {
              "code": "horror",
              "priority": 1
            }
          ]
        }

此標題是唯一具有code ='horror'且優先級= 1的標題。

暫無
暫無

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

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