我正在寻找一种方法,可以在 Java 中的任意文档上重用 ElasticSearch/Lucene 中的过滤器语法和逻辑(无需先对它们建立索引)。
假设我有一个 JSON 对象
{"wheels":4}
和过滤器:
{"exists":{"field":"windows"}}
该对象不在任何索引中,是否可以重用 ElasticSearch/Lucene 过滤器来测试过滤器上的文档,而不(或之前)将其插入索引(在本例中返回 false)?
最佳答案
是的,您可以,该功能在 Elasticsearch 中称为 Percolator:
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-percolate.html
您使用 percolate api 在 Elasticsearch 中注册搜索,然后通过它渗透文档。它返回匹配或不匹配的位置。下面的示例语法取自文档 - 它应该可以让您很好地了解如何实现这一点:
Sample usage
Create an index with a mapping for the field message:
curl -XPUT 'localhost:9200/my-index' -d '{ "mappings": { "my-type": { "properties": { "message": { "type": "string" } } } } }
Register a query in the percolator:
curl -XPUT 'localhost:9200/my-index/.percolator/1' -d '{ "query" : { "match" : { "message" : "bonsai tree" } } }'
Match a document to the registered percolator queries:
curl -XGET 'localhost:9200/my-index/message/_percolate' -d '{ "doc" : { "message" : "A new bonsai tree in the office" } }'
The above request will yield the following response:
{ "took" : 19, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "total" : 1, "matches" : [ { "_index" : "my-index", "_id" : "1" } ] }
关于java - 重用ElasticSearch过滤器来测试内存中的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25823586/