您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

ElasticSearch:在嵌套数组中搜索字段

ElasticSearch:在嵌套数组中搜索字段

嵌套字段应使用嵌套查询进行搜索

echo "Deleting old ElasticSearch index..."
curl -XDELETE 'localhost:9200/arrtest'
echo
echo "Creating new ElasticSearch index..."
curl -XPUT 'localhost:9200/arrtest/?pretty=1' -d '{
   "mappings" : {
      "cust2" : {
         "properties" : {
            "firstName" : {
               "type" : "string",
               "analyzer" : "string_lowercase"
            },
            "lastName" : {
               "type" : "string",
               "analyzer" : "string_lowercase"
            },
            "paymentInfos": {
                "properties": {
                    "billingZip": {
                        "type": "string",
                        "analyzer": "string_lowercase"
                    },
                    "paypalEmail": {
                        "type": "string",
                        "analyzer": "string_lowercase"
                    }
                },
                "type": "nested"
            }
         }
      }
   },

   "settings" : {
      "analysis" : {
         "analyzer" : {
            "uax_url_email" : {
               "filter" : [ "standard", "lowercase" ],
               "tokenizer" : "uax_url_email"
            },

            "string_lowercase": {
                "tokenizer" : "keyword",
                "filter" : "lowercase"
            }
         }
      }
   }
}
'
echo
echo "Index recreation finished"

echo "Inserting one record..."
curl -XPUT 'localhost:9200/arrtest/cust2/1' -d '{
    "firstName": "john",
    "lastName": "smith",

    "paymentInfos": [{
        "billingZip": "10101",
        "paypalEmail": "foo@bar.com"
    }, {
        "billingZip": "20202",
        "paypalEmail": "foo2@bar2.com"
    }]
}
'
echo
echo "Refreshing index to make new records searchable"
curl -XPOST 'localhost:9200/arrtest/_refresh' 
echo
echo "Searching for record..."
curl -XGET 'localhost:9200/arrtest/cust2/_search?pretty=1' -d '{
    "sort": [],
    "query": {
        "bool": {
            "should": [],
            "must_not": [],
            "must": [{
                "nested": {
                    "query": {
                        "query_string": {
                            "fields": ["paymentInfos.billingZip"],
                            "query": "10101"
                        }
                    },
                    "path": "paymentInfos"
                }
            }]
        }
    },
    "facets": {},
    "from": 0,
    "size": 25
}'
echo
其他 2022/1/1 18:21:09 有502人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶