1

i would like to get distinct ip's for example today and where campaigne="2" in sql: select distinct ip from test where timestamp >= "2016-01-16" ... AND fk_campaign_id = "2";

this works but json validator outputs "Duplicate key, names should be unique."

{  
   "size":0,
   "aggs":{  
      "distinct_ip":{  
         "cardinality":{  
            "field":"ip"
         }
      }
   },
   "query":{  
      "range":{  
         "timestamp":{  
            "gte":"2016-01-16T00:00:00",
            "lt":"2016-01-17T00:00:00"
         }
      }
   },
   "query":{  
      "match":{  
         "fk_campaign_id":"2"
      }
   }
}

But if i try to build this query in php, var_dump($params) returns me back json only with one "query", may be because of Duplicate key???

{  
   "size":0,
   "aggs":{  
      "distinct_ip":{  
         "cardinality":{  
            "field":"ip"
         }
      }
   },

part with range is not here?!?!?

   "query":{  
      "match":{  
         "fk_campaign_id":"2"
      }
   }
}

Thanks in advance.

1 Answer 1

1

In your json query is a duplicate key. You need to use bool query whenever you have multiple conditions. since you have AND condition you need to use must clause. This is the right syntax

{
  "query": {
    "bool": {
      "must": [
        {
          "range": {
            "timestamp": {
              "gte": "2016-01-16T00:00:00",
              "lt": "2016-01-17T00:00:00"
            }
          }
        },
        {
          "match": {
            "fk_campaign_id": "2"
          }
        }
      ]
    }
  },
  "size": 0,
  "aggs": {
    "distinct_ip": {
      "cardinality": {
        "field": "ip"
      }
    }
  }
}

Hope this helps!

Sign up to request clarification or add additional context in comments.

2 Comments

Could you recommend any literature for Elasticsearch? Tanks
One of the most important thing about ES is its data structure i.e inverted index, its analysis process, its internals, that link also has links to other papers. This is a good starting point IMO.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.