How can I translate the "more complex" fuzzy example from the QueryDSL guide into Java?
What I have so far is this: (Which works fine, but for example I'm unable to find the builder methods for "max_expansion", which would allow me to restrict the query)
QueryBuilders.fuzzyQuery("name", "kimchy")
Any pointers into the right direction are appreciated.
It supposed to be QueryBuilders.fuzzyQuery("name", "kimchy").maxExpansion(5). But, unfortunately, the maxExpansion() method is currently missing. So, until this pull request is merged, the only way to send this query is by expressing it directly in json. You can do it using XContentBuilder.
Construct a Lucene FuzzyQuery directly, then you can pass that option into a constructor arg.
Related
I'm working on a springboot project and having some trouble with ElasticSearch.
The user will put some JSON-format elasticsearch DSL query strings in the database and they are black-box to me. What I need to do is get the query strings and use them so search information in elasticsearch.
In python, the DSL can be a parameter like this:
body = {
"query":{
"match_all":{}
}
}
es.search(index="my_index",doc_type="test_type",body=body)
How can I perform the search without knowing the details of the string and just using the JSON format query in Java?
I believe there are two ways to do it in modern ES client libraries. I haven't tried them myself, but first one seems to be pretty straightforward.
First one is using low-level client:
Request request = new Request("POST", "/index/_search");
request.setJsonEntity(jsonString);
Response response = client.performRequest(request);
Seems like it's enough just to push JSON as string into setJsonEntity, and you're already set.
Second one is to use high level client, and this gets tricky, though it can provide more robust API. As you might know, elasticsearch has concept of XContent, which is serialization/deserialization to/from different formats, including JSON. Theoretically, it is possible to create JsonXContentParser, which then can be used to instantiate SearchSourceBuilder:
SearchSourceBuilder.fromXContent(jsonXContentParser);
The problem is only that JsonXContentParser requires number of arguments to be instantiated, and i'm not sure how to properly create those dependencies.
So I am exploring how to query mongo from java, and I found several different ways of querying this, and I'm not sure if I'm missing some nuance, thus not fully understanding the queries, or they are the same.
So far I found, for java driver v3.2, this:
collection.find().projection(fields(include("x", "y"), excludeId()))
And I've been told this should work:
BasicDBobject query = new BasicDBObject("x", x).append("y", y);//This example may not compile, I haven't tried it, I'm more talking about the idea and concept.
This query would go with a find(), findOne(), distinct(), and so on.
String fields = "averageSpeed";
coll = db.getCollection(strMongoCollection);
coll.find(fields, query));
So, are both right approaches? Or its purpose is deferent
You always have the option of using the old unwieldy Bson objects yourself, but for the 3.2 driver I'd rather go with the Filters and Projections helper classes.
Thus, a simple search with some criteria can be sent as
collection.find(Filters.eq("myfield", "myvalue"))
For selecting certain fields only, you append a projection:
collection.find(Filters.eq("myfield", "myvalue"))
.projection(Projections.include("myfield", "anotherfield"))
Apart from the more elegant code of the new API, the queries do the same as the BasicDBObject-based calls.
I'm using OrientDB 2.1.6 and have the following Gremling example query:
g.V("username", "testark").out("IsFriendsWith").height.mean()
This works great on the Gremlin shell, but I need to be able to use that from within Java. Using the Java Graph API I could translate most of it to
final GremlinPipeline<Long, Vertex> pipe = new GremlinPipeline<>(orientGraph);
pipe.V("username", "testark").out("IsFriendsWith").property("height").????????
but not I cannot seem to find the equivalent of the mean function. My current workaround involves computing the mean "manually" based on the return value of property, but I'd obviously prefer to get that value from the database via Gremlin. Can anyone please point out to me what's the equivalent of mean?
The mean function is a method of gremlin-groovy so you won't find it on the Java side:
https://github.com/tinkerpop/gremlin/blob/88de5e5d95bd0b704c5090c43a04a3d42992dfb9/gremlin-groovy/src/main/groovy/com/tinkerpop/gremlin/groovy/loaders/PipeLoader.groovy#L28-L35
You would need to do as you are doing and manually calculate the mean.
Im exposing REST services through use of Sprint MVC 4.0 framework and I try following Odata specification for the Query Parameters such as $filter, $search and $orderBy. Each of these contains expressions that I need to parse, build abstract syntax trees and validate. They are all retrieved as String.
I do not need all the constructions that are defined in the Odata grammer (http://docs.oasis-open.org/odata/odata/v4.0/cos01/abnf/odata-abnf-construction-rules.txt), I just pick the ones that are relevant for my uses cases (very few actually)
I would like some tip on how to parse and build the abstract tree in a easy way and if Odata4j might be used as a Utility library to do this job for me? I would like to avoid dragging bunch of new dependencies to odata4j, since I will only use small piece of the code.
You can certainly use odata4j for building ASTs for query parameters. I've done that for exactly the purposes you cite. I split off the query parameters, and then split again on '&' to get parameters. For each of these I inspect the parameter name ($select, $filter, etc.) and then based on that use the corresponding OptionsQueryParser static method on the value, returning an number, or list, or AST specific to that query parameter. For expression ASTs, look at PrintExpressionVisitor and use that as a pattern for writing your own visitor to walk the AST.
I am having some difficulty structuring the exact Elasticsearch query that I am looking for, specifically using the java api.
It seems like if I construct a fieldsearch using the java api, I can only use a single field and a single term. If I use a querystring, it looks like I can apply an entire query to a set of fields. What I want to do is apply a specific query to one field, and another query to a different field.
This is confusing I know. This is the type of query I would like to construct
(name contains "foo" or name contains "bar") AND ( date equals today)
I am really loving Elasticsearch for it's speed and flexibility, but the docs on http://www.elasticsearch.org/ are kind of tough to parse (I noticed "introduction" and "concepts" have no links, but the API section does) If anyone has some good resources on mastering these queries, I'd love to see them. Thanks!
Sounds like a bool query with 2 must clause:
matchQuery("name", "foo bar")
rangeQuery("date").from("2013-02-05").to("2013-02-06")
Does it help?