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

JR *_*lia 14 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"
                            }
                        }
                    }
                }
            }
        }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

小智 25

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

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

你可以看看如何url简单的分析器索引- http://localhost:9200/_analyze?text=http://example.com&analyzer=simple.如您所见,现在被url编入索引为三个令牌['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"
    }
  }
}'
Run Code Online (Sandbox Code Playgroud)

注意:如果你不喜欢在这里使用停用词是有趣的文章停止停止停止词:看看常见术语查询