- Java Transport Client (deprecated): other versions:
- Preface
- Maven Repository
- Deploying in JBoss EAP6 module
- Client
- Index API
- Get API
- Delete API
- Update API
- Bulk API
- Search API
- Count API
- Delete By Query API
- Facets
- Aggregations
- Percolate API
- Query DSL - Queries
- Match Query
- MultiMatch Query
- Boolean Query
- Boosting Query
- IDs Query
- Constant Score Query
- Disjunction Max Query
- Fuzzy Like This (Field) Query (flt and flt_field)
- FuzzyQuery
- Has Child / Has Parent
- MatchAll Query
- More Like This Query (mlt)
- Prefix Query
- QueryString Query
- Range Query
- Span Queries (first, near, not, or, term)
- Term Query
- Terms Query
- Top Children Query
- Wildcard Query
- Nested Query
- Indices Query
- GeoShape Query
- Query DSL - Filters
- And Filter
- Bool Filter
- Exists Filter
- Ids Filter
- Limit Filter
- Type Filter
- Geo Bounding Box Filter
- GeoDistance Filter
- Geo Distance Range Filter
- Geo Polygon Filter
- Geo Shape Filter
- Has Child / Has Parent Filters
- Match All Filter
- Missing Filter
- Not Filter
- Or Filter
- Prefix Filter
- Query Filter
- Range Filter
- Script Filter
- Term Filter
- Terms Filter
- Nested Filter
- Caching
Generate JSON document
editGenerate JSON document
editThere are several different ways of generating a JSON document:
-
Manually (aka do it yourself) using native
byte[]
or as aString
-
Using a
Map
that will be automatically converted to its JSON equivalent - Using a third party library to serialize your beans such as Jackson
- Using built-in helpers XContentFactory.jsonBuilder()
Internally, each type is converted to byte[]
(so a String is converted
to a byte[]
). Therefore, if the object is in this form already, then
use it. The jsonBuilder
is highly optimized JSON generator that
directly constructs a byte[]
.
Do It Yourself
editNothing really difficult here but note that you will have to encode dates according to the Date Format.
String json = "{" + "\"user\":\"kimchy\"," + "\"postDate\":\"2013-01-30\"," + "\"message\":\"trying out Elasticsearch\"" + "}";
Using Map
editMap is a key:values pair collection. It represents a JSON structure:
Map<String, Object> json = new HashMap<String, Object>(); json.put("user","kimchy"); json.put("postDate",new Date()); json.put("message","trying out Elasticsearch");
Serialize your beans
editElasticsearch already uses Jackson but shades it under
org.elasticsearch.common.jackson
package.
So, you can add your own Jackson version in your pom.xml
file or in
your classpath. See Jackson
Download Page.
For example:
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.1.3</version> </dependency>
Then, you can start serializing your beans to JSON:
import com.fasterxml.jackson.databind.*; // instance a json mapper ObjectMapper mapper = new ObjectMapper(); // create once, reuse // generate json byte[] json = mapper.writeValueAsBytes(yourbeaninstance);
Use Elasticsearch helpers
editElasticsearch provides built-in helpers to generate JSON content.
import static org.elasticsearch.common.xcontent.XContentFactory.*; XContentBuilder builder = jsonBuilder() .startObject() .field("user", "kimchy") .field("postDate", new Date()) .field("message", "trying out Elasticsearch") .endObject()
Note that you can also add arrays with startArray(String)
and
endArray()
methods. By the way, the field
method
accepts many object types. You can directly pass numbers, dates and even
other XContentBuilder objects.
If you need to see the generated JSON content, you can use the
string()
method.
String json = builder.string();