elastic search match query over array object

前端 未结 2 871
既然无缘
既然无缘 2021-01-28 01:40

Suppose i\'ve 3 doc

doc_1 = {
    "citedIn": [
        "Bar Councils Act, 1926 - Section 15",
        "Contract Act, 1872 - Section 23&qu         


        
2条回答
  •  独厮守ぢ
    2021-01-28 02:28

    Adding a working example with index data, mapping,search query, and search result.

    You need to use nested query to search on nested fields

    Index Mapping

    {
        "mappings": {
            "properties": {
                "citedIn": {
                    "type": "nested"
                }
            }
        }
    }
    

    Index Data:

     {
            "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"
                }
            ]
        }
    

    Search Query:

    {
        "query": {
            "nested": {
                "path": "citedIn",
                "query": {
                    "bool": {
                        "must": [
                            {
                                "match": {
                                    "citedIn.someFiled": "contract"
                                }
                            },
                            {
                                "match": {
                                    "citedIn.someFiled": "act"
                                }
                            },
                            {
                                "match": {
                                    "citedIn.someFiled": 15
                                }
                            }
                        ]
                    }
                },
                "inner_hits": {}
            }
        }
    }
    

    Search Result:

    "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"
                      }
                    }
                  ]
                }
              }
            }
          }
    

提交回复
热议问题