ElasticSearch: Search across multiple fields with input strings (NumberFormatException) - elasticsearch

I am searching across multiple fields in a query using the * asterisk notation (Ex: I want all fields startings with source so I specify fields source.*) and I specify a query of foobar as string. I am using a Query String type query.
I keep getting a NumberFormatException and I have some fields in there with a mapping type of long and double.
Any idea how to go about this? I need to do a multi-field search.
My query is posted below:
{
"query": {
"bool": {
"must": [{
"query_string": {
"default_field": "source.*",
"query": "foobar"
}
}],
"must_not": [],
"should": []
}
},
"from": 0,
"size": 100000,
"sort": [],
"facets": {}
}

set lenient to true to ignore format based failures
example :
"query_string":
{
"default_field": "source.*",
"query": "foobar",
"lenient": true
}

Related

How to search for a value in any nested field?

Basically I have to find a matching value in all the fields in my document.
I tried using query string but unfortunately, it only searches in first-level fields. The nested field values are not fetched using query string or may be the way I am using it is wrong.
I tried
GET events/_search
{
"query": {
"bool": {
"must": [
{
"query_string": {
"default_field": "*",
"query": "match"
}
}
]
}
}
}
how will I query such a way that it checks all the fields including the nested fields.
Thanks in advance
You can always have multiple clauses, one for the first level fields, and another for nested fields combined in an OR condition:
GET events/_search
{
"query": {
"bool": {
"minimum_should_match": 1,
"should": [
{
"query_string": {
"default_field": "*",
"query": "match",
"lenient": true
}
},
{
"nested": {
"path": "nested_field",
"query": {
"query_string": {
"default_field": "nested_field.*",
"query": "match",
"lenient": true
}
}
}
}
]
}
}
}

MUST and MUST_NOT query in Elasticsearch

I have indexed documents with metadata "User_Id" containing data "A"
and "B". I'm trying to check documents "A NOT B". I am not able to get the desired output. I am restricted to not use "query string query" and use "NOT" operator.
Doesn't must_not support multi_match?
{
"from": 0,
"size": 24,
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "A",
"fields": ["User_Id"],
"fuzziness": "AUTO"
}
}
],
"must_not" :[
{
"multi_match": {
"query": "B",
"fields": ["User_Id"],
"fuzziness": "AUTO"
}
}
]
}
}
}
You need to remove the fuzziness auto. This setting allows approximation in the string: a query "AUTO13273" with fuzziness "AUTO" will match AUTO13272 and AUTO13273 since the distance between those two strings is only 1.
See the fuzziness documentation here

Seperate results based on type Elasticsearch

I am new to Elasticsearch and am struggling with a concept that I hope someone would be kind enough to point me in the right direction. I have a query that returns results of a type Document. This works fine. I want to also have a seperate set of results for objects of type Event. How can I accomplish this?
Any help would be greatly appreciated!
Here is the query that works:
"query": {
"bool": {
"should": [
{ "match": {
"sortableTitle": {
"query": query,
"boost": 3,
"operator": "and"
}}},
{ "match": {
"content": {
"query": query,
"boost": 2,
"operator": "or"
}}},
{ "match": {
"description": {
"query": query,
"boost": 1,
"operator": "or"
}}},
],
"must": {
"match": {
"metaType":{
"query": "Document",
"operator": "and"
}}},
},//end bool
},//end query
You need to specify the type of the documents that you want to get back with your request. The way you specify the type depends on the client that you are using, and it's not part of the request. In case of elasticsearch.js the parameter that you are looking for is called type and it should appear on the same level as body where your request goes:
client.search({
index: 'myindex',
type: 'Document',
body: {
query: {

elasticsearch default_field vs fields different results

Here is two queries.
First:
{
"query": {
"filtered": {
"query": {
"query_string": {
"query": "27444.2",
"default_field": "text"
}
}
}
},
"from": 0,
"size": 50
}
Second:
{
"query": {
"filtered": {
"query": {
"query_string": {
"query": "27444.2"
}
}
}
},
"fields": ["text"],
"from": 0,
"size": 50
}
The only difference between them is that in first i use default_field to specify a field to search, and in second i specify it through fields param. The field name is the same.
I expect both variant to produce same results, but thats not the case. The first variant doesn't return any results, and the second return a result. So what im doing wrong here? Where is the catch
elasticsearch 1.4.2
The way you have given fields param is wrong.
In the second case you are referring to the field params in the query where you are restricting the results to show only certain fields and not the entire _source
The following one is what you are looking for -
{
"query": {
"filtered": {
"query": {
"query_string": {
"query": "27444.2",
"fields": ["text"]
}
}
}
},
"from": 0,
"size": 50
}
2 queries are not the same.
First searches the field 'text' and second searches all fields and in response, returns only 'field'.

How can Elasticsearch search characters like #

I face the problem about writing Elasticsearch query.
My query is like below
{
"query": {
"query_string": {
"default_field": "content",
"query": "#lin1"
}
},
"from": 0,
"size": 1000,
"sort": [
{
"time": "desc"
}
]
}
And I am using query_string
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html
But the # character can not match.
It will come out this kind of result: lin1 or 「lin1」
So how should I write the Elasticsearch query to match #lin1?
It all depends on the analyzer you are using. For all you know, you are using the standard analyzer which discards the '#' symbol from the index. In that case, you'll never be able to search for the '#' symbol. But if that is not the case and you do have '#' indexed, you can modify the query_string section of your query to below:
"query_string": {
"default_field": "content",
"query": "#lin1",
"analyzer": "whitespace"
}

Resources