Home > Net >  How to query all content from a field in Elasticssearch
How to query all content from a field in Elasticssearch

Time:10-07

I'm queriying data from Elasticsearch with python. I can query a certain value in a field like this:

GET index/_search
{
   "query": {
        "match" : {
            "somefieldname": "somevalue" 
        }
    }
}

But how can I query all values inside the field somefieldname?

UPDATE:

Here's an example index:

"_index" : „indexname
        "_type" : "_doc",
        "_id" : "lJlcO3wBhlKWxmXE9jrd",
        "_score" : 0,  
"_source": {
    „field1“: „abc“,
    „field2“: „123",
    „field3": „def“,
   
  },
"_index" : „indexname
        "_type" : "_doc",
        "_id" : "lJlcO3wBhlKWxmXE9jrd",
        "_score" : 0,  
  "_source": {
    „field1“: „fgh“,
    „field2“: „654",
    „field3": „kui“,
   
  },
"_index" : „indexname
        "_type" : "_doc",
        "_id" : "lJlcO3wBhlKWxmXE9jrd",
        "_score" : 00,  
  "_source": {
    „field1“: „567“,
    „field2“: „gfr",
    „field3": „234“,
   
  },

Now I want to query all content from field2 from all docs. So that my output is [„123", „654", „gfr"]

UPDATE:

Index mapping for the field:

{
  "myindex" : {
    "mappings" : {
      "field2" : {
        "full_name" : "field2",
        "mapping" : {
          "field2" : {
            "type" : "keyword"
          }
        }
      }
    }
  }
}

CodePudding user response:

You can use terms aggregation, to get unique values from field2

{
  "size": 0,
  "aggs": {
    "field2values": {
      "terms": {
        "field": "field2"
      }
    }
  }
}

Search Result would be

"aggregations": {
    "field2values": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "123",
          "doc_count": 1
        },
        {
          "key": "654",
          "doc_count": 1
        },
        {
          "key": "gfr",
          "doc_count": 1
        }
      ]
    }
  }
  • Related