在弹性搜索中索引网站/网址

时间:2013-09-24 09:33:44

标签: elasticsearch

我有弹性搜索索引的文档的website字段。示例值:http://example.com。问题是,当我搜索example时,文档不包含在内。如何正确映射网站/网址?

我在下面创建了索引:

{
  "settings":{
    "index":{
        "analysis":{
        "analyzer":{
            "analyzer_html":{
                  "type":"custom",
                  "tokenizer": "standard",
                "filter":"standard",
                "char_filter": "html_strip"
            }
        }
        }
    }
  },
  "mapping":{
    "blogshops": {
        "properties": {
            "category": {
                "properties": {
                    "name": {
                        "type": "string"
                    }
                }
            },
            "reviews": {
                "properties": {
                    "user": {
                        "properties": {
                            "_id": {
                                "type": "string"
                            }
                        }
                    }
                }
            }
        }
    }
  }
}

1 个答案:

答案 0 :(得分:24)

我猜您使用的是standard分析器,它将http://example.dom拆分为两个令牌 - httpexample.com。你可以看看http://localhost:9200/_analyze?text=http://example.com&analyzer=standard

如果您要拆分url,则需要使用不同的analyzer或指定我们自己的custom analyzer

您可以查看urlsimple analyzer - http://localhost:9200/_analyze?text=http://example.com&analyzer=simple的索引编制方式。如您所见,现在url被编入索引为三个标记['http', 'example', 'com']。如果您不想索引['http', 'www']等令牌,则可以使用lowercase tokenizer(这是简单分析器中使用的那个)和stop filter指定您的分析器。例如:

# Delete index
#
curl -s -XDELETE 'http://localhost:9200/url-test/' ; echo

# Create index with mapping and custom index
#
curl -s -XPUT 'http://localhost:9200/url-test/' -d '{
  "mappings": {
    "document": {
      "properties": {
        "content": {
          "type": "string",
          "analyzer" : "lowercase_with_stopwords"
        }
      }
    }
  },
  "settings" : {
    "index" : {
      "number_of_shards" : 1,
      "number_of_replicas" : 0
    },
    "analysis": {
      "filter" : {
        "stopwords_filter" : {
          "type" : "stop",
          "stopwords" : ["http", "https", "ftp", "www"]
        }
      },
      "analyzer": {
        "lowercase_with_stopwords": {
          "type": "custom",
          "tokenizer": "lowercase",
          "filter": [ "stopwords_filter" ]
        }
      }
    }
  }
}' ; echo

curl -s -XGET 'http://localhost:9200/url-test/_analyze?text=http://example.com&analyzer=lowercase_with_stopwords&pretty'

# Index document
#
curl -s -XPUT 'http://localhost:9200/url-test/document/1?pretty=true' -d '{
  "content" : "Small content with URL http://example.com."
}'

# Refresh index
#
curl -s -XPOST 'http://localhost:9200/url-test/_refresh'

# Try to search document
#
curl -s -XGET 'http://localhost:9200/url-test/_search?pretty' -d '{
  "query" : {
    "query_string" : {
        "query" : "content:example"
    }
  }
}'

注意:如果您不喜欢使用停用词,请参阅有趣的文章stop stopping stop words: a look at common terms query