java - 重用ElasticSearch过滤器来测试内存中的对象

标签 java lucene elasticsearch

我正在寻找一种方法,可以在 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/

相关文章:

java - 添加成员/ setter 时默认 `equals` 的行为会改变吗?

elasticsearch - 我是ES新手,在ES中有一个多匹配查询,并且想根据其可用性考虑字段

elasticsearch - 如何在Logstash ElasticSearch中使用_timestamp

java - 通过在索引处插入来操作字符串

java - Webflux postgresql

java - 如何将此方法与另一种方法结合使用?

java - 确定哪个 jar 文件包含 org.apache.lucene 类文件

python - 方面,并根据方面进行一些过滤

Solr - 仅匹配精确的短语

symfony - 在没有 ES 服务器的情况下使用 FOSElasticaBundle 对 Symfony 应用程序进行单元测试?