Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Jira Legacy
serverSystem JiraJIRA
columnIdsissuekey,summary,issuetype,created,updated,duedate,assignee,reporter,priority,status,resolution
columnskey,summary,type,created,updated,due,assignee,reporter,priority,status,resolution
serverId01505d01-b853-3c2e-90f1-ee9b165564fc
keyMSEARCH-426


...

Code Block
languagetext
{
  "group" :        "fans",
  "user.first" : [ "alice", "john" ],
  "user.last" :  [ "smith", "white" ]
}

 The user.first and user.last fields are flattened into multi-value fields and the association between alice and white is lost. This document would incorrectly match a query for alice AND smith.

Related documentation: Nested field type | Elasticsearch Guide [8.4] | Elastic

Solution

Using nested fields for arrays of objects.

...

Code Block
languagetext
titleNested
{
  "query": {
    "nested": {
      "path": "user",
      "query": {
        "bool": {
          "must": [
            { "match": { "user.first": "Alice" }},
            { "match": { "user.last":  "Smith" }} 
          ]
        }
      }
    }
  }
}


Note
Because nested documents are indexed as separate documents, they can only be accessed within the scope of the nested query


Info
All fields in the nested object are also could be added to the parent document as standard (flat) fields by using include_in_parent parameter.


Testing

Changes in Index mappingsIndexed instancesAmount of documentsTotal index sizeAvg size per instanceContributor searchKeyword search
Current 8266332826633225850326050B ~ 25,8GB3127


Expand


Code Block
{
    "query": {
        "multi_match": {
            "query": "Pavlo Smahin",
            "fields": [
                "contributors.name^1.0"
            ],
            "type": "cross_fields",
            "operator": "AND"
        }
    }
}




Expand


Code Block
{
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "semantic",
            "fields": [
              "alternativeTitles.alternativeTitle.*^1.0",
              "contributors.name^1.0",
              "identifiers.value^1.0",
              "indexTitle.*^1.0",
              "series.*^1.0",
              "title.*^1.0"
            ],
            "type": "best_fields",
            "operator": "OR"
          }
        },
        {
          "multi_match": {
            "query": "web",
            "fields": [
              "alternativeTitles.alternativeTitle.*^1.0",
              "contributors.name^1.0",
              "identifiers.value^1.0",
              "indexTitle.*^1.0",
              "series.*^1.0",
              "title.*^1.0"
            ],
            "type": "best_fields",
            "operator": "OR"
          }
        },
        {
          "multi_match": {
            "query": "primer",
            "fields": [
              "alternativeTitles.alternativeTitle.*^1.0",
              "contributors.name^1.0",
              "identifiers.value^1.0",
              "indexTitle.*^1.0",
              "series.*^1.0",
              "title.*^1.0"
            ],
            "type": "best_fields",
            "operator": "OR"
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1.0
    }
  }
}



nested contributors82663322253367726439831643B ~ 26,4GB3198


Expand


Code Block
{
    "query": {
        "nested": {
            "path": "contributors",
            "query": {
                "match_phrase": {
                    "contributors.name": "Pavlo Smahin Test"
                }
            }
        }
    }
}




Expand


Code Block
{
  "query": {
    "bool": {
      "should": [
        {
          "nested": {
            "path": "contributors",
            "query": {
              "match_phrase": {
                "contributors.name": "Pavlo Smahin Test"
              }
            }
          }
        },
        {
          "bool": {
            "must": [
              {
                "multi_match": {
                  "query": "Pavlo",
                  "fields": [
                    "alternativeTitles.alternativeTitle.*^1.0",
                    "identifiers.value^1.0",
                    "indexTitle.*^1.0",
                    "series.*^1.0",
                    "title.*^1.0"
                  ],
                  "type": "best_fields",
                  "operator": "OR"
                }
              },
              {
                "multi_match": {
                  "query": "Smahin",
                  "fields": [
                    "alternativeTitles.alternativeTitle.*^1.0",
                    "identifiers.value^1.0",
                    "indexTitle.*^1.0",
                    "series.*^1.0",
                    "title.*^1.0"
                  ],
                  "type": "best_fields",
                  "operator": "OR"
                }
              },
              {
                "multi_match": {
                  "query": "Test",
                  "fields": [
                    "alternativeTitles.alternativeTitle.*^1.0",
                    "identifiers.value^1.0",
                    "indexTitle.*^1.0",
                    "series.*^1.0",
                    "title.*^1.0"
                  ],
                  "type": "best_fields",
                  "operator": "OR"
                }
              }
            ],
            "adjust_pure_negative": true,
            "boost": 1.0
          }
        }
      ]
    }
  }
}



nested contributors with include_in_parent 82663322253367727184318612B ~ 27,1GB3288


Expand


Code Block
{
    "query": {
        "multi_match": {
            "query": "Pavlo Smahin",
            "fields": [
                "contributors.name^1.0"
            ],
            "type": "cross_fields",
            "operator": "AND"
        }
    }
}




Expand


Code Block
{
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "semantic",
            "fields": [
              "alternativeTitles.alternativeTitle.*^1.0",
              "contributors.name^1.0",
              "identifiers.value^1.0",
              "indexTitle.*^1.0",
              "series.*^1.0",
              "title.*^1.0"
            ],
            "type": "best_fields",
            "operator": "OR"
          }
        },
        {
          "multi_match": {
            "query": "web",
            "fields": [
              "alternativeTitles.alternativeTitle.*^1.0",
              "contributors.name^1.0",
              "identifiers.value^1.0",
              "indexTitle.*^1.0",
              "series.*^1.0",
              "title.*^1.0"
            ],
            "type": "best_fields",
            "operator": "OR"
          }
        },
        {
          "multi_match": {
            "query": "primer",
            "fields": [
              "alternativeTitles.alternativeTitle.*^1.0",
              "contributors.name^1.0",
              "identifiers.value^1.0",
              "indexTitle.*^1.0",
              "series.*^1.0",
              "title.*^1.0"
            ],
            "type": "best_fields",
            "operator": "OR"
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1.0
    }
  }
}



...