带有Start字符串的Elasticsearch搜索模式

时间:2016-12-07 12:53:55

标签: elasticsearch

我是elasticsearch的新手并尝试实施搜索。以下是我的索引和设置curl -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" } } } } }'

以下是搜索字符串

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}' 

当我搜索任何文字如“Boca r”,“Boca w”时,它并没有给我结果。

我的预期结果如下。

“Boca w”应该以“Boca w”开头给我结果。即“Boca west”,“Boca Woods”,“Boca Winds”

请帮我解决这个问题。

由于

2 个答案:

答案 0 :(得分:1)

你应该使用edgeNgram。在elasticsearch文档中查看。

EdgeNgram过滤器准备多个单词,如下所示:

  

Woods-> [W,禾,佑,木材,伍兹]

它使索引更大,但搜索将比任何其他选项(如通配符等)更有效。这是我在title.ngram上使用ngrams创建的简单索引:

{
"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"

        }

      } 
    }
}
}

并搜索查询:

{
  "from" : 0,
  "size" : 10,
 "query" : {
    "match" : {
        "title": 
        {
        "query":"press key han",
        "operator":"or",
        "analyzer":"standard"
      }
      }
  }
}

答案 1 :(得分:0)

如果你有match这样的内容怎么办:

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

或者您可以使用wildcard查询:

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

SO可能会有所帮助。希望它有所帮助!