简体   繁体   English

为什么 elasticsearch matchQuery 不返回结果?

[英]Why elasticsearch matchQuery does not return results?

I have successfully indexed content with elasticsearch, but I am having troubles when trying to query content.我已使用 elasticsearch 成功为内容编制索引,但在尝试查询内容时遇到了麻烦。

What I want to do is to search all docs that both contains value " ipsum " (in any of it's terms) AND term "type" should be equal to "cq:Page".我想要做的是搜索所有包含值“ ipsum ”(在任何术语中)术语“type”应该等于“cq:Page”的文档。

I am using High Level Rest Java Client.我正在使用高级 Rest Java 客户端。 Tried with filtering and with Boolquery but it returns zero results.尝试使用过滤和 Boolquery,但它返回零结果。

Example:例子:

SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.from(0);
sourceBuilder.size(10);

SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("gettingstarted");
searchRequest.source(sourceBuilder);

BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
                boolQueryBuilder.must(QueryBuilders.queryStringQuery("ipsum"));
boolQueryBuilder.must(QueryBuilders.matchQuery("type", "cq:Page"));
sourceBuilder.query(boolQueryBuilder);


SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);

Data looks like this:数据如下所示:

{ 
   "took":4,
   "timed_out":false,
   "_shards":{ 
      "total":1,
      "successful":1,
      "skipped":0,
      "failed":0
   },
   "hits":{ 
      "total":{ 
         "value":1,
         "relation":"eq"
      },
      "max_score":1.0,
      "hits":[ 
         { 
            "_index":"gettingstarted",
            "_type":"_doc",
            "_id":"2",
            "_score":1.0,
            "_source":{ 
               "docs":[ 
                  { 
                     "id":"/content/we-retail/us/en/community/members",
                     "type":"cq:Page",
                     "jcr_title":"Members",
                     "jcr:created":"java.util.GregorianCalendar[time=1564730906165,areFieldsSet=true,areAllFieldsSet=true,lenient=false,zone=sun.util.calendar.ZoneInfo[id=\"GMT+02:00\",offset=7200000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2019,MONTH=7,WEEK_OF_YEAR=31,WEEK_OF_MONTH=1,DAY_OF_MONTH=2,DAY_OF_YEAR=214,DAY_OF_WEEK=6,DAY_OF_WEEK_IN_MONTH=1,AM_PM=0,HOUR=9,HOUR_OF_DAY=9,MINUTE=28,SECOND=26,MILLISECOND=165,ZONE_OFFSET=7200000,DST_OFFSET=0]",
                     "cq:lastModified":"java.util.GregorianCalendar[time=1518654268630,areFieldsSet=true,areAllFieldsSet=true,lenient=false,zone=sun.util.calendar.ZoneInfo[id=\"GMT-05:00\",offset=-18000000,dstSavings=0,useDaylight=false,transitions=0,lastRule=null],firstDayOfWeek=1,minimalDaysInFirstWeek=1,ERA=1,YEAR=2018,MONTH=1,WEEK_OF_YEAR=7,WEEK_OF_MONTH=3,DAY_OF_MONTH=14,DAY_OF_YEAR=45,DAY_OF_WEEK=4,DAY_OF_WEEK_IN_MONTH=2,AM_PM=1,HOUR=7,HOUR_OF_DAY=19,MINUTE=24,SECOND=28,MILLISECOND=630,ZONE_OFFSET=-18000000,DST_OFFSET=0]",
                     "manualCreationDate":"2019-09-05T13:21:00.000+02:00",
                     "jcr:primaryType":"cq:PageContent",
                     "sling:resourceType":"social/console/components/basepage",
                     "searchDescription":"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sportsman delighted improving dashwoods gay instantly happiness six. Ham now amounted absolute not mistaken way pleasant whatever. At an these still no dried folly stood thing. Rapid it on hours hills it seven years. If polite he active county in spirit an. Mrs ham intention promotion engrossed assurance defective. Confined so graceful building opinions whatever trifling in. Insisted out differed ham man endeavor expenses. At on he total their he songs. Related compact effects is on settled do.",
                     "pageImportanceRank":"4"
                  }, ...

Tried this also but without a success, it returns zero results.也试过这个但没有成功,它返回零结果。

boolQueryBuilder.must(QueryBuilders.matchQuery("type", NT_PAGE));

Maybe something like that:也许是这样的:

GET index/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "multi_match": {
            "query": "ipsum",
            "fields": []
          }
        },
        {
          "match": {
            "type": "cq:Page"
          }
        }
      ]
    }
  }
}

"fields": [] -> meaning all fields

It worked with using matchPhraseQuery instead of matchQuery.它使用matchPhraseQuery而不是 matchQuery。 My value was "cp:Page".我的价值是“cp:Page”。

In this case matchQuery returns the document only if this term has exactly the same value as it is in it's index (and at the query time there are no analyzers used ).在这种情况下, matchQuery 仅当该术语具有与其索引中的值完全相同的值时才会返回文档(并且在查询时没有使用分析器)。

On the other hand, matchPhraseQuery will use default analyzers at the query time as well so value in query will be the same as indexed value after analzsers change it.另一方面, matchPhraseQuery 将在查询时使用默认分析器,因此查询中的值将与分析器更改后的索引值相同。

Analyzers can be specified per-query, per-field or per-index.可以按查询、按字段或按索引指定分析器。 At index time, Elasticsearch will look for an analyzer in this order:在索引时,Elasticsearch 将按以下顺序查找分析器:

The analyzer defined in the field mapping.在字段映射中定义的分析器。 An analyzer named default in the index settings.索引设置中名为 default 的分析器。 The standard analyzer.标准分析仪。 At query time, there are a few more layers:在查询时,还有几层:

The analyzer defined in a full-text query.在全文查询中定义的分析器。 The search_analyzer defined in the field mapping.在字段映射中定义的 search_analyzer。 The analyzer defined in the field mapping.在字段映射中定义的分析器。 An analyzer named default_search in the index settings.索引设置中名为 default_search 的分析器。 An analyzer named default in the index settings.索引设置中名为 default 的分析器。 The standard analyzer.标准分析仪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM