Показать страницуИстория страницыСсылки сюдаCopy this pageExport to MarkdownODT преобразованиеНаверх Вы загрузили старую версию документа! Сохранив её, вы создадите новую текущую версию с этим содержимым. Медиафайлы====== how to match an array value by it's key in a key value pair elasticsearch array? ====== Create empty index "twitter" \\ <code bash> $ curl -XDELETE 'http://localhost:9200/twitter/' $ curl -XPUT 'http://localhost:9200/twitter/' </code> create geo_point mapping for the actual "tweet" \\ <code bash> $ curl -XPUT 'http://localhost:9200/twitter/tweet/_mapping' -d ' { "tweet": { "properties": { "db": { "type": "object", "properties": { "@type": { "type": "string" }, "oracle_props": { "type": "nested", "properties": { "@name": { "type": "string" }, "@value": { "type": "string" } } } } } } } }' </code> Let's check if the mapping was set \\ <code bash> $ curl -XGET 'http://localhost:9200/twitter/tweet/_mapping?pretty=true' </code> Post some tweets, with nested data \\ <code bash> $ curl -XPUT 'http://localhost:9200/twitter/tweet/1' -d '{ "name": "Athena", "version": 1, "db": { "@type": "Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 64bit", "oracle_props": [ { "@name": "open_cursors", "@value": 4000 }, { "@name": "USER_ROLE_PRIVS_COUNT", "@value": 1 }, { "@name": "CREATE_PERMISSION", "@value": "Y" } ] } }' </code> Query nested only \\ <code bash> $ curl -XGET localhost:9200/twitter/tweet/_search -d '{ "query": { "nested" : { "path" : "db.oracle_props", "score_mode" : "avg", "query" : { "bool" : { "must" : [ { "term": { "db.oracle_props.@name": "open_cursors" } }, { "range": { "db.oracle_props.@value": { "lte": 4000 } } } ] } } } } }'; </code> Query "Athena" and "Oracle" \\ <code bash> $ curl -XGET localhost:9200/twitter/tweet/_search -d '{ "query" : { "bool" : { "must" : [ { "match" : {"tweet.name" : "Athena"} }, { "match" : {"tweet.db.@type" : "Oracle"} } ] } } }' </code> Combine the former two queries \\ <code bash> $ curl -XGET localhost:9200/twitter/tweet/_search -d '{ "query" : { "bool" : { "must" : [ { "match" : {"tweet.name" : "Athena"} }, { "match" : {"tweet.db.@type" : "Oracle"} }, { "nested" : { "path" : "db.oracle_props", "score_mode" : "avg", "query" : { "bool" : { "must" : [ { "term": { "db.oracle_props.@name": "open_cursors" } }, { "range": { "db.oracle_props.@value": { "lte": 4000 } } } ] } } } } ] } } }' </code> Results as <code json> { "took": 2, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 1, "max_score": 2.462332, "hits": [ { "_index": "twitter", "_type": "tweet", "_id": "1", "_score": 2.462332, "_source": { "name": "Athena", "version": 1, "db": { "@type": "Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 64bit", "oracle_props": [ { "@name": "open_cursors", "@value": 4000 }, { "@name": "USER_ROLE_PRIVS_COUNT", "@value": 1 }, { "@name": "CREATE_PERMISSION", "@value": "Y" } ] } } } ] } } </code>СохранитьПросмотрРазличияОтменить Сводка изменений Примечание: редактируя эту страницу, вы соглашаетесь на использование своего вклада на условиях следующей лицензии: CC0 1.0 Universal