1

I am new to elasticsearch and trying to implement search. Below is my index and settingscurl -XPUT localhost:9200/rets_data/ -d '{ "settings":{ "index":{ "analysis":{ "analyzer":{ "analyzer_startswith":{ "tokenizer":"keyword", "filter":"lowercase" }, "analyzer_whitespacewith":{ "tokenizer":"whitespace", "filter":"lowercase" } } } } }, "mappings":{ "city":{ "properties":{ "CityName":{ "analyzer":"analyzer_startswith", "type":"string" } } }, "rets_aux_subdivision":{ "properties":{ "nn":{ "analyzer":"analyzer_whitespacewith", "type":"string" }, "field_LIST_77":{ "analyzer":"analyzer_whitespacewith", "type":"string" }, "SubDivisionName":{ "analyzer":"analyzer_whitespacewith", "type":"string" }, "SubDivisionAlias":{ "analyzer":"analyzer_whitespacewith", "type":"string" } } }, "rental_aux_subdivision":{ "properties":{ "nn":{ "analyzer":"analyzer_whitespacewith", "type":"string" }, "field_LIST_77":{ "analyzer":"analyzer_whitespacewith", "type":"string" }, "SubDivisionName":{ "analyzer":"analyzer_whitespacewith", "type":"string" }, "SubDivisionAlias":{ "analyzer":"analyzer_whitespacewith", "type":"string" } } } } }'

Below is search string

curl -XGET localhost:9200/rets_data/rets_aux_subdivision/_search?pretty -d '{"query":{"match_phrase_prefix":{"nn":{"query":"boca w","max_expansions":50}}},"sort":{"total":{"order":"desc"}},"size":100}' 

When i am searching for any text like "Boca r", "Boca w" it is not giving me result.

My expected result is below.

"Boca w" should give me result starting with "Boca w". i.e "Boca west", "Boca Woods", "Boca Winds"

Please help me on this.

Thanks

Sanjay Khatri
  • 4,153
  • 7
  • 38
  • 42

2 Answers2

1

You should use edgeNgram. Check this out in elasticsearch documentation.

EdgeNgram filter prepare multiple words from one like this:

Woods->[W,Wo,Woo,Wood,Woods]

It makes index bigger, but searching will be more efficient than any other option like wildcards etc. Here is my simple index creation with ngrams on title.ngram:

{
"settings" : {
"index" : {
"analysis" : {
    "analyzer" : {
        "ngram_analyzer" : {
        "type" : "custom",
        "tokenizer" : "standard",
        "filter" : ["lowercase","my_ngram"]
        }
    },
    "filter" : {
    "my_ngram" : {
    "type" : "edge_ngram",
    "min_gram" : 1,
    "max_gram" : 50
    }
}
}
}
},
  "mappings": 
  {
    "post":
    {
    "properties": 
      {

        "id": 
        {
            "type": "integer",
            "index":"no"
         },
        "title": 
        {
            "type": "text",
            "analyzer":"ngram_analyzer"

        }

      } 
    }
}
}

And search query:

{
  "from" : 0,
  "size" : 10,
 "query" : {
    "match" : {
        "title": 
        {
        "query":"press key han",
        "operator":"or",
        "analyzer":"standard"
      }
      }
  }
}
jgr
  • 2,831
  • 2
  • 15
  • 28
0

What if you have your match something like this:

"query": {
        "match_phrase": {
          "text": {
            "query": "boca w"
          }
        }
      },
"sort":{
    "total":{
        "order":"desc"
       }
    },
"size":100

Or you could use the wildcard query:

"query": {
       "wildcard" : { 
            "yourfield" : "boca w*" 
        }
    }

This SO could be helpful. Hope it helps!

Community
  • 1
  • 1
Kulasangar
  • 9,046
  • 5
  • 51
  • 82