elasticsearch query_string and term search syntax - elasticsearch

I would like to search for query_string that contains Bob and an Industry_Type_ID of either 8 OR 9.
I am getting a parse error: Parse Failure [No parser for element [Industry_Type_ID]]
{
"query" : {
"query_string":{"query":"Bob"},
"terms" : {
"Industry_Type_ID" : [ "8", "9" ],
"minimum_match" : 1
}
}
}
I am sure I am missing something obvious.

You can do it using bool query with two must clauses:
{
"query" : {
"bool" : {
"must" : [
{
"query_string":{"query":"Bob"}
},
{
"terms" : {
"Industry_Type_ID" : [ "8", "9" ],
"minimum_match" : 1
}
}
]
}
}
}

Related

What's the functionality of "path" in ES_dsl.Q()?

I have a statement: ES_dsl.Q('nested', path='student', query=nest_filter)
What kind of role does the "path" play in the above one?
The path is simply the path to the nested field you're using in your query.
In nest_filter, you need to reference your nested field as student.xyz.
Check the equivalence in the query below:
GET /_search
{
"query": {
"nested" : {
"path" : "student", <--- this is the path
"query" : { <--- this is nest_filter
"bool" : {
{ "match" : {"student.name" : "john"} },
{ "range" : {"student.age" : {"gt" : 20}} }
]
}
}
}
}
}

Different results with the same keywords in elasticsearch query

I have a question to understand the search logic. I have an Elasticsearch 5.4 instance and make a query_string query. The default operator is OR. Other settings are not defined.
Now I search for
dog house
and get 10,500 results. Then I search for
house dog
and get only 6,200 results. That's a bit curious for me.
That's my query:
{
"query" : {
"bool" : {
"must" : [
{
"query_string" : {
"query" : "dog house~",
"default_operator" : "OR",
"fuzziness" : "AUTO"
}
},
{
"term" : {
"client" : {
"value" : "MyClient",
"boost" : 1
}
}
},
{
"range" : {
"dateCreate" : {
"gte" : "2000-01-01T00:00:00+0200",
"lte" : "2000-12-31T23:59:59+0200"
}
}
}
]
}
},
"size" : 2,
"from" : 0,
"sort" : [
{
"_score" : {
"order" : "desc"
}
}
],
"collapse" : {
"field" : "title.keyword"
}
}
It is because of the ~ operator. If you try removing it and running the query again the number should be the same if the query is dog house or house dog.
Even the number of results should be the same if you run dog house~ or house~ dog, but the number will change if you change the ~ from house to dog.
If you want both words to specify the transpositions distance of 1 that the fuzzy search can match you can try with house~ dog~ and then the results will be the same if you change the order.

Elasticsearch Multiple Prefix Keywords

I need to use the prefix filter, but allow multiple different prefixes, i.e.
{"prefix": {"myColumn": ["This", "orThis", "orEvenThis"]}}
This does not work. And if I add each as a separate prefix is also obviously doesn't work.
Help is appreciated.
Update
I tried should but without any luck:
$this->dsl['body']['query']['bool']['should'] = [
["prefix" => ["myColumn" => "This"]],
["prefix" => ["myColumn" => "orThis"]]
];
When I add those two constraints, I get ALL responses (as though filter is not working). But if I use must with either of those clauses, then I do get a response back with the correct prefix.
Based on your comments, it sounds like it may just be an issue with the syntax. With all ES queries (just like SQL ones), I suggest starting simple and just submitting them to ES as the raw DSL outside of code (although in your case this wasn't easily doable). For the request, it's a pretty straight forward one:
{
"query" : {
"bool" : {
"must" : [ ... ],
"filter" : [
{
"bool" : {
"should" : [
{
"prefix" : {
"myColumn" : "This"
}
},
{
"prefix" : {
"myColumn" : "orThis"
}
},
{
"prefix" : {
"myColumn" : "orEvenThis"
}
}
]
}
}
]
}
}
}
I added it as a filter because the optional nature of your prefixing is not improving relevancy: it's literally asking that one of them must match. In such cases where the question is "does this match? yes / no", then you should use a filter (with the added bonus that that's cacheable!). If you're asking "does this match, and which matches better?" then you want a query (because that's relevancy / scoring).
Note: The initial issue appeared to be that the bool / must was unmentioned and the suggestion was to just use a bool / should.
{
"bool" : {
"should" : [
{
"prefix" : {
"myColumn" : "This"
}
},
{
"prefix" : {
"myColumn" : "orThis"
}
},
{
"prefix" : {
"myColumn" : "orEvenThis"
}
}
]
}
}
behaves differently than
{
"bool" : {
"must" : [ ... ],
"should" : [
{
"prefix" : {
"myColumn" : "This"
}
},
{
"prefix" : {
"myColumn" : "orThis"
}
},
{
"prefix" : {
"myColumn" : "orEvenThis"
}
}
]
}
}
because the must impacts the required nature of should. Without must, should behaves like a boolean OR. However, with must, it behaves as a completely optional function to improve relevancy (score). To make it go back to the boolean OR behavior with must, you must add minimum_should_match to the bool compound query.
{
"bool" : {
"must" : [ ... ],
"should" : [
{
"prefix" : {
"myColumn" : "This"
}
},
{
"prefix" : {
"myColumn" : "orThis"
}
},
{
"prefix" : {
"myColumn" : "orEvenThis"
}
}
],
"minimum_should_match" : 1
}
}
Notice that it's a component of the bool query, and not of either should or must!

How to perform inner filter in elastic search

I need to do a nested filter stuff in elastic search.
(item_id == 1 && name == "John") || (product_id == 234 && vendor !="youtube") && (date >="2014-11-01" && date <="2014-11-18")
I refer this link:
http://www.elasticsearch.org/guide/en/elasticsearch/client/java-api/current/search.html.
And performed the post filter operation towards the searchQueryBuilder.but sorting is not woks when we use post filter.
I have already rise question about post filter + sort not working.
http://stackoverflow.com/questions/26986756/filter-sorting-not-working-in-elastic-search
There I got a info about using the post filter only when the aggregation occurred.
Need to know how can I achieve the nester filter + sorting together.
Documentation link will be much help full.
To do everything in a filter:
curl -XGET "http://localhost:9200/hubware3/message/_search?pretty" -d' {
"filter" : {
"and" : [{
"bool" : {
"should" :[ {
"bool" : {
"must" : [
{"term" : { "item_id" : "1" }},
{"term" : { "name" : "John" }}
]
}},
{"bool" : {
"must" : {
"term" : { "product_id" : "234" }
},
"must_not" : {
"term" : { "vendor" : "youtube" }
}
}
}
]
}},
{"range" : {"date " : { "gte" : "2014-11-01","lte" : "2014-11-18"}}}
]
}
}'
and and bool must are interchangeable.

How do I have to write a Search Query in ElasticSearch?

I use the Grails ElasticSearch Plugin and want to use the following query:
"bool" : {
"must" : {
"term" : { "user" : "kimchy" }
},
"must_not" : {
"range" : {
"age" : { "from" : 10, "to" : 20 }
}
},
"should" : [
{
"term" : { "tag" : "wow" }
},
{
"term" : { "tag" : "elasticsearch" }
}
],
"minimum_should_match" : 1,
"boost" : 1.0
}
Using the groovy api from the Grails plugin I would write something like:
def res = userAgentIdentService.search() {
"bool" {
"must" {
term("user" : "kimchy" )
}
"must_not" {
"range" {
age("from" : 10, "to" : 20 }
}
}
"should" : [
{
term( "tag" : "wow" )
}
{
term("tag" : "elasticsearch" )
}
]
"minimum_should_match" = 1
"boost" = 1.0
}
}
My query is not working!
Where do I have to define minimum_should_match and how do I have to define it?
How do I have to write the "should" : [ ... ] square brackets notation in the grails / groovy manner?
I think you're missing a couple of json levels in your search request. I don't think you can use the query without specifying that's a query (it could be a filter as well, or even something else). Have a look at this example from the groovy api reference:
def search = node.client.search {
indices "test"
types "type1"
source {
query {
terms(test: ["value1", "value2"])
}
}
}

Resources