How to implement with complicate wording (hero hero hero)

My data is
Doc 1 hero hero hero
Doc 2 hero hero
Doc 3 hero
We would have Result is Doc 3:"hero"
How do I implement this case use regular expression or wild card

can you provide detail information on it ..
it is incomplete ,and write some query that you have tried such that we can understand what you have tried and what you want.

Thank you for you quick reply
.
GET autocomplete/_search
{
"query": {
"multi_match": {
"query": "hero",
"fields": [
"Name"
]
}
},
"regexp": {
"menu_name": {
"value": "hero*|hero*",
"flags": "ALL",
"max_determinized_states": 10000,
"rewrite": "constant_score"
}
}
}

Now can you tell me that what you want from query ??

  • are you trying to get the field with only one hero or what?

Yes sir I would result only "hero"

can i get a sample data ...without data i cant check

Index ngram

PUT autocomplete_test
{
   "settings": {
        "analysis": {
            "analyzer": {
                "trigrams": {
                    "tokenizer": "trigram_tokenizer",
                    "filter":   [
                        "lowercase"
                    ]
                }
            },
            "tokenizer": {
                "trigram_tokenizer": {
                    "type": "ngram",
                    "min_gram": 3,
                    "max_gram": 3,
                    "token_chars": []
                }
            }
        }
    },
    "mappings": {

            "properties": {
                "menu_name": {
                    "type":     "text",
                    "analyzer": "trigrams" 
                }
            }
        
    }
}

Put data

PUT autocomplete_test/_doc/1
{
  "doc1": "hero hero hero"
  
}

PUT autocomplete_test/_doc/2
{
  "doc1": "hero hero"
}

PUT autocomplete_test/_doc/3
{
  "doc1": "hero"
}

**Data**
{
  "took" : 712,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "autocomplete_test",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 1.0,
        "_source" : {
          "doc1" : "hero hero hero"
        }
      },
      {
        "_index" : "autocomplete_test",
        "_type" : "_doc",
        "_id" : "2",
        "_score" : 1.0,
        "_source" : {
          "doc1" : "hero hero"
        }
      },
      {
        "_index" : "autocomplete_test",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 1.0,
        "_source" : {
          "doc1" : "hero"
        }
      }
    ]
  }
}

**query **

GET autocomplete/_search
{
"query": {
"multi_match": {
"query": "hero",
"fields": [
"Name"
]
}
},
"regexp": {
"menu_name": {
"value": "hero*|hero*",
"flags": "ALL",
"max_determinized_states": 10000,
"rewrite": "constant_score"
}
}
}
1 Like

Sorry if i am wrong

Why didn't you use keyword since you need exact matching text . like below:


GET autocomplete_test/_search
{
  "query": {
    "match": {
      "doc1.keyword": "hero"
    }
  }
}

Output

{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.9808292,
    "hits" : [
      {
        "_index" : "autocomplete_test",
        "_type" : "_doc",
        "_id" : "3",
        "_score" : 0.9808292,
        "_source" : {
          "doc1" : "hero"
        }
      }
    ]
  }
}

This feature made for auto complete

"Hero" is result we want
Is we serch

H :Doc1,Doc2,Doc3
He :Doc1,Doc2,Doc3
Her : Doc1,Doc2,Doc3
Hero : Doc1,Doc2,Doc3

But we need only "Hero":Doc3

We not sure we need how to implement by Regexp query or script Regexp query

as per my knowledge it is difficult to achieve ...but as per me it is not possible ..
If you find any solution please enlighten me too

Sure I will try and I will let you know.

1 Like

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