elasticsearch - Elasticsearch-带通配符的query_string

标签 elasticsearch query-string

我在 flex 搜索中有一些文本,其中包含各种格式的url(http://www,www。)。我想做的就是搜索所有包含google.com的文本。

对于当前搜索,我使用类似以下查询的内容:

query = { "query": {
                "bool": {
                     "must": [{
                            "range": {
                            "cdate": {
                                "gt": dfrom,
                                "lte": dto }
                            }
                        },
             { "query_string":{
                "default_operator": "AND",
                "default_field": "text",
                "analyze_wildcard":"true",
                "query": searchString } }
            ]
        }
        }}

但是看起来像google.com的查询永远不会返回任何结果,例如搜索“test”一词可以正常工作(不带“”)。我确实想使用query_string,因为我想使用 bool(boolean) 运算符,但我确实需要不仅可以搜索整个单词的子字符串。

谢谢 !

最佳答案

的确,标准分析器将http://www.google.com标记为httpwww.google.com,因此找不到google.com

因此,仅标准分析器将无济于事,我们需要一个 token 过滤器来正确转换URL token 。如果您的text字段仅包含URL的另一种方式是使用UAX Email URL tokenizer,但是由于该字段可以包含任何其他文本(即用户注释),因此将无法使用。

幸运的是,周围有一个名为analysis-url的新插件,它提供了URL token 过滤器,而这正是我们所需要的(我恳求了small modification之后,谢谢@jlinn ;-))

首先,您需要安装插件:

bin/plugin install https://github.com/jlinn/elasticsearch-analysis-url/releases/download/v2.2.0/elasticsearch-analysis-url-2.2.0.zip

然后,我们可以开始播放了。我们需要为您的text字段创建适当的分析器:
curl -XPUT localhost:9200/test -d '{
  "settings": {
    "analysis": {
      "filter": {
        "url_host": {
          "type": "url",
          "part": "host",
          "url_decode": true,
          "passthrough": true
        }
      },
      "analyzer": {
        "url_host": {
          "filter": [
            "url_host"
          ],
          "tokenizer": "whitespace"
        }
      }
    }
  },
  "mappings": {
    "url": {
      "properties": {
        "text": {
          "type": "string",
          "analyzer": "url_host"
        }
      }
    }
  }
}'

使用此分析器和映射,我们可以正确索引您要搜索的主机。例如,让我们使用新的分析器分析字符串blabla bla http://www.google.com blabla
curl -XGET 'localhost:9200/urls/_analyze?analyzer=url_host&pretty' -d 'blabla bla http://www.google.com blabla'

我们将获得以下 token :
{
  "tokens" : [ {
    "token" : "blabla",
    "start_offset" : 0,
    "end_offset" : 0,
    "type" : "word",
    "position" : 0
  }, {
    "token" : "bla",
    "start_offset" : 0,
    "end_offset" : 0,
    "type" : "word",
    "position" : 1
  }, {
    "token" : "www.google.com",
    "start_offset" : 0,
    "end_offset" : 0,
    "type" : "word",
    "position" : 2
  }, {
    "token" : "google.com",
    "start_offset" : 0,
    "end_offset" : 0,
    "type" : "word",
    "position" : 3
  }, {
    "token" : "com",
    "start_offset" : 0,
    "end_offset" : 0,
    "type" : "word",
    "position" : 4
  }, {
    "token" : "blabla",
    "start_offset" : 0,
    "end_offset" : 0,
    "type" : "word",
    "position" : 5
  } ]
}

如您所见,http://www.google.com部分将被标记为:
  • www.google.com
  • google.com,即您期望的
  • com

  • 因此,现在,如果您的searchStringgoogle.com,您将能够找到所有包含text(或google.com)的www.google.com字段的文档。

    关于elasticsearch - Elasticsearch-带通配符的query_string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34887458/

    相关文章:

    elasticsearch - 在LogStash中写入@timestamp

    elasticsearch - 如何解决Couchbase Elasticsearch复制中的索引错误?

    elasticsearch - elasticsearch查询不支持query_string?

    seo - 用查询字符串分页不好吗?

    ruby-on-rails - 通过 Rails 中的查询字符串从表单传递数据

    linux - 将参数发布到 htaccess sef 链接

    go - 在 Go 的 http 包中,如何获取 POST 请求的查询字符串?

    android - 如何制作REST API来查询干草堆elasticsearch?

    elasticsearch - ElasticSearch分析器以匹配 “Java”, “Script”和 “JavaScript”

    elasticsearch - Elasticsearch Java API:等到在搜索结果中找到文档?