Search for parts of a string field

Hello guys, I'm new to the Elasticsearch world and I'm sorry if my doubt is very basic.
I need to create an index on there will be a type that will store a product catalog. In this there will be several fields but what I need to use for research is only one DESCRIPTION field.

Examples of contents in the DESCRIPTION field:
CH COMBINADA 06MM GEDORE
CH COMBINADA 07MM GEDORE
CH COMBINADA 08MM GEDORE
CH COMBINADA 06MM BESEL
CH COMBINADA 07MM BESEL
CH COMBINADA 08MM BESEL

An example of a search would be "CH COMB 06"

records should be returned:
CH COMBINADA 06MM GEDORE
CH COMBINADA 06MM BESEL

but nothing returns. I've used several techniques I found on google but nothing works.

if I search for "CH COMBINADA", it returns results, but I really need to search for one or more parts of words.

In other words, you need the LIKE operator of the SQL language.

Can someone give me a light?

Hi Fabricio,

you can use a wild card query: "CH COMB*06*"
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html

Hello, thanks for the quick response.
I had already tried with the link instruction that you passed. However I was not successful. For example:
I have a product that the description is: "ARMARIO P/FERRAMENTA AM-2 / MARCON".

If I use the query as written below, it returns results:
{ "query": { "wildcard": { "descricao": { "value": "arma*" } } } }
However, with the other methods below, nothing returns
{ "query": { "wildcard": { "descricao": { "value": "arma*ferra*" } } } }
{ "query": { "wildcard": { "descricao": { "value": "ARMA* FERRA*" } } } }
{ "query": { "wildcard": { "descricao": { "value": "arma* *ferra*" } } } }
{ "query": { "wildcard": { "descricao": { "value": "ARMA* FERRAMENTA*" } } } }

If I use 2 terms or more, it does not return results.

you can use 2 x must much :

  "query": {
        "bool": {
           "must":  [
               {"match": {"descricao": "arma"}},
               {"match": {"descricao":"ferra"}}
            ]
         }
    }

See this topic about double wildcards

1 Like

Using wildcard I got a result very close to what I need. But now I have another problem.

For example, in my SQL database, if I send the following query
(SELECT * FROM produtos WHERE descricao LIKE 'PF%SX%1/2%') I get the following result:
PF SX D 3/16X1/2 RI BSW-24 G2 PO
PF SX D 3/16X 1.1/2 RI BSW-24 G2 PO
PF SX D 3/16X 2.1/2 RI BSW-24 G2 PO
PF SX F 1/4X 1/2 RI UNC-20 G2 PO
PF SX F 1/4X 1.1/2 RI UNC-20 G2 PO
PF SX F 1/4X 2.1/2 RI UNC-20 G2 PO
PF SX F 1/4X 4.1/2 RI UNC-20 G2 PO
PF SX F 1/4X 5.1/2 RI UNC-20 G2 PO
PF SX G 5/16X 1/2 RI UNC-18 G2 PO

In elastic, if I search the way below, no results will return.
{
"query": {
"bool": {
"must": [
{"wildcard": {"descricao": "pf"}},
{"wildcard": {"descricao": "sx*"}},
{"wildcard": {"descricao": "1/2*"}}
]
}
}
}

I believe the problem is the term 1/2. Because if I remove the condition {"wildcard": {"description": "1/2 *"} returns results.

Is there any way to use slashes on the filters?

You will need to change the mapping of the field to not be analyzed, otherwise the slashes will not be indexed.
you can check these 2 posts on how to use slashes in filters:

https://discuss.elastic.co/t/how-to-use-slash-to-search-in-discovery/92340

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.