【问题标题】:elasticsearch wildcard search with and operator使用和运算符进行弹性搜索通配符搜索
【发布时间】:2012-10-11 23:46:09
【问题描述】:

我正在开发一个使用弹性搜索的应用程序,在某些情况下,我想根据术语和语言环境进行搜索。我正在本地主机上测试这个

http://localhost:9200/index/type/_search

和参数

query : {
                        wildcard : { "term" : "myterm*" }
                    },
                    filter : {
                        and : [
                            {
                                term : { "lang" : "en" }
                            },
                            {
                                term : { "translations.lang" : "tr" } //this is subdocument search
                            },
                        ]
                    }

这是一个示例文档:

{
    "_index": "twitter",
    "_type": "tweet",
    "_id": "5084151d2c6e5d5b11000008",
    "_score": null,
    "_source": {
      "lang": "en",
      "term": "photograph",
      "translations": [
        {
          "_id": "5084151d2c6e5d5b11000009",
          "lang": "tr",
          "translation": "fotoğraf",
          "score": "0",
          "createDate": "2012-10-21T15:30:37.994Z",
          "author": "anonymous"
        },
        {
          "_id": "50850346532b865c2000000a",
          "lang": "tr",
          "translation": "resim",
          "score": "0",
          "createDate": "2012-10-22T08:26:46.670Z",
          "author": "anonymous"
        }
      ],
      "author": "anonymous",
      "createDate": "2012-10-21T15:30:37.994Z"
    }
  }

我正在尝试使用通配符(用于自动完成)输入语言“en”和输出语言“tr”来获取术语。它正在获取具有“myterm”但不适用的条款,并对此进行操作。任何建议将不胜感激

提前致谢

【问题讨论】:

  • 你能发布一个你的文档是什么样子的例子吗?

标签: node.js wildcard elasticsearch


【解决方案1】:

我猜translations 元素有nested 类型。如果是这种情况,你应该使用nested查询:

curl -XPOST "http://localhost:9200/twitter/tweet/_search" -d '{
    query: {
        wildcard: {
            "term": "term*"
        }
    },
    filter: {
        and: [{
            term: {
                "lang": "en"
            }
        }, {
            "nested": {
                "path": "translations",
                "query": {
                    "term" : { "translations.lang" : "tr" }
                }
            }
        }]
    }
}'

【讨论】:

  • 感谢您的回复,我的文档没有嵌套。另外,您需要将排序字段设置为not_analyzed。
【解决方案2】:

我已经设法通过以下查询解决了我的问题;

query : {
   wildcard : { "term" : "myterm*" }
},
filter : {
   and : [
      {
         term : { "lang" : "en" }
      },
      {
         term : { "translations.lang" : "tr" } //this is subdocument search
      }
   ]
},
sort : {
   {"term" : "desc"}
}

这里很重要的一点是,您需要将排序字段设置为 not_analyzed。因为,您无法对已分析的字段进行排序。

【讨论】:

    猜你喜欢
    • 2014-03-02
    • 1970-01-01
    • 2021-08-16
    • 1970-01-01
    • 1970-01-01
    • 2019-09-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多