Elastic Search exact match query with wildcard search for multiple fields - elasticsearch

I have elastic search data store like below, and I need to write multi search ES query through these data with exact match and exact match + *(wildcard)
[
{
"id": "123",
"name": "test123 abc bct",
"externalObj": {
"id": "abc 123"
}
},
{
"id": "124",
"name": "test124 abc bct",
"externalObj": {
"id": "abc 124"
}
}
]
currently i have written query like below,
{
"query": {
"query_string": {
"fields": [
"name^5",
"id",
"externalObj.*"
],
"query": "(test124 abc)",
"default_operator": "AND"
}
}
}
Above query is working fine with exact match but I need to get the data for partial search and maximum relevant score for the response as well. that thing doesn't work with this query.
e.g: "query": "test124 ab"
Can anyone help me out for above problem ?

There are 2 options to achieve what you want. You can choose one of them to use:
Set default_operator to OR (or just simply remove it since the default value is OR).
{
"query": {
"query_string": {
"fields": [
"name^5",
"id",
"externalObj.*"
],
"query": "test124 a"
}
}
}
Change your query into test124 a*
{
"query": {
"query_string": {
"fields": [
"name^5",
"id",
"externalObj.*"
],
"query": "test124 a*",
"default_operator": "AND"
}
}
}

Related

Elasic search: find doc by id and highlight words based on query string

I like to find an document in elastic search an highlight terms based on an query string.
Is this possible?
I tried to run an query-string elastic search and filter the result based on ID. But those sounds not very efficient, because elastic first generates an huge list of all document matched the querystring (which could by millions) an pic only one document based on the filter.
Is there a way or query-contstruct to combine querystring and "search for term in _id field" in one boolean search?
Something like this (which is not working):
"query": {
"bool": {
"must": {
"query_string": {
"query": "red*",
"fields": [
"text",
"title"
]
},
"term": {
"_id":"fda72434fa172"
}
}
}
},
"highlight": {
"fields": {
[...]
I made a small example that can be a starting point.
Use filter to perform your query and retrieve the doc by id.
Then I used match and highlight to highlight the term I want.
POST test/_doc/fda72434fa172
{
"text": "I like to find an document in elastic search an highlight terms based on an query string. Is this possible?"
}
GET test/_search
{
"query": {
"bool": {
"filter": [
{
"term": {
"_id": "fda72434fa172"
}
}
],
"must": [
{
"match": {
"text": {
"query": "elastic search"
}
}
}
]
}
},
"highlight": {
"fields": {
"text": {}
}
}
}

How to use multi-match query to get half search terms?

I want to find names that start or have the phrase man in them. But I also want to query on different indexes.
So it should return all fields that have the term man in them such as hoffman last name, or anything that has the term in it.
How can I achieve this?
"query": {
"multi_match": {
"query": "man",
"fields": [
"name",
"last_name",
"email"
]
}
}
}
{
"query": {
"multi_match": {
"query": "man",
"fields": [
"billing.name",
"billing.last_name",
"billing.email"
]
}
}
}
If you want to search across multiple indexes, then you can use _index field, and for searching across multiple fields you can use query_String instead of a multi-match query.
GET /_search
{
"query": {
"bool": {
"must": [
{
"terms": {
"_index": [
"index-1",
"index-2"
]
}
},
{
"query_string": {
"query": "*man*"
}
}
]
}
}
}

How to filter the follwing query in elasticsearch?

I am using the following search:
{
"_source": [
"title",
"bench",
"court",
"id_"
],
"size": 10,
"query": {
"bool": {
"must": {
"multi_match": {
"query": "murder"
,
"fields": [
"title",
"content"
]
}
},
"should": {
"multi_match": {
"query": "murder",
"fields": [
"title.standard",
"content.standard"
]
}
}
}
},
"highlight": {
"fields": {
"title": {},
"content": {}
}
}
}
I now want to filter the results using the id (_id) elastic search gave it during indexing. For example, {"_id" : 5903}. I guess you have to use the term query. The results should be such that only if the _id is matched, the document returns. How can I do that?
In order to get your query filtered by doc's id (one, or many), there is a special id query in elasticsearch. Here are the details: https://www.elastic.co/guide/en/elasticsearch/reference/master/query-dsl-ids-query.html

Elasticsearch : Improving multi_match results

Hi I am trying to search a value in a different fields. I am using multi_match with cross_fields type.
Here is the query
"query": {
"filtered": {
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "red cross",
"type": "cross_fields",
"fields": [
"name^20",
"keywords^12",
"description^1"
]
}
}
]
}
}
} }
Results I am getting for the above query
"results": [
{
"name": "CrossPurpose",
.....
} ,
{
"name": "Community Center",
"keywords": "American Red Cross,Red Cross,Center,Blood",
. . .
},
{
"name": "Some Group",
"description":".... red cross ..."
},
.......
....
..
{
"name" : "Red cross" ,
.....
}
]
I want to improve the search results.
I want to see the record with name "Red cross" in the first position.
First it should match as whole word "red cross" then only it should match "red" , "cross".
I want preference to be given more for name field than keywords and description.
Please help me to improve the query .
Thanks in advance.
You need to use type = best_fields
so your query will be
{
"query": {
"multi_match" : {
"query": "red cross",
"type": "best_fields",
"fields": [ "name", "keywords", "description" ],
"tie_breaker": 0.3
}
}
}

ElasticSearch : MultiMatch with "match a list of values"

How can I combine MultiMatch + "match a list of values" in a single query.
ie . I want to query a list of names ["John","Bas","Peter"] against a list of fields ["first_name","Alias","nick_name","surname"]
-match a list of values-
{
"query": {
"filtered" : {
"filter" : {
"terms": {
"first_name": ["John","Bas","Peter"]
}
}
}
}
}
-MultiMatch-
{
"multi_match" : {
"query": "john",
"fields": ["first_name","Alias","nick_name","surname"]
}
}
You can provide multiple search terms to the multi_match as well:
"query": {
"multi_match": {
"query": "John Bas Peter",
"fields": [
"first_name",
"Alias",
"nick_name",
"surname"
]
}
}
Also, multi_match has various ways by which it's actually transforming internally the query: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html#multi-match-types
If you use the _validate API with this you could see how exactly ES is re-interpreting the query:
GET /_validate/query?explain=true
{
"query": {
"multi_match": {
"query": "John Bas Peter",
"fields": [
"first_name",
"Alias",
"nick_name",
"surname"
]
}
}
}
The above gives:
"explanations": [
{
"index": "test",
"valid": true,
"explanation": "((Alias:john Alias:bas Alias:peter) | (surname:john surname:bas surname:peter) | (nick_name:john nick_name:bas nick_name:peter) | (first_name:john first_name:bas first_name:peter))"
}
]

Resources