【问题标题】:Elasticsearch search pattern with Start string带有开始字符串的 Elasticsearch 搜索模式
【发布时间】:2016-12-07 12:53:54
【问题描述】:

我是弹性搜索的新手,正在尝试实现搜索。下面是我的索引和设置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”

请帮帮我。

谢谢

【问题讨论】:

  • 你能发一份样本文件吗?

标签: elasticsearch


【解决方案1】:

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

EdgeNgram 过滤器从这样的一个中准备多个单词:

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

它使索引更大,但搜索将比任何其他选项(如通配符等)更有效。这是我在 title.ngram 上使用 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"

        }

      } 
    }
}
}

搜索查询:

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

【讨论】:

  • 我今天试试。希望它能解决我的问题。
【解决方案2】:

如果你的match 是这样的:

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

或者您可以使用wildcard 查询:

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

这个SO 可能会有所帮助。希望对您有所帮助!

【讨论】:

    猜你喜欢
    • 2016-08-15
    • 2015-04-19
    • 2012-08-30
    • 1970-01-01
    • 2013-10-27
    • 1970-01-01
    • 2020-11-16
    • 1970-01-01
    • 2021-11-24
    相关资源
    最近更新 更多