0

I would query elasticsearch for retrieve all the document that has field value like a given string.

For example field LIKE "abc" has to return

  • "abc"
  • "abcdef"
  • "abcd"
  • "abc1"

So all the field that has "abc" string inside.

I try this query but return only the document with field = "abc":

{"query":{"more_like_this":{"fields":["FIELD"],"like_text":"abc","min_term_freq" : 1,"max_query_terms" : 12}}}

What is the correct query?

Thanks

michele
  • 26,348
  • 30
  • 111
  • 168

3 Answers3

0

If you're trying to do a Prefix Query, then you can use this.

{ "query": {
  "prefix" : { "field" : "abc" }
}

See ElasticSearch Prefix Query ElasticSearch Prefix Query

0

Although your question is incomplete. I will try to give you several ideas.

One way surely is a prefix query, but much more efficiently is to build an edge ngram analyzer. That way you'll have your data prepared on inserts and query will be much faster. edge ngram is the most flexible way to do your functionality also, because you can autocomplete words that appear in any order. If you don't need to do this, but you only need "search as you type" queries then the best way is to use completion suggester. If you need to find strings that appear in the middle of the words than you can check ngram analyzer.

Here is how I set an edge ngram analyzer from my code.

        "settings": {
            "analysis": {
                "filter"  : {
                    "edge_filter"        : {
                        "type"    : "edge_ngram",
                        "min_gram": 1,
                        "max_gram": 256
                    }
                },
                "analyzer": {
                    "edge_analyzer" : {
                        "type"     : "custom",
                        "tokenizer": "whitespace",
                        "filter"   : ["lowercase", "edge_filter"]
                    },
                    "lowercase_whitespace": {
                        "type": "custom",
                        "tokenizer": "whitespace",
                        "filter": [ "lowercase" ]
                    }
                }
            }
        },
        "mappings": {
            "my_type": {
                "properties": {
                    "name": {
                        "type": "keyword",
                        "fields": {
                            "suggest": {
                                "type": "text",
                                "analyzer" : "edge_analyzer",
                                "search_analyzer": "lowercase_whitespace"
                            }
                        }
                    }
                }
            }
        }
user732456
  • 2,638
  • 2
  • 35
  • 49
-1

You should be able to perform a wildcard query as described here.

Elasticsearch like query

{
  "query": {
    "wildcard": {
      "<<FIELD NAME>>": "*<<QUERY TEXT>>*"
    }
  }
}
AdamPillingTech
  • 456
  • 2
  • 7