Search over all fields with highlighting - elasticsearch

I recently working with Elasticsearch 2 and would like to request a query over all text fields.
GET myindex/mydata/_search
{
"query": {
"simple_query_string": {
"query": "Raketenfahrrad"
}
},
"highlight": {
"fields": [ { "*": {} } ]
}
}
The query returns the expected results but without any highlighting. I experienced that I get highlighting when I narrow the search fields manually:
{
"query": {
"simple_query_string": {
"query": "Raketenfahrrad",
"fields": ["MainTitle","SubTitle","Author","Content"]
}
},
"highlight": {
"fields": [ { "*": {} } ]
}
}
But that doesn't fit my requirement "Search over all" and will fail when the next new property is added to mydata type.

From ES 2.0, highlighting will be performed only on queried fields, you have to set require_field_match option to false. Here is the link to the change
Try this
{
"query": {
"simple_query_string": {
"query": "Raketenfahrrad"
}
},
"highlight": {
"fields": {
"*": {}
},
"require_field_match" : false
}
}

Related

Can you reference other queries in Elasticsearch percolator?

can percolator queries reference other stored query docs in a percolator index? For example, given I have the following Boolean query, with _id=1, already indexed in the percolator:
{
"query": {
"bool": {
"must": [
{ "term": { "tag": "wow" } }
]
}
}
}
Could I have another query, with _id=2, indexed (note that I'm making up the _percolator_ref_id terms query key):
{
"query": {
"bool": {
"should": [
{ "term": { "tag": "elasticsearch" } },
{ "terms" : { "_percolator_ref_id": [1] } }
]
}
}
}
If I percolated the following document:
{ "tag": "wow" }
I would expect both _id=1 and _id=2 queries to match. Does some functionality like _percolator_ref_id exist?
Thanks!
Edit: To clarify, I do not know beforehand how many query references appear in a given query (e.g., the _id=2 query could reference 10 other queries potentially).
You can do something like below
2 queries are registered in below index
PUT myindex
{
"mappings": {
"properties": {
"query1": {
"type": "percolator"
},
"query": {
"type": "percolator"
},
"field": {
"type": "text"
}
}
}
}
You can use bool and must/should to combine different queries
GET /myindex/_search
{
"query": {
"bool": {
"must": [
{
"percolate": {
"field": "query",
"document": {
"field": "fox jumps over the lazy dog"
}
}
},
{
"percolate": {
"field": "query1",
"document": {
"field": "fox jumps over the lazy dog"
}
}
}
]
}
}
}

No highlights from nested inner hit when term contains brackets

I have a document with a nested field and I'm having some trouble getting highlighting to work. Why am I not getting highlighting when my term query contains pointy brackets (<>)?
We have two fields in a nested mapping containing similar data:
"value": {
"type": "keyword",
"normalizer": "lowercase"
},
"valueWithQualifier": {
"type": "keyword",
"normalizer": "lowercase"
}
The lowercase normalizer uses the filters ["asciifolding", "lowercase"]
The value is generally an alphanumeric string but the valueWithQualifier takes the form value<qualifier>. When I execute a term query on the value field, it generally returns highlighting information. When I execute a term query on the valueWithQualifier field, I never get highlighting info.
{
"query": {
"nested": {
"path": "assoc",
"query": {
"term": {
"assoc.value": "123abc"
}
},
"inner_hits": {
"highlight": {
"fields": {
"assoc.value*": {}
}
}
}
}
}
}
This returns an inner hit with a highlight:
"highlight": {
"assoc.value": [
"<em>123abc</em>"
]
}
However, this query returns the inner_hit but no highlighting:
{
"query": {
"nested": {
"path": "assoc",
"query": {
"term": {
"assoc.valueWithQualifier": "123abc<qual>"
}
},
"inner_hits": {
"highlight": {
"fields": {
"assoc.value*": {}
}
}
}
}
}
}
However, this does return the highlighting (but I'd rather use a term query due to efficiency):
{
"query": {
"nested": {
"path": "assoc",
"query": {
"prefix": {
"assoc.valueWithQualifier": "123abc"
}
},
"inner_hits": {
"highlight": {
"fields": {
"assoc.value*": {}
}
}
}
}
}
}
"highlight": {
"assoc.valueWithQualifier": [
"<em>123abc<qual></em>"
]
}
And before someone asks, I have tried adding "encoder": "html" to the highlight.
It turns out this is a bug that was fixed in ES 6.2 (https://github.com/elastic/elasticsearch/pull/27604).

Term query on nested fields returns no result in Elasticsearch

I have a nested type field in my mapping. When I use Term search query on my nested field no result is returned from Elasticsearch whereas when I change Term to Match query, it works fine and Elasticsearch returns expected result
here is my mapping, imagine I have only one nested field in my type mapping
{
"homing.estatefiles": {
"mappings": {
"estatefile": {
"properties": {
"DynamicFields": {
"type": "nested",
"properties": {
"Name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"ValueBool": {
"type": "boolean"
},
"ValueDateTime": {
"type": "date"
},
"ValueInt": {
"type": "long"
}
}
}
}
}
}
}
}
And here is my term query (which returns no result)
{
"from": 50,
"size": 50,
"query": {
"bool": {
"filter": [
{
"nested": {
"query": {
"bool": {
"must": [
{
"term": {
"DynamicFields.Name":{"value":"HasParking"}
}
},
{
"term": {
"DynamicFields.ValueBool": {
"value": true
}
}
}
]
}
},
"path": "DynamicFields"
}
}
]
}
}
}
And here is my query which returns expected result (by changing Term query to Match query)
{
"from": 50,
"size": 50,
"query": {
"bool": {
"filter": [
{
"nested": {
"query": {
"bool": {
"must": [
{
"match": {
"DynamicFields.Name":"HasParking"
}
},
{
"term": {
"DynamicFields.ValueBool": {
"value": true
}
}
}
]
}
},
"path": "DynamicFields"
}
}
]
}
}
}
This is happening because the capital letters with the analyzer of elastic.
When you are using term the elastic is looking for the exact value you gave.
up until now it sounds good, but before it tries to match the term, the value you gave go through an analyzer of elastic which manipulate your value.
For example in your case it also turn the HasParking to hasparking.
And than it will try to match it and of course will fail. They have a great explanation in the documentation in the "Why doesn’t the term query match my document" section. This analyzer not being activated on the value when you query using match and this why you get your result.

terms query does not support minimum_match in Elasticsearch 2.3.3

I want to search documents which match at least two key words using terms query like this:
{
"query": {
"terms": {
"title": ["java","编程","思想"],
"minimum_match": 2
}
},
"highlight": {
"fields": {
"title": {}
}
}
}
It returns "terms query does not support minimum_match".What's wrong with my query?
The correct name was minimum_should_match and that setting has been deprecated in ES 2.0.
What you can do instead is to use a bool/should query with three term queries and the minimum_should_match setting for bool/should queries:
{
"query": {
"bool": {
"minimum_should_match": 2,
"should": [
{
"term": {
"title": "java"
}
},
{
"term": {
"title": "编程"
}
},
{
"term": {
"title": "思想"
}
}
]
}
},
"highlight": {
"fields": {
"title": {}
}
}
}

exact match query in elasticsearch

I'm trying to run an exact match query in ES
in MYSQL my query would be:
SELECT * WHERE `content_state`='active' AND `author`='bob' AND `title` != 'Beer';
I looked at the ES docs here:
https://www.elastic.co/guide/en/elasticsearch/guide/current/_finding_exact_values.html
and came up with this:
{
"from" : '.$offset.', "size" : '.$limit.',
"filter": {
"and": [
{
"and": [
{
"term": {
"content_state": "active"
}
},
{
"term": {
"author": "bob"
}
},
{
"not": {
"filter": {
"term": {
"title": "Beer"
}
}
}
}
]
}
]
}
}
but my results are still coming back with the title = Beer, it doesn't seem to be excluding the titles that = Beer.
did I do something wrong?
I'm pretty new to ES
I figured it out, I used this instead...
{
"from" : '.$offset.', "size" : '.$limit.',
"query": {
"bool": {
"must": [
{
"query_string": {
"default_field": "content_state",
"query": "active"
}
},
{
"query_string": {
"default_field": "author",
"query": "bob"
}
}
],
"must_not": [
{
"query_string": {
"default_field": "title",
"query": "Beer"
}
}
]
}
}
}
Query String Query is a pretty good concept to handle various relationship between search criteria. Have a quick look into Query string query syntax to understand in detail about this concept
{
"query": {
"query_string": {
"query": "(content_state:active AND author:bob) AND NOT (title:Beer)"
}
}
}
Filters are supposed to work on exact values, if you had defined your mapping in a manner where title was a non-analyzed field, your previous attempt ( with filters) would have worked as well.
{
"mappings": {
"test": {
"_all": {
"enabled": false
},
"properties": {
"content_state": {
"type": "string"
},
"author": {
"type": "string"
},
"title": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}

Resources