当前位置: 首页 > 知识库问答 >
问题:

弹性搜索中如何只从数组中获取特定元素

阎声
2023-03-14
PUT store
{
  "mappings": {
    "properties": {
      "storeList": {"type": "nested"},
      "storeLocation": {"type": "text"},
      "storePinCode" : {"type": "long"}
    }
  }
}
{
    "storeLocation": "tirupati",
    "storePinCode" : 517501
    "storeList" : [
        {
            "storeName" : "apollo",
            "storeType" : "med"
        },
        {
            "storeName" : "carrots",
            "storeType" : "restaurants"
        },
        {
            "storeName" : "more",
            "storeType" : "supermarket"
        }
    ]
},
{
    "storeLocation": "hyderabad",
    "storePinCode" : 500038
    "storeList" : [
        {
            "storeName" : "apollo",
            "storeType" : "med"
        },
        {
            "storeName" : "bahar cafe",
            "storeType" : "restaurants"
        },
        {
            "storeName" : "dmart",
            "storeType" : "supermarket"
        }
    ]
}

我的例外输出应该如下所示

{
  "took" : 2,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "store",
        "_type" : "_doc",
        "_id" : "Yk8SFWwB2zt5weEsMHn7",
        "_score" : 1.0,
        "_source" : {
          "storeLocation" : "tirupati",
          "storePinCode" : 517501,
          "storeList" : [
            {
              "storeName" : "apollo",
              "storeType" : "med"
            }
          ]
        }
      },
      {
        "_index" : "store",
        "_type" : "_doc",
        "_id" : "ZE8SFWwB2zt5weEsqnkd",
        "_score" : 1.0,
        "_source" : {
          "storeLocation" : "hyderabad",
          "storePinCode" : 500038,
          "storeList" : [
            {
              "storeName" : "apollo",
              "storeType" : "med"
            }
          ]
        }
      }
    ]
  }
}
POST store/_search
{
  "query": {
    "nested": {
      "path": "storeList",
      "query": {
        "bool" : {
          "must" : [
            {"match":{"storeList.storeName": "apollo"}}
          ]
        }
      },
      "inner_hits": {} 
    }
  }
}

实际产出:

{
  "took" : 4,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.093527,
    "hits" : [
      {
        "_index" : "store",
        "_type" : "_doc",
        "_id" : "Yk8SFWwB2zt5weEsMHn7",
        "_score" : 1.093527,
        "_source" : {
          "storeLocation" : "tirupati",
          "storePinCode" : 517501,
          "storeList" : [
            {
              "storeName" : "apollo",
              "storeType" : "med"
            },
            {
              "storeName" : "carrots",
              "storeType" : "restaurants"
            },
            {
              "storeName" : "more",
              "storeType" : "supermarket"
            }
          ]
        },
        "inner_hits" : {
          "storeList" : {
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : 1.093527,
              "hits" : [
                {
                  "_index" : "store",
                  "_type" : "_doc",
                  "_id" : "Yk8SFWwB2zt5weEsMHn7",
                  "_nested" : {
                    "field" : "storeList",
                    "offset" : 0
                  },
                  "_score" : 1.093527,
                  "_source" : {
                    "storeName" : "apollo",
                    "storeType" : "med"
                  }
                }
              ]
            }
          }
        }
      },
      {
        "_index" : "store",
        "_type" : "_doc",
        "_id" : "ZE8SFWwB2zt5weEsqnkd",
        "_score" : 1.093527,
        "_source" : {
          "storeLocation" : "hyderabad",
          "storePinCode" : 500038,
          "storeList" : [
            {
              "storeName" : "apollo",
              "storeType" : "med"
            },
            {
              "storeName" : "bahar cafe",
              "storeType" : "restaurants"
            },
            {
              "storeName" : "dmart",
              "storeType" : "supermarket"
            }
          ]
        },
        "inner_hits" : {
          "storeList" : {
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : 1.093527,
              "hits" : [
                {
                  "_index" : "store",
                  "_type" : "_doc",
                  "_id" : "ZE8SFWwB2zt5weEsqnkd",
                  "_nested" : {
                    "field" : "storeList",
                    "offset" : 0
                  },
                  "_score" : 1.093527,
                  "_source" : {
                    "storeName" : "apollo",
                    "storeType" : "med"
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
}

你能帮帮我吗...

@Ajay sharma,按照你的建议,我把我的问题改成这样

GET store/_search
{
  "_source": {
    "includes": [ "*" ],
    "excludes": [ "storeList" ]
  },
  "query": {
    "nested": {
      "path": "storeList",
      "inner_hits": {       
        "_source": [
          "storeName", "storeType"
        ]
      },
      "query": {
        "bool": {
          "must": [
            {"match":{"storeList.storeName": "more"}}
          ]
        }
      }
    }
  }
}

但我得到了如下的回应...

{
        "_index" : "store",
        "_type" : "_doc",
        "_id" : "Yk8SFWwB2zt5weEsMHn7",
        "_score" : 1.0946013,
        "_source" : {
          "storeLocation" : "tirupati",
          "storePinCode" : 517501
        },
        "inner_hits" : {
          "storeList" : {
            "hits" : {
              "total" : {
                "value" : 1,
                "relation" : "eq"
              },
              "max_score" : 1.0946013,
              "hits" : [
                {
                  "_index" : "store",
                  "_type" : "_doc",
                  "_id" : "Yk8SFWwB2zt5weEsMHn7",
                  "_nested" : {
                    "field" : "storeList",
                    "offset" : 2
                  },
                  "_score" : 1.0946013,
                  "_source" : { }
                }
              ]
            }
          }
        }
      }

共有1个答案

梁渊
2023-03-14

我不能用评论来回应你的评论。因此分享作为答案。

我已经更新了下面的查询。请查一下。我在本地机器上复制了您的索引,并可以得到所需的结果。

查询

{
  "_source": {
    "includes": [ "*" ],
    "excludes": [ "storeList" ]
  },
  "query": {
    "nested": {
      "path": "storeList",
      "inner_hits": {       
        "_source": [
          "storeList.storeName", "storeList.storeType"  <-- changes are here -->
        ]
      },
      "query": {
        "bool": {
          "must": [
            {"match":{"storeList.storeName": "more"}}
          ]
        }
      }
    }
  }
}

输出

"hits": {
    "total": 1,
    "max_score": 0.9808292,
    "hits": [
      {
        "_index": "store",
        "_type": "store",
        "_id": "2",
        "_score": 0.9808292,
        "_source": {
          "storeLocation": "tirupati",
          "storePinCode": 517501
        },
        "inner_hits": {
          "storeList": {
            "hits": {
              "total": 1,
              "max_score": 0.9808292,
              "hits": [
                {
                  "_nested": {
                    "field": "storeList",
                    "offset": 2
                  },
                  "_score": 0.9808292,
                  "_source": {
                    "storeList": {
                      "storeType": "supermarket",
                      "storeName": "more"
                    }
                  }
                }
              ]
            }
          }
        }
      }
    ]
  }
 类似资料:
  • 如何从数组中只得到没有课程名称的分数: 这是我的测试课 这是我的分级课 现在,如果我调用一个方法:gradeList.getAllGrades(),它给我:[Physics 10,LT 4,Math 7],我需要的是:10,4,7

  • 我对编程很陌生,我想做一个程序,用不同的变量发出12张卡片,然后将每张完整的卡片存储在某个地方供以后使用: N=Number(卡片上的数字,可以从1到3) C=Color(卡片是什么颜色,绿色、蓝色或红色) F=Form(有3种形式:蛇、时钟和圆) R=Fill(可以是满的、半的或空的) 这是我到目前为止得到的:

  • 在我的代码库中,我有两个在弹性上搜索的代码。 一个是通过ElasticSearchTemplte,从那里我构建elastic查询,并在elastic上查询。 在第二个中,我通过spring-data-jpa.进行查询 在第一种情况下,我可以看到我的弹性搜索查询,可以直接在ES上查询。 我的问题是,有没有办法从spring-data-jpa中获得弹性查询。 我想看看spring-data-jpa在访

  • 我想从这个代码中得到的数组中得到一个特定的数组数据: 结果是: 数组(1){[“用户”]= 我想得到的是用户名值,我尝试过这样的解决方案: 但它给了我这样的错误: 正在尝试获取非对象的属性“username” 如何解决这个问题?谢谢你的关注。

  • 我需要获取值,当单击具有但不起作用的特定链接时。。。 我的尝试: 我也试过 有什么想法吗?