Elasticsearch在过滤​​器上使用自定义分析器

时间:2015-01-06 17:56:05

标签: search filter elasticsearch analyzer

我在elasticsearch nested filter return empty result询问我在查询中遇到的一些错误并且没有得到任何结果,但在答案中我指出我用于过滤器的表达式并未作为I进行分析期望的。

我有一个自定义分析器来完成这项工作我如何在下一个查询中指定过滤器来使用这个自定义分析器:

GET /develop/_search?search_type=dfs_query_then_fetch
{
  "query": {
    "filtered" : {
        "query": {
          "bool": {
            "must": [
              { "match": { "title": "post" }}
            ]
          }
        },
        "filter": {
          "bool": { 
            "must": [
              {"term": {
                "featured": 0
              }},
              { 
              "nested": {
                "path": "seller",
                "filter": {
                  "bool": {
                    "must": [
                      { "term": { "seller.firstName": "Test 3" } }
                    ]
                  }
                },
                "_cache" : true
              }}
            ]
          } 
        }
    }
  },
  "sort": [
    {
      "_score":{
        "order": "desc"
      }
    },{
      "created": {
        "order": "desc"
      }
    }
  ],
  "track_scores": true
} 

1 个答案:

答案 0 :(得分:1)

这是一个似乎可以做你想要的设置。我使用与最后一个答案相同的基本代码,但在索引定义中使用了index_analyzersearch_analyzer,如下所示:

curl -XDELETE "http://localhost:9200/my_index"

curl -XPUT "http://localhost:9200/my_index" -d'
{
   "settings": {
      "number_of_shards": 1,
      "number_of_replicas": 0,
      "analysis": {
         "filter": {
            "snowball": { "type": "snowball", "language": "English" },
            "english_stemmer": { "type": "stemmer", "language": "english" },
            "english_possessive_stemmer": { "type": "stemmer", "language": "possessive_english" },
            "stopwords": { "type": "stop",  "stopwords": [ "_english_" ] },
            "worddelimiter": { "type": "word_delimiter" }
         },
         "tokenizer": {
            "nGram": { "type": "nGram", "min_gram": 3, "max_gram": 20 }
         },
         "analyzer": {
            "custom_analyzer": {
               "type": "custom",
               "tokenizer": "nGram",
               "filter": [
                  "stopwords",
                  "asciifolding",
                  "lowercase",
                  "snowball",
                  "english_stemmer",
                  "english_possessive_stemmer",
                  "worddelimiter"
               ]
            },
            "custom_search_analyzer": {
               "type": "custom",
               "tokenizer": "standard",
               "filter": [
                  "stopwords",
                  "asciifolding",
                  "lowercase",
                  "snowball",
                  "english_stemmer",
                  "english_possessive_stemmer",
                  "worddelimiter"
               ]
            }
         }
      }
   },
   "mappings": {
      "posts": {
         "properties": {
            "title": {
               "type": "string",
               "analyzer": "custom_analyzer",
               "boost": 5
            },
            "seller": {
               "type": "nested",
               "properties": {
                  "firstName": {
                     "type": "string", 
                     "index_analyzer": "custom_analyzer", 
                     "search_analyzer": "custom_search_analyzer",
                     "boost": 3
                  }
               }
            }
         }
      }
   }
}'

然后添加了测试文档

curl -XPUT "http://localhost:9200/my_index/posts/1" -d'
{"title": "post", "seller": {"firstName":"Test 1"}}'
curl -XPUT "http://localhost:9200/my_index/posts/2" -d'
{"title": "post", "seller": {"firstName":"Test 2"}}'
curl -XPUT "http://localhost:9200/my_index/posts/3" -d'
{"title": "post", "seller": {"firstName":"Test 3"}}'

然后match中的几个bool个查询,其中一个是multiword query,似乎可以实现您的目标:

curl -XPOST "http://localhost:9200/my_index/_search" -d'
{
   "query": {
      "bool": {
         "must": [
            {
               "match": {
                  "title": "post"
               }
            },
            {
               "nested": {
                  "path": "seller",
                  "query": {
                     "match": {
                        "seller.firstName": {
                            "query": "Test 3",
                            "operator": "and"
                        }
                     }
                  }
               }
            }
         ]
      }
   }
}'
...
{
   "took": 5,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 6.8380365,
      "hits": [
         {
            "_index": "my_index",
            "_type": "posts",
            "_id": "3",
            "_score": 6.8380365,
            "_source": {
               "title": "post",
               "seller": {
                  "firstName": "Test 3"
               }
            }
         }
      ]
   }
}

以下是我使用的代码:

http://sense.qbox.io/gist/8cd954aa60be8c44f64e4282e15e6b565c945ecb

这能解决您的问题吗?