Elasticsaerch Query Exact match

to find the exact match

mappings

"keywords": {
    "type": "object",
    "enabled": True,
    "properties": {
        "keyword_values": {
            "type": "text",
            "analyzer": "synonym_stemmer_bad_words_analyzer",
            "fields": {
                "keyword": {
                    "type": "keyword"
                }
            }
        }
    }
}

Doc 1

"keywords": [
    {
        "keyword_values": "marbles"
    },
    {
        "keyword_values": "flooring"
    }
}

Doc 2

"keywords": [
    {
        "keyword_values": "marble"
    },
    {
        "keyword_values": "floor"
    }
}

Doc 3

"keywords": [
    {
        "keyword_values": "marble floor"
    },
    {
        "keyword_values": "floor marble"
    }
}

If am searching for marble with keywords

using the below query

Query: "keywords.keyword_values"

{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "keywords.keyword_values": {
                            "query": "marble",
                            "operator": "AND",
                            "prefix_length": 0,
                            "max_expansions": 50,
                            "fuzzy_transpositions": "true",
                            "lenient": "false",
                            "zero_terms_query": "NONE",
                            "auto_generate_synonyms_phrase_query": "true",
                            "boost": 7
                        }
                    }
                }
            ],
            "minimum_should_match": "1"
        }
    }
}

I will be getting all three doc results, but I need to fetch only Doc 1 & Doc 2

so I have used the query with the keyword

Query: "keywords.keyword_values.keyword"

{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "keywords.keyword_values.keyword": {
                            "query": "marble",
                            "operator": "AND",
                            "prefix_length": 0,
                            "max_expansions": 50,
                            "fuzzy_transpositions": "true",
                            "lenient": "false",
                            "zero_terms_query": "NONE",
                            "auto_generate_synonyms_phrase_query": "true",
                            "boost": 7
                        }
                    }
                }
            ],
            "minimum_should_match": "1"
        }
    }
}

in this am getting only Doc 2, here i need to fetch Doc 1 & Doc 2

Hi @Mohan_T

Please provide the analyzer below:

{
    "tokens": [
        {
            "token": "marbl",
            "start_offset": 0,
            "end_offset": 6,
            "type": "<ALPHANUM>",
            "position": 0
        }
    ]
}

@RabBit_BR I have updated the analyzer, and the above one am getting the results when am using analyzer

I don't know if you can create a new field and new analyzer but in the example below it was possible to retrieve the docs using the keyword tokenizer.

PUT idx_test
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "keyword",
          "filter": [
            "my_filter"
          ]
        }
      },
      "filter": {
        "my_filter": {
          "type": "stemmer"
        }
      }
    }
  },
  "mappings": {
    "properties": {
      "keywords": {
        "properties": {
          "keyword_values": {
            "type": "text",
            "analyzer": "my_analyzer",
            "fields": {
              "keyword": {
                "type": "keyword"
              }
            }
          }
        }
      }
    }
  }
}

Query

GET idx_test/_search
{
  "query": {
    "match": {
      "keywords.keyword_values": "marbles"
    }
  }
}

@RabBit_BR Thank you, it's working as expected, but I have one more use case, for example, Docs 4 & 5 has the below data.

Doc 4

"keywords": [
    {
        "keyword_values": "marble floor"
    }
}

Doc 5

"keywords": [
    {
        "keyword_values": "floor marble"
    }
}

Search Query

{
    "query": {
        "bool": {
            "should": [
                {
                    "match": {
                        "keywords.keyword_values": {
                            "query": "marble floor",
                            "operator": "AND",
                            "prefix_length": 0,
                            "max_expansions": 50,
                            "fuzzy_transpositions": "true",
                            "lenient": "false",
                            "zero_terms_query": "NONE",
                            "auto_generate_synonyms_phrase_query": "true",
                            "boost": 7
                        }
                    }
                }
            ],
            "minimum_should_match": "1"
        }
    }
}

I want to get Docs 4 & 5 but am getting only Doc 4 using the above query

I saw and other post similiar question and the orientation was using span fist query, maybe work for you.

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