Elasticsearch: wildcard in query differences : "Alex*" vs "*lex*" - elasticsearch

I have a bunch of data indexed using keyword tokenizer.
{
state: open
settings: {
index.number_of_replicas: 0
index.analysis.analyzer.default.type: keyword
index.number_of_shards: 5
index.version.created: 900599
}
mappings: {
evenements: {
properties: {
prenom: {
type: string
}
nom: {
type: string
}
statut: {
type: string
}
fieldDate: {
format: dateOptionalTime
type: date
}
}
}
}
}
When querying
{
"query": {
"bool": {
"must": [
{
"query_string": {
"default_field": "evenements.prenom",
"query": "*lex*"
}
}
]
}
}
}
I get results, but when querying
{
"query": {
"bool": {
"must": [
{
"query_string": {
"default_field": "evenements.prenom",
"query": "Alex*"
}
}
]
}
}
}
I get no results (A ou a instead of first * does not change anything).
Any explanation about this ?
Thanks.
Yann

Try to set lowercase_expanded_terms property of query string to false. By default this property is set to true and can cause the behaviour you are having.
{
"query": {
"bool": {
"must": [
{
"query_string": {
"lowercase_expanded_terms": false,
"default_field": "evenements.prenom",
"query": "Alex*"
}
}
]
}
}
}

Related

How to search for separate key and value fields in an array in ElasticSearch?

My ElasticSearch documents contain a nested collection of form fields. Each field has a name and a value and the mapping is as follows:
form: {
properties: {
id: { type: 'integer' },
name: { type: 'text' },
form_data: {
type: 'nested',
properties: {
'name': { type: 'keyword' },
'value': { type: 'text', analyzer: 'full_text_analyzer' }
}
}
}
}
I need to allow the user to search for multiple form fields to refine their search. They can choose which fields to search by and assign a value to each. For example
applicant_name = 'Joe'
pet_type = 'dog'
This would find all documents that contained a field named applicant_name which had a value fuzzy matching Joe as well as a field named pet_type and a value fuzzy matching dog.
The query I'm trying to do this with is as follows.:
{
"query": {
"bool": {
"must": [{
"nested": {
"path": "form_data",
"query": {
"filter": {
"bool": {
"must": [
{
"bool": {
"must": [
{ "term": { "form_data.name": "applicant_name" } },
{ "match": { "form_data.value": "Joe" } }
]
}
},
{
"bool": {
"must": [
{ "term": { "form_data.name": "pet_type" } },
{ "match": { "form_data.value": "dog" } }
]
}
}
]
}
}
}
}
}]
}
}
}
However, I get 0 results.
Try using a nested query per condition in your initial "must" clause:
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "form_data",
"query": {
"bool": {
"must": [
{ "term": { "form_data.name": "applicant_name" } },
{ "match": { "form_data.value": "Joe" } }
]
}
}
}
},
{
"nested": {
"path": "form_data",
"query": {
"bool": {
"must": [
{ "term": { "form_data.name": "pet_type" } },
{ "match": { "form_data.value": "dog" } }
]
}
}
}
}
]
}
}
}

Get exact values filter in Elastic/Lucene

When querying Elastic with:
"query": {
"bool": {
"must": [
{
"query_string": {
"query": "data.payload.NSFILEID.num:1492141378",
"analyze_wildcard": true
}
}
]
}
}
I get docs with data.payload.NSFILEID.num = 1492141378 and close ones.
Same behaviour with:
{
"query": {
"term": {
"data.payload.NSFILEID.num": 1492141378
}
}
}
This field is indexed as number.
How could I get only exact ones?

ElasticSearch multiple string to search with wildcard query

I'm trying to have multiple wildcard query match in my elasticsearch query in Kibana. I can't quite figure it out.
Basically I want any document with an attribute type="erreur"
and I want to exclude all documents that match the strings "An established*" or "java.lang.*" on the field descr_courte
{
"query": {
"bool": {
"must": {
"term": {
"type": "erreur"
}
},
"must_not": {
"wildcard": {
"descr_courte": ["An established*", "java.lang.*"]
}
}
}
}
}
if I put a single wildcard query it works fine
{
"query": {
"bool": {
"must": {
"term": {
"type": "erreur"
}
},
"must_not": {
"wildcard": {
"descr_courte":
"An established*"
}
}
}
}
}
the error I get:
Error: Request to Elasticsearch failed: {"error":{"root_cause":[{"type":"illegal_state_exception","reason":"Can't get text on a START_ARRAY at 1:454"}],"type":"search_phase_execution_exception","reason":"all shards
Any idea?
Try putting them is separate clauses.
{
"query": {
"bool": {
"must": {
"term": {
"type": "erreur"
},
"must_not": [
{
"wildcard": {
"descr_courte": "An established*"
}
},
{
"wildcard": {
"descr_courte": "java.lang.*"
}
}
]
}
}
}
}
My guess is that you can't make an array for wildcard query like ["An established*", "java.lang.*"], so you need to:
{
"query": {
"{
"must": {
"term": {
"type": "erreur"
}
},
"must_not": {
"regexp": {
"descr_courte": "(An established|java\.lang\.).*"
}
}
}
}
}
More info about regexp query in https://www.elastic.co/guide/en/elasticsearch/reference/5.1/query-dsl-regexp-query.html
Another option is to combine your query terms with the logical operators NOT, AND and OR in the query string
{
"query": {
"query_string" : {
"query" : "type:erreur AND NOT(descr_courte:An established* OR descr_courte:java.lang.*)"
}
}
}
See more info at https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_wildcards

Give more weight to documents having true in a boolean field

(I use elasticsearch version 2.3.3)
I am doing a simple match query on a text field but now want to give more weight to documents having true in a given boolean field.
My current query is something like
{
"query": {
"match": {
"title": "QUICK!"
}
}
Is that possible?
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "QUICK!"
}
}
],
"should": [
{
"term": {
"my_boolean_field": {
"value": true
}
}
}
]
}
}
}

How to combine term filters with a missing filter in Elasticsearch?

We are using Elasticsearch 1.6 and I have a working three term query that I need to modify with a stand alone working missing filter. Here is the current code:
The original term query with three entries
GET ...
{
"query": {
"nested": {
"path": "MAIN_FIELD",
"query": {
"bool": {
"must": [
{
"term": {
"MAIN_FIELD.ID": 1234
}
},
{
"term": {
"MAIN_FIELD.OTHER_IND": "false"
}
},
{
"term": {
"MAIN_FIELD.INDICATOR": "Y"
}
}
]
}
}
}
}
}
The stand alone missing query:
GET ...
{
"query" : {
"filtered" : {
"filter" : {
"missing" : { "field" : "MAIN_FIELD.OTHER_IND" }
}
}
}
}
How do I change the term query from the first query:
"term": {
"MAIN_FIELD.OTHER_IND": "false"
}
to use a missing filter?
I think what you want is below:
{
"query": {
"nested": {
"path": "MAIN_FIELD",
"query": {
"bool": {
"must": [
{
"term": {
"MAIN_FIELD.ID": 1234
}
},
{
"filtered": {
"filter": {
"missing": {
"field": "MAIN_FIELD.OTHER_IND"
}
}
}
},
{
"term": {
"MAIN_FIELD.INDICATOR": "Y"
}
}
]
}
}
}
}
}

Resources