15

我有一个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"
                            }
                        }
                    }
                }
            }
        }
    }
  }
}
4

1 回答 1

28

我猜你正在使用standard分析器,它分为http://example.dom两个标记 -httpexample.com. 你可以看看http://localhost:9200/_analyze?text=http://example.com&analyzer=standard

如果要拆分url,则需要使用不同的分析器或指定我们自己的自定义分析器

您可以看看如何使用简单的分析器url进行索引- 。如您所见, now 被索引为三个标记。如果您不想索引诸如此类的标记,则可以使用小写标记器(这是简单分析器中使用的标记器)指定您的分析器并停止过滤器。例如这样的:http://localhost:9200/_analyze?text=http://example.com&analyzer=simpleurl['http', 'example', 'com']['http', 'www']

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

注意:如果您不喜欢使用停用词,这里有一篇有趣的文章停止停止停用词:查看常用术语查询

于 2013-09-24T11:12:56.917 回答