how to perform partial match elasticsearch - elasticsearch

i want to perform both exact match and partial match. for example, "Alize", so if i type "Ali" it should return the result of "Alize" as well. for this case i only can return the result if i type exact word "Alize".
POST /ecommerce/_search
'{
"query": {
"multi_match": {
"fields": [
"name"
],
"operator": "AND",
"query": "Ali*"
}
},
"size": 20,
"stored_fields": [
"uid",
"_source"
]
}`

You can use querystring query as following
"query": {
"query_string": {
"query": "Ali*",
"fields": ["name"]
}
}
Or use wildcard
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{"query": {"wildcard": {"name": {"value": "Ali*"}}}},
]
}
}
}
}
Wildcard document

This solutions work perfectly for django_elasticsearch_dsl
search_keyword = search_keyword + "*"
query = document_class.search().query(
{
"query_string": {
"query": search_keyword,
"fields": ["name", "code"]
}
}

Related

How to apply fuzziness in a multi_match query to only specified fields in elasticsearch?

If I have a search query like below:
query: {
multi_match: {
query: searchQry,
fields: ["field1", "field2", "field3"],
fuzziness: 2,
fuzzy_transpositions: true,
},
},
How am I able to apply fuzziness only on selective fields such as "field1" or "field2" or "field1" and "field3" instead of all of them, which is the standard behaviour?
Using MultiMatch is not possible.
You can mix multi-match with matches with separated fuzziness.
Example:
{
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "xpto",
"fields": [
"field1",
"field2"
]
}
},
{
"match": {
"fiedl1": {
"query": "xpto",
"fuzziness": 1
}
}
},
{
"match": {
"fiedl2": {
"query": "xpto",
"fuzziness": 2
}
}
}
]
}
}
}

Elasticsearch Query NOT searching in the specified fields

I am struggling with an elasticsearch query. In the fields option, we have specified '*' which means it should look in all fields as well as given the higher weights to a few fields. But it isn't working as it should.
This query was written by my colleague, it'd be great if you could explain it as well as point out the solution. Here's my query:
{
"query": {
"bool": {
"must": [
{
"simple_query_string": {
"query": "Atoms for Peace",
"default_operator": "AND",
"flags": "PREFIX|PHRASE|NOT|AND|OR|FUZZY|WHITESPACE",
"fields": [
"*",
"systemNumber^5",
"global_search",
"objectType^2",
"partTypes.text",
"partTypes.id",
"gs_am_people^2",
"gs_am_person^2",
"gs_am_org^2",
"gs_title^2",
"_currentLocation.displayName",
"briefDescription",
"physicalDescription",
"summaryDescription",
"_flatPersonsNameId",
"_flatPeoplesNameId",
"_flatOrganisationsNameId",
"_primaryDate",
"_primaryDateEarliest",
"_primaryDateLatest"
]
}
}
]
}
}
Your query is fine but it will not work on field with "nested" data type.
From doc
Searching across all eligible fields does not include nested documents. Use a nested query to search those documents.
You need to use nested query
{
"query": {
"bool": {
"minimum_should_match": 1,
"should": [
{
"simple_query_string": {
"query": "Atoms for Peace",
"default_operator": "AND",
"flags": "PREFIX|PHRASE|NOT|AND|OR|FUZZY|WHITESPACE",
"fields": [
"*",
"systemNumber^5",
"global_search",
"objectType^2",
"partTypes.text",
"partTypes.id",
"gs_am_people^2",
"gs_am_person^2",
"gs_am_org^2",
"gs_title^2",
"_currentLocation.displayName",
"briefDescription",
"physicalDescription",
"summaryDescription",
"_flatPersonsNameId",
"_flatPeoplesNameId",
"_flatOrganisationsNameId",
"_primaryDate",
"_primaryDateEarliest",
"_primaryDateLatest"
]
}
},
{
"nested": {
"path": "record",
"query": {
"simple_query_string": {
"query": "Atoms for Peace",
"default_operator": "AND",
"flags": "PREFIX|PHRASE|NOT|AND|OR|FUZZY|WHITESPACE",
"fields": [
"*"
]
}
}
}
}
]
}
}
}

Querying fields with AND in ElasticSearch

In my ElasticSearch document index I have a property type like
type= LOCATION | PERSON | TIME
and a text field that represents the whole document.
To search for types like LOCATION and a specific text like `Mountain View" I do like
doc.text:Mountain View AND doc.type:LOCATION
If I want to do a OR query I would use instead the query_string approach like
"query": {
"query_string": {
"query": "entity.text: (Mountain View OR Menlo Park) AND entity.type:LOCATION"
}
}
This works as well. To do AND queries, like searching for item.text having both "Mountain View" and "Menlo Park" for a item.type=LOCATION, it does not work doing like
"query": {
"query_string": {
"query": "entity.text: (California AND Nevada) AND entity.type:LOCATION"
}
}
Other attempts were:
Using bool clause with should like:
{
"query": {
"bool": {
"should": [
{ "match": { "text": "Menlo Park" }},
{ "match": { "text": "Mountain View" }}
]
}
}
}
Using cross-fields with multi_match
"query": {
"multi_match": {
"query": "California Nevada",
"type": "cross_fields",
"operator": "AND",
"fields": [
"text"
]
}
}
Another approach was using must with the latter (in this case omitting the type by the way):
{
"query": {
"bool": {
"must": [
{
"multi_match" : {
"query": "Nevada",
"type": "cross_fields",
"fields": [ "text"],
}
},
{
"multi_match" : {
"query": "California",
"type": "cross_fields",
"fields": [ "text" ]
}
}
]
}
}
}
but it returns no results neither. Note that in the last case using should instead of must will produce an OR query that will work ok.
So how to perform an AND query on the same field text to match multiple values like California and Nevada?
If I understood the question right, I would do the following:
{
"query": {
"bool" : {
"must": [
"match" : {
"text" : {
"query" : "California Nevada",
"operator" : "and"
}
}
]
}
}
}
Documentation
Hope it helps!

Elastic : search two terms, one on _all, other one on a field

I would like to mix a search on a whole document (eg "developer") and a search on some field for another term (eg "php").
I can do each search separately but I can't mix them.
Here my example (simplified to show only my issue) :
{
"query": {
"function_score": {
"query": {
"match": {
"_all": "developer"
},
"multi_match": {
"query": "php",
"fields": [
"skills.description",
"skills.description",
"skills.details"
],
"operator": "or",
"type": "most_fields"
}
}
}
}
If I run this example I have an error :
Parse Failure [Failed to parse source
Is there a way to search on both _all and specific fields with two terms?
Thanks.
Yes, you're almost there, you need to combine them into a bool/must query:
{
"query": {
"function_score": {
"query": {
"bool": {
"must": [
{
"match": {
"_all": "developer"
}
},
{
"multi_match": {
"query": "php",
"fields": [
"skills.description",
"skills.description",
"skills.details"
],
"operator": "or",
"type": "most_fields"
}
}
]
}
}
}
}
}

How to filter using any of the full text queries?

I've tried numerous ways to filter using a full text query, but to no avail.
Here's what I've attempted:
{
"_source": "_id",
"query": {
"filtered": {
"multi_match": {
"query": "test search query",
"fields": ["title"]
},
"filter": {
"term": {"user_id": "1"}
}
}
}
}
The filtered query takes a filter and a query, you're missing a query part which wraps your multi_match:
{
"_source": "_id",
"query": {
"filtered": {
"query": { <-- add this
"multi_match": {
"query": "test search query",
"fields": ["title"]
}
},
"filter": {
"term": {"user_id": "1"}
}
}
}
}

Resources